Home > Net >  ValueError: x and y can be no greater than 2-D, but have shapes (2,) and (2, 1, 991)
ValueError: x and y can be no greater than 2-D, but have shapes (2,) and (2, 1, 991)

Time:07-07

I wanna plot my Data but I get this Error. Do u know what should I do ?

fig, ax = plt.subplots(figsize=(12,8))

ax.plot(wl, dat[:], color='black')

ax.plot(wl2, dat2[:], color='green')


ax.axvline(4861.32,color='b', linewidth=1)

ax.text(4861.32   0.2, np.mean(dat)   4.8*np.std(dat), "H"  r'$\beta$', fontsize = 10, color = 'b')

ax.set_title('Spectra', fontsize = 15)

ax.set_xlabel(r'Wavelength [$\mathrm{\AA}$]',fontsize=15)

ax.set_ylabel(r'Flux [erg/cm2/cm/A]', fontsize = 15)
ax.set_xlim(3700,5000)

ax.set_ylim([np.mean(dat2) - 1.5*np.std(dat2), np.mean(dat2)   2*np.std(dat2)])

Traceback

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [81], in <cell line: 5>()
      1 fig, ax = plt.subplots(figsize=(12,8))
      3 ax.plot(wl, dat[:], color='black')
----> 5 ax.plot(wl2, dat2[:], color='green')

File ~\anaconda3\envs\py10\lib\site-packages\matplotlib\axes\_axes.py:1632, in Axes.plot(self, scalex, scaley, data, *args, **kwargs)
   1390 """
   1391 Plot y versus x as lines and/or markers.
   1392 
   (...)
   1629 (``'green'``) or hex strings (``'#008000'``).
   1630 """
   1631 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1632 lines = [*self._get_lines(*args, data=data, **kwargs)]
   1633 for line in lines:
   1634     self.add_line(line)

File ~\anaconda3\envs\py10\lib\site-packages\matplotlib\axes\_base.py:312, in _process_plot_var_args.__call__(self, data, *args, **kwargs)
    310     this  = args[0],
    311     args = args[1:]
--> 312 yield from self._plot_args(this, kwargs)

File ~\anaconda3\envs\py10\lib\site-packages\matplotlib\axes\_base.py:501, in _process_plot_var_args._plot_args(self, tup, kwargs, return_kwargs)
    498     raise ValueError(f"x and y must have same first dimension, but "
    499                      f"have shapes {x.shape} and {y.shape}")
    500 if x.ndim > 2 or y.ndim > 2:
--> 501     raise ValueError(f"x and y can be no greater than 2D, but have "
    502                      f"shapes {x.shape} and {y.shape}")
    503 if x.ndim == 1:
    504     x = x[:, np.newaxis]

ValueError: x and y can be no greater than 2D, but have shapes (2,) and (2, 1, 991)

CodePudding user response:

  • There are currently 4 arrays of data, wl, dat, wl2, dat2.
  • wl and wl2 have the following shape.
import numpy as np

wl = wl2 = np.array([0, 1])

# wl and wl2 look essentially like this
array([0, 1])

>>> wl.shape
(2,)
  • dat looks essentially like wl and wl2, with the same shape
  • dat2 does not look the same because it's a 3D array, and the shape of wl2 doesn't match dat2.
# sample data in the same shape as dat2
np.random.seed(2022)
dat2 = np.random.normal(4, 1, (2, 1, 991))

>>> dat2.shape
(2, 1, 991)

# dat2 looks like the following
array([[[3.9994721 , 3.72509858, 3.86071444, ..., 4.24822846,
         2.7409143 , 5.96405405]],

       [[3.54264793, 5.54945521, 5.68753333, ..., 5.88797684,
         3.98211438, 5.69521236]]])
  • dat2 can't be plotted against wl2 because the shapes do not match.
    • dat2 can be sliced (dat2[:, :, 0]) to match the shape of wl2, where 0 is 1 pair of numbers out of 991 pairs.
>>> dat2[:, :, 0]
array([[3.9994721 ],
       [3.54264793]])

>>> dat2[:, :, 0].shape
(2, 1)  # which corresponds to (2,) and will plot without issue
  • See other resources:
    • enter image description here

      5 pairs of numbers (out of 991 pairs)

      fig, ax = plt.subplots(figsize=(12,8))
      for i, pair in enumerate(zip(*dat2[0, :, :5], *dat2[1, :, :5]), 1):
          ax.plot(wl2, pair, marker='o', label=f'Pair: {i}')
      ax.legend()
      plt.show()
      

      enter image description here

      Each group of 991

      fig, ax = plt.subplots(figsize=(12, 8))
      for i, col in enumerate(dat2):
          ax.plot(col.flat, marker='.', label=f'Col: {i}')
      ax.legend()
      plt.show()
      

      enter image description here

  • Related