如何在 Matplotlib 中旋转 X 轴刻度标签文本?
Matplotlib 是一个了不起的,也是Python中使用最广泛的数据可视化库之一,用于绘制数组。它是一个基于 NumPy 数组的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈一起使用。它非常受欢迎,因为它具有自定义选项,因为我们可以从其对象层次结构中调整任何元素。
旋转 X 轴标签
要旋转 X 轴标签,matplotlib 提供了多种方法,即在图形级别更改它或通过在轴级别更改它或使用内置函数单独更改它。下面列出了一些方法:
让我们创建一个简单的线图,我们将在进一步的示例中对其进行修改:
Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Creating dataset
x = np.arange(0, np.pi*2, 0.05)
y = np.sin(x**2)
# Creating plot
plt.plot(x, y)
# Setting title
plt.title('Simple Example')
# Show plot
plt.show()
Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Creating dataset
x = np.arange(0, np.pi*2, 0.05)
y = np.sin(x**2)
# Creating plot
plt.plot(x, y)
# Rotating X-axis labels
plt.xticks(rotation = 25)
# Setting title
plt.title('Rotating using plt.xticks()')
# Show plot
plt.show()
Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Creating dataset
x = np.arange(0, np.pi*2, 0.05)
y = np.sin(x**2)
# Creating Figure
fig, ax = plt.subplots()
# Creating plot
ax.plot(x, y)
# Rotating X-axis labels
ax.set_xticklabels(ax.get_xticks(), rotation = 50)
# Setting title
plt.title('Rotating using tick.set_rotation()')
# Show plot
plt.show()
Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Creating dataset
x = np.arange(0, np.pi*2, 0.05)
y = np.sin(x**2)
# Creating Figure
fig, ax = plt.subplots()
# Creating plot
ax.plot(x, y)
# Rotating X-axis labels
for tick in ax.get_xticklabels():
tick.set_rotation(75)
# Setting title
plt.title('Rotating using ax.set_xticklabels()')
# Show plot
plt.show()
Python3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Creating dataset
x = np.arange(0, np.pi*2, 0.05)
y = np.sin(x**2)
# Creating Figure
fig, ax = plt.subplots()
# Creating plot
ax.plot(x, y)
# Rotating X-axis labels
ax.tick_params(axis='x', labelrotation = 100)
# Setting title
plt.title('Rotating using ax.xtick_params()')
# Show plot
plt.show()
输出 :
示例 1:在本示例中,我们将使用plt.xticks() 在图形级别上旋转 X 轴标签。
Syntax: matplotlib.pyplot.xticks(ticks=None, labels=None, **kwargs)
Parameters: This method accept the following parameters that are described below:
- ticks: This parameter is the list of xtick locations. and an optional parameter. If an empty list is passed as an argument then it will removes all xticks
- labels: This parameter contains labels to place at the given ticks locations. And it is an optional parameter.
- **kwargs: This parameter is Text properties that is used to control the appearance of the labels.
蟒蛇3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Creating dataset
x = np.arange(0, np.pi*2, 0.05)
y = np.sin(x**2)
# Creating plot
plt.plot(x, y)
# Rotating X-axis labels
plt.xticks(rotation = 25)
# Setting title
plt.title('Rotating using plt.xticks()')
# Show plot
plt.show()
输出 :
示例 2:在此示例中,我们将使用tick.set_rotation() 在轴级别上旋转 X 轴标签。
Syntax: Axes.get_xticks(self, minor=False)
Parameters: This method accepts the following parameters.
- minor : This parameter is used whether set major ticks or to set minor ticks
Return value: This method returns a list of Text values.
蟒蛇3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Creating dataset
x = np.arange(0, np.pi*2, 0.05)
y = np.sin(x**2)
# Creating Figure
fig, ax = plt.subplots()
# Creating plot
ax.plot(x, y)
# Rotating X-axis labels
ax.set_xticklabels(ax.get_xticks(), rotation = 50)
# Setting title
plt.title('Rotating using tick.set_rotation()')
# Show plot
plt.show()
输出 :
示例 3:在本示例中,我们将使用内置的ax.set_xticklabels()函数单独旋转 X 轴标签。
蟒蛇3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Creating dataset
x = np.arange(0, np.pi*2, 0.05)
y = np.sin(x**2)
# Creating Figure
fig, ax = plt.subplots()
# Creating plot
ax.plot(x, y)
# Rotating X-axis labels
for tick in ax.get_xticklabels():
tick.set_rotation(75)
# Setting title
plt.title('Rotating using ax.set_xticklabels()')
# Show plot
plt.show()
输出 :
示例 4:在此示例中,我们将使用内置的ax.xtick_params()函数单独旋转 X 轴标签。
Syntax: matplotlib.pyplot.tick_params(axis=’both’, **kwargs)
蟒蛇3
# Import libraries
import matplotlib.pyplot as plt
import numpy as np
# Creating dataset
x = np.arange(0, np.pi*2, 0.05)
y = np.sin(x**2)
# Creating Figure
fig, ax = plt.subplots()
# Creating plot
ax.plot(x, y)
# Rotating X-axis labels
ax.tick_params(axis='x', labelrotation = 100)
# Setting title
plt.title('Rotating using ax.xtick_params()')
# Show plot
plt.show()
输出 :