📌  相关文章
📜  如何在 Matplotlib 中旋转 X 轴刻度标签文本?

📅  最后修改于: 2022-05-13 01:55:27.657000             🧑  作者: Mango

如何在 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 轴标签。

蟒蛇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 轴标签。

蟒蛇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 轴标签。

蟒蛇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()

输出 :