[英]Rotate x axis labels in Matplotlib parasite plot
After Thomas very helpfully fixed my issues making two parasite sub plots in this question , I'm now trying to rotate the x axis labels on the subplots. 在Thomas非常有帮助地解决了我在此问题中制作两个寄生子图的问题之后 ,我现在尝试旋转子图上的x轴标签。
Unfortunately, my modification to the example code here seems to have no effect on the x axis labels: 不幸的是,我对此处示例代码的修改似乎对x轴标签没有影响:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
host = host_subplot(111, axes_class=AA.Axes)
plt.subplots_adjust(right=0.75)
par1 = host.twinx()
par2 = host.twinx()
offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right",
axes=par2,
offset=(offset, 0))
par1.axis["right"].toggle(all=True)
par2.axis["right"].toggle(all=True)
host.set_xlim(0, 2)
host.set_ylim(0, 2)
host.set_xlabel("Distance")
host.set_ylabel("Density")
par1.set_ylabel("Temperature")
par2.set_ylabel("Velocity")
p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")
par1.set_ylim(0, 4)
par2.set_ylim(1, 65)
host.legend()
host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
par2.axis["right"].label.set_color(p3.get_color())
plt.xticks(rotation = 45) #<- The only change from the example
plt.draw()
plt.show()
gives un-rotated x axis labels in: 在以下位置给出未旋转的x轴标签:
Although I've shown the plt.xticks(rotation = 45)
, I've also tried other ways that work with "conventional" matplotlib plots without success. 尽管我已经展示了
plt.xticks(rotation = 45)
,但我也尝试了其他与“常规” matplotlib绘图一起使用的方法,但plt.xticks(rotation = 45)
成功。 Does anyone know if there is a way to do this, or am I just dealing with too much of a niche case? 有谁知道是否有办法做到这一点,还是我只是在处理过多的小众案件? Maybe I should just figure out a way to live with using sub plots and no parasite axes?
也许我应该找出一种使用子图且没有寄生轴的方法?
Thanks a lot, Alex 非常感谢Alex
There are two ways to produce parasite axes: 有两种产生寄生虫轴的方法:
Using the mpl_toolkits
axisartist
and axes_grid1
package, examples are 使用
mpl_toolkits
axisartist
和axes_grid1
包,示例如下:
Using usual subplots and their spines. 使用通常的子图和他们的刺。 Examples are
例子是
Here you are using the first approach, which may be a bit unintuitive due to it using the special axes provided by the axisartist
toolkit. 在这里,您使用的是第一种方法,由于使用
axisartist
工具包提供的特殊轴,因此可能有点不直观。
Use the usual subplots approach for which all the usual ways of rotating ticklabels work just fine, eg 使用通常的子图方法,所有常规的刻度线旋转方法都可以正常工作,例如
plt.setp(ax.get_xticklabels(), rotation=90)
In case you want to stick with the mpl_toolkits
approach you need to obtain the ticklabels from the axis
via axis["right"].major_ticklabels
, 如果您要坚持使用
mpl_toolkits
方法,则需要通过axis["right"].major_ticklabels
从axis
获取刻度axis["right"].major_ticklabels
,
plt.setp(par2.axis["bottom"].major_ticklabels, rotation=-135)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.