Matplotlib:xticks 标签未显示 [英] Matplotlib: xticks labels not showing

查看:36
本文介绍了Matplotlib:xticks 标签未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是matplotlib的新手,但遇到一个小问题.我正在尝试制作 3 个相互堆叠的图,共享 x 轴和 2 个不同的 y 轴.这是我当前的代码

I'm new to matplotlib and I'm having a small problem. I'm trying to make 3 plots stacked on top of each other, sharing the x axis and with 2 different y axis. This is my current code

import numpy as np
import matplotlib.pyplot as plt

periods = range(1,13)
n0 = [4.2, 6.7, 10.6, 51.3, 339, 45.6, 56.3, 112.9, 182.7, 185.7, 126.2, 25.39]
alp = [2.12, 2.14, 2.19, 2.35, 2.54, 2.33, 2.34, 2.43, 2.45, 2.46, 2.466, 2.249]
B = [0.045, 0.041, 0.04, 0.04, 0.057, 0.048, 0.044, 0.057, 0.054, 0.065, 0.06, 0.045]
emin = [166, 201.9, 215, 270.7, 351.8, 263.7, 302.2, 323.6, 328.7, 346.1, 279.5, 259.8]
emax = [21806, 28407, 5706, 22087, 17978, 11699, 19440, 17988, 26938, 14812, 14195, 26121]
eq = [7.8, 11.8, 13.3, 15.2, 8.87, 10.5, 13.8, 7.6, 11.5, 7.4, 6.4, 13.5]

f, (ax1, ax2, ax3) = plt.subplots(3, sharex = True)
ax1.scatter(periods, emin, c="k")
ax1.set_ylabel(r"$E_{min}")
ax1.yaxis.set_ticks(range(160, 380, 40))

ax4 = ax1.twinx()
ax4.scatter(periods, n0, c="r")
ax4.set_ylabel(r"$N_0$", color = 'r')
ax4.tick_params(colors = 'r')
ax4.yaxis.set_ticks(np.arange(20, 340, 50))

ax2.scatter(periods, emax, c="k")
ax2.set_ylabel(r"$E_{max}$")
ax2.yaxis.set_ticks(np.arange(5000, 30000, 5000))
ax5 = ax2.twinx()
ax5.scatter(periods, alpha, c="r")
ax5.set_ylabel("alp", color = 'r')
ax5.tick_params(colors = 'r')
ax5.yaxis.set_ticks(np.arange(2.1, 2.6, 0.1))

ax3.scatter(periods, eq, c="k")
ax3.set_ylabel("Eq")
ax3.yaxis.set_ticks(np.arange(6, 15, 2))
ax3.set_xlabel("Periods")
ax6 = ax3.twinx()
ax6.scatter(periods, B, c="r")
ax6.set_ylabel("B", color = 'r')
ax6.tick_params(colors = 'r')
ax6.yaxis.set_ticks(np.arange(0.02, 0.09, 0.02))
ax6.xaxis.set_ticks(range(1,13))

f.subplots_adjust(hspace = 0)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
plt.show()

但是没有显示 x 轴标签.标签本身("Periods")很好,但刻度线不是.我试图改变线路

But the x axis labels are not being shown. The label itself ("Periods") is fine, but the ticks aren't. I've tried to change the line

ax6.xaxis.set_ticks(range(1,13))

到其他所有号码,但仍然不起作用.有什么帮助吗?附带说明一下,如何将y轴标签旋转90度以使其处于水平状态?我试过在 ylabel 中包含 rotation = 90 但它没有改变(尽管其他值有效).

to every other number, but still doesn't work. Any help? On a side note, how to rotate the y axis labels by 90 degrees so that they are horizontal? I've tried including rotation = 90 in the ylabel but it doesn't change (though other values work).

非常感谢

忘记添加了,如果我删除了 twinx 轴它工作正常

Forgot to add, if I erase the twinx axes it works fine

推荐答案

为使x刻度出现,您需要将倒数第二行更改为最后一行并设置 visible = True .要将 y 标签旋转 90 度,请按您的方式使用 rotation =,但将旋转设置为 0.这样做可能会使您的标签和刻度重叠.您可以通过使用 ax.set_ylabel()中的 labelpad = 删除它.这是轴和标签之间的间距.

In order for your x ticks to appear you need to change the second to last line and set visible = True. In order to rotate the y labels by 90 degrees, use rotation = as you are doing, but set the rotation to 0. Doing this may make your labels and your ticks overlap. You can remove this by using the labelpad = in your ax.set_ylabel(). This is the spacing between the axis and the label.

f, (ax1, ax2, ax3) = plt.subplots(3, sharex = True)
ax1.scatter(periods, emin, c="k")
ax1.set_ylabel(r"$E_{min}$",rotation=0,labelpad=20)
ax1.yaxis.set_ticks(range(160, 380, 40))

ax4 = ax1.twinx()
ax4.scatter(periods, n0, c="r")
ax4.set_ylabel(r"$N_0$", color = 'r',rotation=0,labelpad=10)
ax4.tick_params(colors = 'r')
ax4.yaxis.set_ticks(np.arange(20, 340, 50))

ax2.scatter(periods, emax, c="k")
ax2.set_ylabel(r"$E_{max}$",rotation=0,labelpad=20)
ax2.yaxis.set_ticks(np.arange(5000, 30000, 5000))
ax5 = ax2.twinx()
ax5.scatter(periods, alp, c="r")
ax5.set_ylabel("alp", color = 'r',rotation=0,labelpad=15)
ax5.tick_params(colors = 'r')
ax5.yaxis.set_ticks(np.arange(2.1, 2.6, 0.1))

ax3.scatter(periods, eq, c="k")
ax3.set_ylabel("Eq",rotation=0,labelpad=20)
ax3.yaxis.set_ticks(np.arange(6, 15, 2))
ax3.set_xlabel("Periods")
ax6 = ax3.twinx()
ax6.scatter(periods, B, c="r")
ax6.set_ylabel("B", color = 'r',rotation=0,labelpad=10)
ax6.tick_params(colors = 'r')
ax6.yaxis.set_ticks(np.arange(0.02, 0.09, 0.02))
ax1.xaxis.set_ticks(np.arange(1, 13, 1))


f.subplots_adjust(hspace = 0,left=0.14,right=0.90)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=True)
plt.show()

这将产生以下情节:

您可能想要使用 labelpad 进行试验,以便将标签准确放置在您想要的位置,因为每个轴的刻度标签长度都不相同.

You may want to experiment with the labelpad to get the labels exactly where you want them as the length of your tick labels are not the same for each axis.

这篇关于Matplotlib:xticks 标签未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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