如何使用 pyplot 增加 Python 绘图中的时间标签间距

问题描述 投票:0回答:1

请问如何间隔 x 轴标签。我的“ts”和“temp”源自之前脚本中的其他数据帧。 ts 是从 Print(df?) 复制并粘贴的,它的 dtype 是“datetime64[ns]”。我无法转换 ts 和 temp 以便能够在此脚本中绘制。

import numpy as np
import pandas as pd
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

#dtype: datetime64[ns]  in my original code
ts = ['2023-09-03 00:30:00', '2023-09-03 00:40:00', '2023-09-03 01:30:00', '2023-09-03 01:40:00', '2023-09-03 02:30:00', '2023-09-03 02:40:00', '2023-09-03 03:30:00' '2023-09-03 03:40:00', '2023-09-03 04:30:00', '2023-09-03 04:40:00', '2023-09-03 05:30:00', '2023-09-03 05:40:00', '2023-09-03 06:30:00', '2023-09-03 06:40:00', '2023-09-03 07:30:00', '2023-09-03 07:40:00', '2023-09-03 08:30:00', '2023-09-03 08:40:00', '2023-09-03 09:30:00', '2023-09-03 09:40:00', '2023-09-03 10:30:00', '2023-09-03 10:40:00', '2023-09-03 11:30:00', '2023-09-03 11:40:00', '2023-09-03 12:30:00', '2023-09-03 12:40:00', '2023-09-03 13:30:00', '2023-09-03 13:40:00', '2023-09-03 14:30:00', '2023-09-03 14:40:00', '2023-09-03 15:30:00', '2023-09-03 15:40:00', '2023-09-03 16:30:00', '2023-09-03 16:40:00', '2023-09-03 17:30:00', '2023-09-03 17:40:00', '2023-09-03 18:30:00', '2023-09-03 18:40:00', '2023-09-03 19:30:00', '2023-09-03 19:40:00', '2023-09-03 20:30:00', '2023-09-03 20:40:00', '2023-09-03 21:30:00', '2023-09-03 21:40:00', '2023-09-03 22:30:00', '2023-09-03 22:40:00', '2023-09-03 23:30:00', '2023-09-03 23:40:00']

# temp dtype: object
temp = ['-0.310364', '0.649189', '-0.297809', '0.386841', '-0.382441','0.288227', '-0.170187', '0.327015', '-0.122558', '0.240469', '-0.13553',
        '0.235048', '-0.552962', '0.290034', '-0.904663', '1.573051', '-0.502816', '-0.675617', '-0.741736', '2.277356', '-1.08424', '0.230401',
        '-2.013976', '0.886853', '-1.100503', '0.853258', '-0.425564', '1.454237', '-0.46393', '1.642925', '0.312705', '2.059798', '-0.65732',
        '0.42963', '-0.780653', '2.755777', '-1.211638', '1.43835', '-1.195375', '0.449282', '-1.032738', '0.583155', '-0.990273', '0.142854',
        '-0.62581', '0.369997', '-0.506108', '0.466836'] 
# creating the bar plot
df = pd.DataFrame(ts, temp)
df1 = df.reset_index()
ts = df1['ts']
temp = df1['temp']

fig = plt.figure(figsize =(8, 5)) # width:8, height:5
plt.bar(ts, temp, align='center', width = 0.005)
plt.xticks(ts, rotation=90)
plt.title('mins')
plt.savefig('Test_BarPlot.png')
plt.show()

我的输出如下:

df:                 temp
ts
2023-09-03 00:30:00 -0.310364  
2023-09-03 00:40:00  0.649189  
2023-09-03 01:30:00 -0.297809  
2023-09-03 01:40:00  0.386841  
2023-09-03 02:30:00 -0.382441  
2023-09-03 02:40:00  0.288227  
2023-09-03 03:30:00 -0.170187  
2023-09-03 03:40:00  0.327015  
2023-09-03 04:30:00 -0.122558  
2023-09-03 04:40:00  0.240469  
2023-09-03 05:30:00  -0.13553  
2023-09-03 05:40:00  0.235048  
2023-09-03 06:30:00 -0.552962  
2023-09-03 06:40:00  0.290034  
2023-09-03 07:30:00 -0.904663  
2023-09-03 07:40:00  1.573051  
2023-09-03 08:30:00 -0.502816  
2023-09-03 08:40:00 -0.675617  
2023-09-03 09:30:00 -0.741736  
2023-09-03 09:40:00  2.277356  
2023-09-03 10:30:00  -1.08424  
2023-09-03 10:40:00  0.230401  
2023-09-03 11:30:00 -2.013976  
2023-09-03 11:40:00  0.886853  
2023-09-03 12:30:00 -1.100503  
2023-09-03 12:40:00  0.853258  
2023-09-03 13:30:00 -0.425564  
2023-09-03 13:40:00  1.454237  
2023-09-03 14:30:00  -0.46393  
2023-09-03 14:40:00  1.642925  
2023-09-03 15:30:00  0.312705  
2023-09-03 15:40:00  2.059798  
2023-09-03 16:30:00  -0.65732  
2023-09-03 16:40:00   0.42963  
2023-09-03 17:30:00 -0.780653  
2023-09-03 17:40:00  2.755777  
2023-09-03 18:30:00 -1.211638  
2023-09-03 18:40:00   1.43835  
2023-09-03 19:30:00 -1.195375  
2023-09-03 19:40:00  0.449282  
2023-09-03 20:30:00 -1.032738  
2023-09-03 20:40:00  0.583155  
2023-09-03 21:30:00 -0.990273  
2023-09-03 21:40:00  0.142854  
2023-09-03 22:30:00  -0.62581  
2023-09-03 22:40:00  0.369997  
2023-09-03 23:30:00 -0.506108  
2023-09-03 23:40:00  0.466836  

我得到的条形图如下

python axis x-axis
1个回答
0
投票

要增加 X 标签之间的距离,您可以使用

plt
增加绘图
plt.set_figwidth(n)
的宽度,其中
n
是新的宽度(以英寸为单位)。

使数据更具可读性的另一个选项是将 x 标签文本旋转 90°

plt.set_xticklabels(plt.get_xticks(), rotation=90)

由于您尚未发布代码,因此可能需要尝试不同的宽度和旋转值才能获得合适的输出。

© www.soinside.com 2019 - 2024. All rights reserved.