I am trying to plot the following code where data1, data2, data3 are vectors.
data1 = np.array(means1)
print('data1=',data1)
data2 = np.array(ci_l)
print('data2',data2)
data3 = np.array(ci_h)
print('data3',data3)
x = data1
y = np.concatenate([data2[:,None],data3[:,None]], axis=1)
print('x=', x,'y=',y)
plt.plot(x, [i for (i,j) in y], 'rs', markersize = 4)
plt.plot(x, [j for (i,j) in y], 'bo', markersize = 4)
plt.show()
For each x points as you see in the code I have two y points. When I run the code I obtain the following output:
data1= [[22.8]
[31.6]
[27.4]
[30.4]
[30.6]]
data2 [[21.80474319]
[30.60474319]
[26.40474319]
[29.40474319]
[29.60474319]]
data3 [[23.79525681]
[32.59525681]
[28.39525681]
[31.39525681]
[31.59525681]]
x= [[22.8]
[31.6]
[27.4]
[30.4]
[30.6]] y= [[[21.80474319]
[23.79525681]]
[[30.60474319]
[32.59525681]]
[[26.40474319]
[28.39525681]]
[[29.40474319]
[31.39525681]]
[[29.60474319]
[31.59525681]]]
and this figure:
My question is how to plot a line that connect each y pair? My problem is similar to this:
< Matplotlib how to draw vertical line between two Y points >
I try to add the following line as suggested to the code:
plt.plot((x,x),([i for (i,j) in y], [j for (i,j) in y]),c='black')
but I obtain the following error:
Traceback (most recent call last):
File "/Users/oltiana/Desktop/datamining/chapter4.py", line 151, in <module>
plt.plot((x,x),([i for (i,j) in y], [j for (i,j) in y]),c='black')
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/pyplot.py", line 3019, in plot
return gca().plot(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_axes.py", line 1605, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 315, in __call__
yield from self._plot_args(this, kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 504, in _plot_args
raise ValueError(f"x and y can be no greater than 2D, but have "
ValueError: x and y can be no greater than 2D, but have shapes (2, 5, 1) and (2, 5, 1)
I try to solve the problem using shape and reshape bout still does not work. Any suggestion would be helpful for me. Thank you!