【发布时间】:2021-11-26 19:54:41
【问题描述】:
我正在尝试绘制以下代码,其中 data1、data2、data3 是向量。
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()
对于您在代码中看到的每个 x 点,我有两个 y 点。当我运行代码时,我得到以下输出:
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]]]
还有这个图:
我的问题是如何绘制一条连接每对 y 对的线?我的问题与此类似:
Matplotlib how to draw vertical line between two Y points>
我尝试按照代码中的建议添加以下行:
plt.plot((x,x),([i for (i,j) in y], [j for (i,j) in y]),c='black')
但我收到以下错误:
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)
我尝试使用 shape 和 reshape bout 解决问题仍然不起作用。任何建议都会对我有所帮助。谢谢!
【问题讨论】:
标签: python python-3.x matplotlib statistics