Python 将时间列打印为x轴刻度时出错
我有一个df如下Python 将时间列打印为x轴刻度时出错,python,python-3.x,matplotlib,seaborn,Python,Python 3.x,Matplotlib,Seaborn,我有一个df如下 Time Samstag 0 00:15:00 80.6 1 00:30:00 74.6 2 00:45:00 69.2 3 01:00:00 63.6 4 01:15:00 57.1 5 01:30:00 50.4 6 01:45:00 44.1 7 02:00:00 39.1 8 02:15:00 36.0 9 02:30:00 34.4 10 02:45:0
Time Samstag
0 00:15:00 80.6
1 00:30:00 74.6
2 00:45:00 69.2
3 01:00:00 63.6
4 01:15:00 57.1
5 01:30:00 50.4
6 01:45:00 44.1
7 02:00:00 39.1
8 02:15:00 36.0
9 02:30:00 34.4
10 02:45:00 33.7
11 03:00:00 33.3
12 03:15:00 32.7
13 03:30:00 32.0
14 03:45:00 31.5
15 04:00:00 31.3
16 04:15:00 31.5
17 04:30:00 31.7
18 04:45:00 31.5
19 05:00:00 30.3
20 05:15:00 28.1
21 05:30:00 26.4
22 05:45:00 27.1
23 06:00:00 32.3
24 06:15:00 42.9
25 06:30:00 56.2
26 06:45:00 68.5
27 07:00:00 76.3
28 07:15:00 77.0
29 07:30:00 72.9
30 07:45:00 67.3
31 08:00:00 63.6
32 08:15:00 64.5
33 08:30:00 69.5
34 08:45:00 77.4
35 09:00:00 87.1
36 09:15:00 97.4
37 09:30:00 108.4
38 09:45:00 119.9
39 10:00:00 132.1
40 10:15:00 144.7
41 10:30:00 156.7
42 10:45:00 166.9
43 11:00:00 174.1
44 11:15:00 177.4
45 11:30:00 177.7
46 11:45:00 176.2
47 12:00:00 174.1
48 12:15:00 172.6
49 12:30:00 172.0
50 12:45:00 172.4
51 13:00:00 174.1
52 13:15:00 177.1
53 13:30:00 180.4
54 13:45:00 183.0
55 14:00:00 183.9
56 14:15:00 182.4
57 14:30:00 179.5
58 14:45:00 176.6
59 15:00:00 175.1
60 15:15:00 176.0
61 15:30:00 178.9
62 15:45:00 182.8
63 16:00:00 186.8
64 16:15:00 190.3
65 16:30:00 193.8
66 16:45:00 197.9
67 17:00:00 203.5
68 17:15:00 210.8
69 17:30:00 218.8
70 17:45:00 226.3
71 18:00:00 231.8
72 18:15:00 234.4
73 18:30:00 234.5
74 18:45:00 233.0
75 19:00:00 230.9
76 19:15:00 228.7
77 19:30:00 226.9
78 19:45:00 225.3
79 20:00:00 224.0
80 20:15:00 223.0
81 20:30:00 221.5
82 20:45:00 218.9
83 21:00:00 214.2
84 21:15:00 207.0
85 21:30:00 197.0
86 21:45:00 184.4
87 22:00:00 169.2
88 22:15:00 151.8
89 22:30:00 133.7
90 22:45:00 116.7
91 23:00:00 102.7
92 23:15:00 93.0
93 23:30:00 86.6
94 23:45:00 82.2
我试图将其描绘如下:
sns.lineplot(x="Time", y="Samstag", data=w_df)
plt.xticks(rotation=15)
plt.xlabel("Time")
plt.ylabel("KWH")
plt.show()
它给出了:
x轴的标签为00:00、05:33:20。。。。等等
我试图将时间列绘制为x轴上的刻度
我试过:
t = pd.to_datetime(w_df["Time"], format='%H:%M:%S')
t = t.apply(lambda x: x.strftime('%H:%M:%S'))
sns.lineplot(x="Time", y="Samstag", data=w_df)
plt.xticks(ticks=t, rotation=15)
plt.xlabel("Time")
plt.ylabel("KWH")
plt.show()
它抛出以下错误:
回溯最近一次呼叫上次:
文件,第2行,在
plt.xticksticks=t,旋转=15
文件
/home/user/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py,
第1540行,在xticks中
locs=ax.set\u xticksticks
文件
/home/user/anaconda3/lib/python3.7/site-packages/matplotlib/axes//u base.py,
第3350行,在集合中
ret=self.xaxis.set_ticksticks,minor=minor
文件
/home/user/anaconda3/lib/python3.7/site-packages/matplotlib/axis.py,
第1755行,在集合中
self.set\u view\u intervalminticks,maxticks
文件
/home/user/anaconda3/lib/python3.7/site-packages/matplotlib/axis.py,
第1892行,塞特
setterself、minvmin、vmax、oldmin、maxvmin、vmax、oldmax、
TypeError:“所以我获取了一些您的数据并试图得到您的结果。不幸的是,我的Seaborn绘图与您希望的格式相同。这可能与时间列的格式有关。当我从您的示例中创建我的小数据集时,我将时间列设置为字符串,看起来一切都很好
d = {'Time': ["00:15:00", "00:30:00", "00:45:00", "01:00:00", "01:15:00", "01:30:00", "01:45:00",
"02:00:00", "02:15:00", "02:30:00", "02:45:00", "03:00:00", "03:15:00", "03:30:00", "03:45:00",
"04:00:00", "04:15:00", "04:30:00", "04:45:00", "05:00:00", "05:15:00", "05:30:00",
"05:45:00", "06:00:00"],
'Samstag': [80.6, 74.6,69.2, 62.6, 57.1,50.4, 44.1, 39.1, 36.0, 34.4, 33.7,33.3, 32.7, 32.0,
31.5, 31.3, 31.5, 31.7, 31.5,30.3, 28.1, 26.4, 27.1, 32.3]
}
df = pd.DataFrame(d)
sns.lineplot(x="Time", y="Samstag", data=df)
plt.xticks(rotation=15)
plt.xlabel("Time")
plt.ylabel("KWH")
plt.show()
这使得每个时间戳都有一个记号。也许您可以将时间列更改为字符串(如果尚未更改的话)
df['Time'] = df['Time'].astype(str)
df['Time'] = df['Time'].astype(str)