ValueError: x and y must have same first dimension, but have shapes (16,) and (20,)

Khawar_Islam: <p>I already gone through stack overflow answer but i didn’t find any suitable answer. I am simply draw a PSNR graph between two excel values. The code is mentioned below</p>
<pre><code class="lang-auto">import os

import numpy as np
from scipy.misc import imread
import matplotlib.pyplot as plt

line = True

lstm_ssim = np.genfromtxt('test/psnr/bpg_psnr.csv', delimiter=',')
lstm_ssim = lstm_ssim[:, :-1]
if line:
    lstm_ssim = np.mean(lstm_ssim, axis=0)
    lstm_bpp = np.arange(1, 17) / 192 * 24
    plt.plot(lstm_bpp, lstm_ssim, label='LSTM', marker='o')
else:
    lstm_bpp = np.stack([np.arange(1, 17) for _ in range(24)]) / 192 * 24
    plt.scatter(
        lstm_bpp.reshape(-1), lstm_ssim.reshape(-1), label='LSTM', marker='o')

jpeg_ssim = np.genfromtxt('test/psnr/jpeg_psnr.csv', delimiter=',')
jpeg_ssim = jpeg_ssim[:, :-1]
if line:
    jpeg_ssim = np.mean(jpeg_ssim, axis=0)

jpeg_bpp = np.array([
    os.path.getsize('test/jpeg/kodim{:02d}/{:02d}.jpg'.format(i, q)) * 8 /
    (imread('test/jpeg/kodim{:02d}/{:02d}.jpg'.format(i, q)).size // 3)
    for i in range(1, 25) for q in range(1, 21)
]).reshape(24, 20)

if line:
    jpeg_bpp = np.mean(jpeg_bpp, axis=0)
    plt.plot(jpeg_bpp, jpeg_ssim, label='JPEG', marker='x')
else:
    plt.scatter(
        jpeg_bpp.reshape(-1), jpeg_ssim.reshape(-1), label='JPEG', marker='x')

plt.xlim(0., 2.)
plt.ylim(0.7, 1.0)
plt.xlabel('bit per pixel')
plt.ylabel('PSNR')
plt.legend()
plt.show()

</code></pre>
<p>There are 20 columns and 24 rows are present in excel.<br>
                    <a href="https://i.stack.imgur.com/z9QUF.png" target="_blank" rel="noopener nofollow ugc" class="onebox">
            <img src="https://i.stack.imgur.com/z9QUF.png" width="690" height="233">
          </a>

</p>
<p>Python error</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "test/psnr/draw_psnr.py", line 14, in &lt;module&gt;
    plt.plot(lstm_bpp, lstm_ssim, label='LSTM', marker='o')
  File "/home/khawar/anaconda3/envs/RNN_Fresh/lib/python3.5/site-packages/matplotlib/pyplot.py", line 2749, in plot
    *args, scalex=scalex, scaley=scaley, data=data, **kwargs)
  File "/home/khawar/anaconda3/envs/RNN_Fresh/lib/python3.5/site-packages/matplotlib/__init__.py", line 1785, in inner
    return func(ax, *args, **kwargs)
  File "/home/khawar/anaconda3/envs/RNN_Fresh/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 1604, in plot
    for line in self._get_lines(*args, **kwargs):
  File "/home/khawar/anaconda3/envs/RNN_Fresh/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 393, in _grab_next_args
    yield from self._plot_args(this, kwargs)
  File "/home/khawar/anaconda3/envs/RNN_Fresh/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 370, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "/home/khawar/anaconda3/envs/RNN_Fresh/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 231, in _xy_from_xy
    "have shapes {} and {}".format(x.shape, y.shape))
ValueError: x and y must have same first dimension, but have shapes (16,) and (20,)
(RNN_Fresh) khawar@khawar-B365-HD3:~/Desktop/RNN_Fresh/pytorch-image-comp-rnn$ 
</code></pre>

KarthikR: <p>Can you print the shapes you feed to Matplotlib and see what they are?</p>

Khawar_Islam: <p>lstm_ssim.shape: The shape is (24, 21)<br>
print(lstm_bpp.shape)<br>
(16,)</p>

KarthikR: <p>I mean, the shapes of variables (lstm_bpp, lstm_ssim) you pass to Matplotlib</p>

Khawar_Islam: <p>lstm_ssim.shape: The shape is (24, 21)<br>
print(lstm_bpp.shape)<br>
(16,)</p>

KarthikR: <p>Can you try to isolate which segment is giving the error (as you use plot and scatter plots) and for that, can you reshape the values you pass to Matplotlib?</p>
<p>Eg:<br>
import numpy as np<br>
import matplotlib.pyplot as plt</p>
<p>x = np.random.rand(24,20)<br>
x = x[:, :-1]<br>
print(x.shape)<br>
x = x.mean(axis=0)</p>
<p>y = np.arange(1, 17) / 192 * 24<br>
<span class="hashtag">#plt</span>.plot(x,y)  <span class="hashtag">#x</span> and y must have same first dimension, but have shapes (19,) and (16,)<br>
y = np.arange(1, 20) / 192 * 24<br>
plt.plot(x,y)  <span class="hashtag">#This</span> works</p>