【问题标题】:Matplotlib multiple grids zorder not working with errorbarsMatplotlib 多个网格 zorder 不使用错误栏
【发布时间】:2017-04-11 17:10:00
【问题描述】:

当将具有不同 y 轴的多个误差线绘制到一个子图中时,我目前正在处理 matplotlib 的一些奇怪行为。

这样做时,对应于第二个误差线图的网格始终与第一个误差线图重叠。使用zorder 选项,我只能将另一个网格移动到错误栏下方。

我希望两个网格都低于两个误差线。 有谁知道这个问题的解决方案,或者这是 matplotlib 中的一个错误?

我正在使用 Python 2.7.12 和 matplotlib 1.5.1。

最小的工作示例:

import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.colors as colors

# numbers of epochs
ttimes_means_list = [251.4, 153.3, 22.0, 202.1, 46.6]
ttimes_stddevs_list = [32.1, 35.1, 12.0, 84.9, 14.7]
# numbers of times
times_means_list = [5231, 3167, 860, 3932, 1244]
times_stddevs_list = [1381, 572, 253, 1445, 215]

labels = ['x1', 'x2', 'x3', 'x4', 'x5']
linewidth=3

xvalues = np.arange(0, len(times_means_list), 1)
xvalues_s = xvalues + 0.1

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.set_ylabel('Number')
ax1.xaxis.grid(False)
ax1.yaxis.grid(True, color='navy', linewidth=linewidth/2.0)
ax2.xaxis.grid(False)
ax1.set_zorder(0)
ax2.set_zorder(0)
ax2.yaxis.grid(True, color='darkorange', linewidth=linewidth/2.0)
ax2.set_ylabel('Time')
ax1.set_xticks(xvalues)
ax2.set_xticks(xvalues)
ax1.set_xticklabels(labels)
ax2.set_xticklabels(labels)
ax1.set_xlabel('x label')
errplot1 = ax1.errorbar(xvalues, ttimes_means_list, yerr=ttimes_stddevs_list,
        linestyle="None", elinewidth=linewidth, color='navy', label='Number',
        capthick=linewidth, zorder=10)
errplot2 = ax2.errorbar(xvalues_s, times_means_list, yerr=times_stddevs_list, 
        linestyle="None", elinewidth=linewidth, color='darkorange',
        label='Time', capthick=linewidth, zorder=10)
ax1.set_axisbelow(True)
ax2.set_axisbelow(True)
fig.legend((errplot1, errplot2), ('number', 'time'), 
        loc='upper right', numpoints=1)
plt.xlim(-0.5, len(times_means_list)-0.5)
plt.title('Some plot title')
plt.savefig('mwe_plot.pdf')
plt.clf()

输出图(橙色网格点与蓝色条重叠):

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    也许你可以画两次“海军”线。

    import numpy as np
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    import matplotlib.colors as colors
    
    # numbers of epochs
    ttimes_means_list = [251.4, 153.3, 22.0, 202.1, 46.6]
    ttimes_stddevs_list = [32.1, 35.1, 12.0, 84.9, 14.7]
    # numbers of times
    times_means_list = [5231, 3167, 860, 3932, 1244]
    times_stddevs_list = [1381, 572, 253, 1445, 215]
    
    labels = ['x1', 'x2', 'x3', 'x4', 'x5']
    linewidth=3
    
    xvalues = np.arange(0, len(times_means_list), 1)
    xvalues_s = xvalues + 0.1
    
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax2 = ax1.twinx()
    ax1.set_ylabel('Number')
    ax1.xaxis.grid(False)
    g1 = ax1.yaxis.grid(True, color='navy', linewidth=linewidth/2.0)
    ax2.xaxis.grid(False)
    ax1.set_zorder(0)
    ax2.set_zorder(0)
    g2 = ax2.yaxis.grid(True, color='darkorange', linewidth=linewidth/2.0)
    ax2.set_ylabel('Time')
    ax1.set_xticks(xvalues)
    ax2.set_xticks(xvalues)
    ax1.set_xticklabels(labels)
    ax2.set_xticklabels(labels)
    ax1.set_xlabel('x label')
    ax1.errorbar(xvalues, ttimes_means_list, yerr=ttimes_stddevs_list,
            linestyle="None", elinewidth=1, color='navy', label='Number',
            capthick=1, zorder=10,)
    errplot2 = ax2.errorbar(xvalues_s, times_means_list, yerr=times_stddevs_list, 
            linestyle="None", elinewidth=linewidth, color='darkorange',
            label='Time', capthick=linewidth, zorder=10)
    ax1.set_axisbelow(True)
    ax2.set_axisbelow(True)
    
    # draw the 'navy' line again
    ax3 = ax1.twinx()
    ax3.yaxis.grid(False)
    errplot1 = ax3.errorbar(xvalues, ttimes_means_list, yerr=ttimes_stddevs_list,
            linestyle="None", elinewidth=linewidth, color='navy', label='Number',
            capthick=linewidth, zorder=10)
    
    fig.legend((errplot1, errplot2), ('number', 'time'), 
            loc='upper right', numpoints=1)
    plt.xlim(-0.5, len(times_means_list)-0.5)
    plt.title('Some plot title')
    # plt.savefig('mwe_plot.pdf')
    # plt.clf()
    
    plt.show()
    

    你会得到:

    【讨论】:

    • 非常感谢!确实,这解决了问题。我刚刚添加了ax3.get_yaxis().set_visible(False),以便摆脱右侧的附加标签。
    • @ml4294 感谢您的改进!
    猜你喜欢
    • 2015-10-08
    • 1970-01-01
    • 2013-10-19
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 2017-08-09
    • 1970-01-01
    相关资源
    最近更新 更多