简体   繁体   English

如何旋转特定的刻度标签

[英]How to rotate specific tick labels

Use case: you have tick marks on the x axis.用例:您在 x 轴上有刻度线。 But some ticks are either too close or you need to otherwise emphasize them via rotation.但是有些刻度线要么太近,要么您需要通过旋转来强调它们。 So, for example, you might have a list of dates across the x axis that are always first of the month.因此,例如,您可能有一个横跨 x 轴的日期列表,这些日期始终是该月的第一天。 But then you want to 'call out' one date in particular, say your birthday, and rotate it, say, 25 degrees.但是你想特别“喊出”一个日期,比如你的生日,然后旋转 25 度。

How could you rotate that one 'tick' while NOT rotating all the others?你怎么能旋转那个“刻度”而不旋转所有其他的?

I know plt.xticks(rotation = 335) will rotate all the ticks nicely.我知道plt.xticks(rotation = 335)会很好地旋转所有刻度线。

But let's say you have:但是假设你有:

f, ax = plt.subplots(figsize=(16, 8))

ax.set_xticks((0,1455,5000,10000,15000,20000,25000,30000))

and you want to rotate 1455 by 335 degrees, while NOT rotating any of the others.并且您想将1455旋转 335 度,同时旋转其他任何一个。

How do you do it?你怎么做呢?

After you set the tick positions (and optionally also set their labels), you can access the matplotlib text elements via ax.get_xticklabels() .设置刻度位置(也可以选择设置它们的标签)后,您可以通过ax.get_xticklabels()访问 matplotlib 文本元素。 So, you can just access the second element in the list and change its rotation.因此,您只需访问列表中的第二个元素并更改其旋转。 (Similarly, you can eg change colors.) (同样,您可以更改 colors。)

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(16, 8))
ax.set_xticks((0, 1455, 5000, 10000, 15000, 20000, 25000, 30000))
ticks = ax.get_xticklabels()
ticks[1].set_rotation(335)
plt.show()

旋转一个刻度标签

This applies to pandas.DataFrame.plot , matplotlib , and seaborn .这适用于pandas.DataFrame.plot seaborn matplotlib

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM