every blog every motto: Just live your life cause we don’t live twice.
0. Preface
This article mainly summarizes the method of rotating the x-axis label
:
- When I checked other blogs, I found that some of the explanatory text near the x/y axis (meaning it represents) are called labels, and some call the text corresponding to the scale as labels.
- This article refers to the latter, but the author prefers to call the first type a label. I don’t know a reasonable name for the time being, so I use "label" instead.
1. Text
1.1 No rotation
1.1.1 Method 1:
import pandas as pd
import matplotlib.pyplot as plt
file_name = 'data/input/plot2.xlsx'
data = pd.read_excel(file_name, 'thumbs up')
print(data)
data['Date'] = data.Date.dt.strftime('%Y-%m-%d') # 改变时间类型
x = data['Date'].values.tolist()
y = data['sum of instructing'].values.tolist()
plt.plot(x, y)
plt.show()
We found that the scale texts corresponding to the x-axis are close together, as shown in the following figure:
1.1.2 Method two:
import pandas as pd
import matplotlib.pyplot as plt
file_name = 'data/input/plot2.xlsx'
data = pd.read_excel(file_name, 'thumbs up')
print(data)
data['Date'] = data.Date.dt.strftime('%Y-%m-%d') # 改变时间类型
x = data['Date'].values.tolist()
y = data['sum of instructing'].values.tolist()
fig,ax = plt.subplots()
ax.plot(x, y)
plt.show()
1.2 Rotation
1.2.1 Method 1: plt.xticks()
import pandas as pd
import matplotlib.pyplot as plt
file_name = 'data/input/plot2.xlsx'
data = pd.read_excel(file_name, 'thumbs up')
print(data)
data['Date'] = data.Date.dt.strftime('%Y-%m-%d') # 改变时间类型
x = data['Date'].values.tolist()
y = data['sum of instructing'].values.tolist()
plt.plot(x, y)
plt.xticks(rotation=90) # 旋转90度
plt.show()
The results are as follows:
1.2.2 Method 2: ax.set_xticklabels
import pandas as pd
import matplotlib.pyplot as plt
file_name = 'data/input/plot2.xlsx'
data = pd.read_excel(file_name, 'thumbs up')
print(data)
data['Date'] = data.Date.dt.strftime('%Y-%m-%d') # 改变时间类型
x = data['Date'].values.tolist()
y = data['sum of instructing'].values.tolist()
fig,ax = plt.subplots()
ax.plot(x, y)
ax.set_xticklabels(labels=x, rotation=90) # 旋转90度
plt.show()
result:
1.2.3 Method Three: tick.set_rotation()
import pandas as pd
import matplotlib.pyplot as plt
file_name = 'data/input/plot2.xlsx'
data = pd.read_excel(file_name, 'thumbs up')
print(data)
data['Date'] = data.Date.dt.strftime('%Y-%m-%d') # 改变时间类型
x = data['Date'].values.tolist()
y = data['sum of instructing'].values.tolist()
fig,ax = plt.subplots()
ax.plot(x, y)
# ax.set_xticklabels(labels=x, rotation=90) # 旋转90度
for tick in ax.get_xticklabels():
tick.set_rotation(90)
plt.show()
1.2.4 Method 4: pl.xticks() (attached)
Description:
- This method is applicable to the above two (not rotated in 1.1), that is, whether you use plt to draw directly, or create a fig object first.
- Use plt here, another method (ax) readers can verify by themselves
import pandas as pd
import matplotlib.pyplot as plt
import pylab as pl
file_name = 'data/input/plot2.xlsx'
data = pd.read_excel(file_name, 'thumbs up')
print(data)
data['Date'] = data.Date.dt.strftime('%Y-%m-%d') # 改变时间类型
x = data['Date'].values.tolist()
y = data['sum of instructing'].values.tolist()
plt.plot(x, y)
pl.xticks(rotation=90)
plt.show()
references
[1] https://blog.csdn.net/songrenqing/article/details/78927283
[2] https://ask.csdn.net/questions/769831
[3] https://blog.csdn.net/qq_15407209/article/details/86486480
[4] https://blog.csdn.net/changzoe/article/details/78851329
[5] https://blog.csdn.net/changzoe/article/details/78851329