首页 > 解决方案 > 如何使用 2 y 缩放轴和共享 X 轴进行绘图?

问题描述

我正在尝试在同一图表上绘制一个带有一些 y 轴的浮动聚集条形图,其中一些散点图数据具有相同的 X 单位但不同的 y 单位。

我尝试使用函数 axis.twinx() 遵循以下帖子中的示例

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

由于我的条形图的 x 最大值为 2400,而我的散点图的 x 最大值约为 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 值相关联。

标签: pythonpython-3.xmatplotlibplot

解决方案


推荐阅读