【问题标题】:Show Y Axis Grid Lines In Plot [duplicate]在绘图中显示 Y 轴网格线
【发布时间】:2016-03-13 23:42:18
【问题描述】:

我正在尝试重新创建这个:

我的代码差不多了:

def make_bar(g_title, y_title, x_labels, data_series, file_name, cat_title,
             x_title):
    rcParams['figure.figsize'] = 6.4125, 3.6
    n_groups = 13
    bar_width = 0.35
    opacity = 0.4
    fig, ax = plt.subplots()
    fig.subplots_adjust(right=1.5) # adjust 0.8 for your needs.
    index = np.arange(n_groups)
    plt.bar(index, tuple(data_series[0][1]), bar_width,
                     alpha=opacity,
                     color='b',
                     label='{}'.format(data_series[0][0]))
    plt.bar(index + bar_width, tuple(data_series[1][1]), bar_width,
                     alpha=opacity,
                     color='r',
                     label='{}'.format(data_series[1][0]))
    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width * 0.6, box.height])
    plt.xlabel(x_title, fontsize=10)
    plt.ylabel(y_title, fontsize=10)
    plt.title(g_title, fontsize=11)
    plt.xticks(index + bar_width, tuple(x_labels), fontsize=8)
    plt.yticks(fontsize=8)
    plt.axis('tight')
    lgd = plt.legend(fontsize=8, bbox_to_anchor=(1.15, 0.5))
    plt.tight_layout()
    plt.savefig('{}/{}.png'.format(images, file_name), dpi=100, format='png',
                bbox_extra_artists=(lgd,), bbox_inches='tight')
    plt.close('all')
    return

输出:

我需要像原版一样的 y 轴网格线,但不知道如何到达那里。我已经用尽了所有的想法。帮忙?

【问题讨论】:

标签: python matplotlib


【解决方案1】:

您正在寻找plt.grid。但是,由于您只想要 Y 轴网格,因此您需要指定 axis 关键字,所以它看起来像这样:

plt.grid(color='black', which='major', axis='y', linestyle='solid')

See here for more info.

【讨论】:

  • 这让我明白了,但它们不像我的例子那样落后。 i.imgur.com/U69O4su.png我怎么能做到这一点?我试过 [line.set_zorder(3) for line in ax.lines] 没有运气。
  • 还尝试将 zorder 添加到您的代码中。累了所有 3 没有运气
  • 我一直在尝试回答这个问题,但目前我在访问 matplotlib 文档时遇到了问题,而且我所知道的解决方案都不适合我。另一件事您可以尝试设置ax.set_axisbelow(True)。出于某种原因,两者似乎都不适合我,但也许你会有更好的运气。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多