如何在 Matplotlib 中旋转 X 轴刻度标签文本
在本教程文章中,我们将介绍在 Python 标签中旋转 X 轴刻度标签文本的不同方法。这包括,
plt.xticks(rotation= )
fig.autofmt_xdate(rotation= )
ax.set_xticklabels(xlabels, rotation= )
plt.setp(ax.get_xticklabels(), rotation=)
ax.tick_params(axis='x', labelrotation= )
刻度标签文本在 X
轴上的默认方向是水平或 0
度。如果刻度标签文本过长(例如相邻标签文本之间重叠),则会带来不便。
创建上图的代码是,
from matplotlib import pyplot as plt
from datetime import datetime, timedelta
values = range(10)
dates = [datetime.now()-timedelta(days=_) for _ in range(10)]
fig,ax = plt.subplots()
plt.plot(dates, values)
plt.grid(True)
plt.show()
plt.xticks(rotation= )
旋转 Xticks 标签文本
from matplotlib import pyplot as plt
from datetime import datetime, timedelta
values = range(10)
dates = [datetime.now()-timedelta(days=_) for _ in range(10)]
fig,ax = plt.subplots()
plt.plot(dates, values)
plt.xticks(rotation=45)
plt.grid(True)
plt.show()
plt.xticks(rotation=45)
plt.xticks
获取或设置刻度位置和 x 轴标签的属性。
rotation
是 x 轴标签文本的逆时针旋转角度。
fig.autofmt_xdate(rotation= )
旋转 Xticks 标签文本
from matplotlib import pyplot as plt
from datetime import datetime, timedelta
values = range(10)
dates = [datetime.now()-timedelta(days=_) for _ in range(10)]
fig,ax = plt.subplots()
plt.plot(dates, values)
fig.autofmt_xdate(rotation=45)
plt.grid(True)
plt.show()
autofmt_xdate
旋转刻度线标签,如果标签是 date
对象,则自动将其右对齐。
但是它实际上不仅适用于 date
对象,而且适用于普通标签文本字符串。
from matplotlib import pyplot as plt
from datetime import datetime, timedelta
values = range(10)
dates = [datetime.now()-timedelta(days=_) for _ in range(10)]
xlabels = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
fig,ax = plt.subplots()
plt.plot(dates, values)
ax.set_xticklabels(xlabels)
fig.autofmt_xdate(rotation=45)
plt.grid(True)
plt.show()
ax.set_xticklabels(xlabels, rotation= )
旋转 Xticks 标签文本
set_xticklabels
设置带有字符串标签列表的 x-tick
标签。
字符串标签列表可以是新指定的列表,也可以是当前图的现有标签列表,由 get_xticklabels()
来读取。
from matplotlib import pyplot as plt
from datetime import datetime, timedelta
values = range(10)
dates = [datetime.now()-timedelta(days=_) for _ in range(10)]
xlabels = [datetime.strftime(datetime.now()-timedelta(days=_), "%m/%d/%Y") for _ in range(10)]
fig,ax = plt.subplots()
plt.plot(dates, values)
ax.set_xticklabels(xlabels, rotation=45, ha='right')
plt.grid(True)
plt.show()
plt.setp(ax.get_xticklabels(), rotation=)
旋转 Xticks 标签文本
matplotlib.pyplot.setp
设置 artist
对象的属性。
plt.setp(ax.get_xticklabels(), rotation=)
设置 xtick 标签对象的 rotation
属性。
from matplotlib import pyplot as plt
from datetime import datetime, timedelta
values = range(10)
dates = [datetime.now()-timedelta(days=_) for _ in range(10)]
xlabels = [datetime.strftime(datetime.now()-timedelta(days=_), "%m/%d/%Y") for _ in range(10)]
fig,ax = plt.subplots()
plt.plot(dates, values)
plt.setp(ax.get_xticklabels(), rotation=45, ha='right')
plt.grid(True)
plt.tight_layout()
plt.show()
ax.tick_params(axis='x', labelrotation= )
旋转 Xticks 标签文本
tick_params
设置刻度线、刻度线标签和网格线的参数。
ax.tick_params(axis='x', labelrotation= )
设置刻度标签在 x
轴上的 labelrotation
属性。
from matplotlib import pyplot as plt
from datetime import datetime, timedelta
values = range(10)
dates = [datetime.now()-timedelta(days=_) for _ in range(10)]
xlabels = [datetime.strftime(datetime.now()-timedelta(days=_), "%m/%d/%Y") for _ in range(10)]
fig,ax = plt.subplots()
plt.plot(dates, values)
ax.tick_params(axis='x', labelrotation=45)
plt.grid(True)
plt.show()
旋转 xticklabels
对齐
在上面的示例代码中,我们使用参数 ha='right'
,该的意思是ħ orizontal alignment 为 right
。
ha='right'
将标签文本的右端与刻度对齐。
ha='left'
将标签文本的左端与刻度对齐。
ha='center'
使标签文本的中心与刻度线对齐。
from matplotlib import pyplot as plt
from datetime import datetime, timedelta
xvalues = range(5)
yvalues = xvalues
xlabels = [datetime.strftime(datetime.now()-timedelta(days=_), "%m/%d/%Y") for _ in xvalues]
alignment = ["right", "left", "center"]
fig,axes = plt.subplots(1, 3)
for n, ax in enumerate(axes):
ax.plot(xvalues, yvalues)
ax.set_title(alignment[n] + "alignment")
ax.set_xticks(xvalues)
ax.set_xticklabels(xlabels, rotation=45, ha=alignment[n])
ax.grid(True)
plt.show()
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串