【问题标题】:Error plotting a time column as x-axis ticks将时间列绘制为 x 轴刻度时出错
【发布时间】:2020-09-28 13:04:48
【问题描述】:

我有一个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: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, ....等等。

我正在尝试将 Time 列绘制为 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()

它会抛出以下错误:

Traceback(最近一次通话最后一次):

文件“”,第 2 行,在 plt.xticks(ticks=t, rotation=15)

文件 “/home/user/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py”, 第 1540 行,在 xticks 中 locs = ax.set_xticks(ticks)

文件 “/home/user/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py”, 第 3350 行,在 set_xticks 中 ret = self.xaxis.set_ticks(ticks, minor=minor)

文件 “/home/user/anaconda3/lib/python3.7/site-packages/matplotlib/axis.py”, 第 1755 行,在 set_ticks 中 self.set_view_interval(min(ticks), max(ticks))

文件 “/home/user/anaconda3/lib/python3.7/site-packages/matplotlib/axis.py”, 第 1892 行,在 setter 中 setter(self, min(vmin, vmax, oldmin), max(vmin, vmax, oldmax),

TypeError: 'numpy.ndarray' 的实例和 'str'

谁能告诉我我做错了什么?

还有,

w_df.dtypes
Out[27]: 
Time        object
Samstag    float64
Sonntag    float64
Werktag    float64
dtype: object

【问题讨论】:

    标签: python python-3.x matplotlib seaborn


    【解决方案1】:

    因此,我获取了您的一些数据并试图获得您的结果。不幸的是,我的 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)
    

    【讨论】:

      猜你喜欢
      • 2019-04-20
      • 2021-11-16
      • 2020-05-29
      • 2012-10-15
      • 1970-01-01
      • 2012-02-21
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多