我可以创建此图:

import matplotlib.pyplot as plt

x = range(1, 16)
zero = [0] * 15
pointfive = [0.5] * 15

fig = plt.figure()

ax1 = fig.add_subplot(111)
ax1.plot(x, days, 'moccasin')
ax1.set_ylabel('days', color='moccasin')

ax2 = ax1.twinx()
ax2.plot(x, probs, "turquoise")
ax2.set_ylabel('probabilities', color="turquoise")

ax3 = fig.add_subplot(111)
ax3.plot(x, zero , "--", color="moccasin")

ax4 = fig.add_subplot(111)
ax4.plot(x, pointfive , "--", color="turquoise")

fig.text(0.5, 0.04, 'days to lat, ha='center')


python - Matplotlib:如何为数据分配正确的y轴比例?-LMLPHP

这些让我想改变的事情:


弯曲的虚线应根据右侧的y轴比例绘制
应当添加通用的x标签和标题


这说明了我想要的更改:

python - Matplotlib:如何为数据分配正确的y轴比例?-LMLPHP

我怎么做?谢谢!

最佳答案

您已经定义了轴ax1(左)和ax2(右)。只需使用这些来选择要绘制的轴:

x = range(1, 16)
y1 = 10*np.random.random(size=len(x))
y2 = np.random.random(size=len(x))
zero = [0] * 15
pointfive = [0.5] * 15

fig = plt.figure()

ax1 = fig.add_subplot(111)
ax1.plot(x, y1, 'moccasin')
ax1.set_ylabel('days', color='moccasin')

ax2 = ax1.twinx()
ax2.plot(x, y2, "turquoise")
ax2.set_ylabel('probabilities', color="turquoise")


ax1.plot(x, zero , "--", color="moccasin")
ax2.plot(x, pointfive , "--", color="turquoise")

ax1.set_xlabel('days to lat')

ax1.set_title('TITLE')

关于python - Matplotlib:如何为数据分配正确的y轴比例?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57376398/

10-13 03:46