如何用2个y缩放轴和共享的X轴绘图?

时间:2019-08-05 21:46:41

标签: python python-3.x matplotlib plot

我正在尝试在同一图形上绘制带有某些y轴的浮动群集条形图,并使用一些散点图数据,这些散点图数据具有相同的X单位但具有不同的y单位。

我尝试使用功能axis.twinx()

遵循以下文章中的示例

https://matplotlib.org/gallery/api/two_scales.html

由于我的条形图的x最大值为2400,散点图的max值为1600,因此具有共享x轴的新组合图将与显示的轴与散点图上的值不匹配。

例如。散点图的最终值(1600,y)处于图形显示x = 2400的位置,但是当悬停在该点上时,这些点正确显示为(1600,y)。

如何用x中第二个轴的正确缩放比例来完成这2个缩放的Y轴?

plt,clf()
fig, ax1 = plt.subplot()

#part of my bar graph code
#pw_min = plt.barh(y_pos, w_min,gwidth)
ax1.pw_max52 = plt.barh(y_pos+4*gw, w_max52, height = gw, left = w_min, color = 'b')
ax1.pw_max89 = plt.barh(y_pos+2*gw, w_max89, height = gw, left = w_min, color = 'r')
#ax.twinx()
ax2 = ax1.twinx()

#plotting the scatter plot on the second axes we just made with twinx()
hwscat = ax2.plot(xsca, ysca, 'g.',xscb, yscb, 'b.')

plt.show()

似乎第二把斧头只是将绘图放置在空间中的位置,而不是将它们与正确的x值相关。

0 个答案:

没有答案