在x和y值上提供具有不同大小数组的pyplot



我有两个numpy数组。一个用于x轴条目,另一个用于y轴,如您在下面的代码中所见

plt.figure(figsize=(10, 10))
plt.plot(range(0,len(TVals_R)),TVals,'bo',markersize=1,label='Dry Run') #I need x and y arrays in different size here
plt.figure(figsize=(10, 10))
plt.ylabel('Temperature ($^circ$C)')
plt.xlabel('Measurement')
plt.title("Temperature vs. Measurement")
plt.legend(loc="upper right")

在x轴上,我想使用一个比y大的数组,比如len(TVals_R)。因为我将在具有不同x轴范围的图中再添加两条线。但它返回错误ValueError: x and y must have same first dimension, but have shapes (920,) and (498,)

有没有办法在pylot上使用不同大小的列表?

我也尝试过使用不同的轴在图中添加两条不同大小的线,但由于我有第三条线,我无法使用它

plt.figure(figsize=(10, 10))
fig,ax1=plt.subplots()
ax2=ax1.twiny()
ax3=ax1.twiny()
curve1, = ax1.plot(range(0,len(TVals)),TVals,'bo',markersize=1,label='Dry Run')
curve2, = ax2.plot(range(0,len(TVals_R)),TVals_R,'ro',markersize=1,label='Radiation Run')
curve3, = ax3.plot(range(0,len(TVals)),TVals_interpolated_R,'go',markersize=1,label='handheld meter and n linear interpolation')
curves = [curve1,curve2,curve3]
ax2.legend(curves, [curve.get_label() for curve in curves]) 
ax1.set_xlabel('Measurement', color=curve1.get_color()) 
ax2.set_xlabel('Measurement', color=curve2.get_color())
ax1.set_ylabel('Temperature ($^circ$C)')  
plt.ylabel('Temperature ($^circ$C)')
#plt.xlabel('Measurement')
plt.title("Temperature vs. Measurement")

返回错误ValueError: x and y must have same first dimension, but have shapes (920,) and (498,)

我没有将轴分为ax1、ax2等,而是尝试将空元素添加到较小的列表中以匹配最大的列表,但在numpy中添加[]会添加0 (zero),这在我的数据中是误导性的

我感谢在这两种方法上提供的任何帮助。

看起来最好的方法是忽略错误,这样可以覆盖具有不同x轴范围的2个图。这种解决方案不需要分离。

plt.figure(figsize=(10, 10))
try:
plt.plot(range(0,len(TVals)),TVals,'o',markersize=1,label='Dry Run')
plt.plot(range(0,len(TVals_R)),TVals_R,'ro',markersize=1,label='Radiation Run')
plt.plot(range(0,len(TVals)),TVals_interpolated,'go',markersize=1,label='handheld meter and n linear interpolation')
except ValueError:
pass
plt.ylabel('Temperature ($^circ$C)')
plt.xlabel('Measurement')
plt.title("Temperature vs. Measurement")
plt.legend(loc="upper right")
plt.show()

最新更新