在matplotlib中共享轴时显示刻度标签 [英] Show tick labels when sharing an axis in matplotlib

查看:133
本文介绍了在matplotlib中共享轴时显示刻度标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下功能:

def plot_variance_analysis(indices, stat_frames, legend_labels, shape):
    x = np.linspace(1, 5, 500)
    fig, axes = plt.subplots(shape[0], shape[1], sharex=True sharey=True)
    questions_and_axes = zip(indices, axes.ravel())
    frames_and_labels = zip(stat_frames, legend_labels)
    for qa in questions_and_axes:
        q = qa[0]
        ax = qa[1]
        for fl in frames_and_labels:
            frame = fl[0]
            label = fl[1]
            ax.plot(x, stats.norm.pdf(x, frame['mean'][q], frame['std'][q]), label=label)
            ax.set_xlabel(q)
            ax.legend(loc='best')
    plt.xticks([1,2,3,4,5])
    return fig, axes

以下是我自己的一些示例数据:

Here's what I get with some of my own sample data:

我试图保持轴之间的共享状态,但同时在 all 子图(包括前两个)上显示x轴的刻度标签.我在文档中找不到关闭此功能的任何方法.有什么建议?还是我应该逐个轴设置x刻度标签?

I'm trying to maintain the shared state between axes, but at the same time display the tick labels for the x axis on all subplots (including the top two). I can't find any means to turn this off in the documentation. Any suggestions? Or should I just set the x tick labels axis by axis?

如果这很重要,我正在运行matplotlib 1.4.0.

I'm running matplotlib 1.4.0, if that's important.

推荐答案

缺少的刻度已将其visible属性设置为False.在plt.subplot的文档中指出了这一点.解决此问题的最简单方法可能是:

The ticks that are missing have had their visible property set to False. This is pointed out in the documentation for plt.subplot. The simplest way to fix this is probably to do:

for ax in axes.flatten():
    for tk in ax.get_yticklabels():
        tk.set_visible(True)
    for tk in ax.get_xticklabels():
        tk.set_visible(True)

在这里,我遍历了所有轴,您不必这样做,但是这种方式的代码更简单.如果您愿意,也可以在丑陋的衬里中使用列表推导来做到这一点:

Here I've looped over all axes, which you don't necessarily need to do, but the code is simpler this way. You could also do this with list comprehensions in an ugly one liner if you like:

[([tk.set_visible(True) for tk in ax.get_yticklabels()], [tk.set_visible(True) for tk in ax.get_yticklabels()]) for ax in axes.flatten()]

这篇关于在matplotlib中共享轴时显示刻度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆