在 x 和 y 值上为 pyplot 提供不同大小的数组
Feeding pyplot with different size arrays on x and y values
我有两个 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 轴范围的图表中再添加两条线。但它 returns 错误 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")
哪个returns错误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()
我有两个 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 轴范围的图表中再添加两条线。但它 returns 错误 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")
哪个returns错误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()