【问题标题】:Axis break where one side has no data plotted? (near zero)一侧没有绘制数据的轴中断? (接近于零)
【发布时间】:2017-07-20 20:04:14
【问题描述】:

问题

我有一个包含多个变量的线图,其中数据值远高于零。该图还有两个单独的 y 轴,分别对应于实线和虚线。我已经注意到 y 轴标签中的数据值,但我的导师认为我还应该在零上方添加一个轴中断,以提​​请注意这些值远高于零的事实,并表明,例如,实心 CTRL不是固体 MFKC 的两倍。

我已经使用 matplotlib 查找了轴中断的示例,但我所看到的所有示例都涉及跨越图的两个“边”的数据,并且两半使用单独的轴。我没有任何想要在中断下方绘制的数据,向已经有两个坐标轴的绘图添加更多轴可能会不必要地复杂。

代码

#Plot
x= np.arange(0,3)
fig,ax = plt.subplots(figsize=(6,6))

#DOD
dod1 = ax.plot(x, ctrl_dod,  color='r',      label='CTRL'  )
dod2 = ax.plot(x, mfkc_dod,  color='orange', label='MFKC'  )
dod3 = ax.plot(x, gses_dod,  color='green',  label='GSES'  )
dod4 = ax.plot(x, gses3_dod, color='blue',   label='GSES-3')
dod5 = ax.plot(x, gses4_dod, color='purple', label='GSES-4')
ax.yaxis.grid(True, linestyle='-')
ax.xaxis.grid(True)
ax.set_xlabel('Month')
ax.set_ylabel('Dust Optical Depth')
ax.legend()
ax.set_title('Global Mean DOD and DCM')

#DCM
ax2  = ax.twinx()
dcm1 = ax2.plot(x, ctrl_dcm*1e6,  color='r',       linestyle='--')
dcm2 = ax2.plot(x, mfkc_dcm*1e6,  color='#e68a00', linestyle='--')
dcm3 = ax2.plot(x, gses_dcm*1e6,  color='green',   linestyle='--')
dcm4 = ax2.plot(x, gses3_dcm*1e6, color='blue',    linestyle='--')
dcm5 = ax2.plot(x, gses4_dcm*1e6, color='purple',  linestyle='--')
ax2.yaxis.grid(True)
ax2.xaxis.grid(True)
ax2.set_xlabel('Month')
ax2.set_ylabel('Dust Column Mass (mg m-2)')
#Limits
axes = plt.gca()
axes.set_xlim([-0.25,2.25])

#Labels
axes.set_xticks(x)
axes.set_xticklabels(['June','July','August'])

#Save
pylab.savefig('dod+dcm.png')

问题

如果我将 y 轴范围更改为都从零开始,我如何在两侧添加一个刚好高于零(低于 0.020 和 40)的换行符?

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    创建一个所有值为 0.020 且形状与时间数组相同的 numpy 数组。根据时间绘制该数组并打开点连接(即绘制趋势线)。您可以对 40 执行相同的操作。然后只需将 y 下限设置为零。

    【讨论】:

    • 我想我不明白这是如何工作的。请您提供一个简单的例子好吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 2021-03-10
    • 2016-06-09
    • 2018-11-21
    相关资源
    最近更新 更多