gpt4 book ai didi

python - 我无法将我在 Seaborn 中的线图的 xticks 设置为相应小时的值

转载 作者:行者123 更新时间:2023-12-01 23:57:18 24 4
gpt4 key购买 nike

我尝试了很多不同的方法,但我无法获得合理的 xtick 标签。这是我写的代码。

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

#Line of Code just for importing the .csv Data
df = pd.read_csv('path of the csv file', sep=",", comment='#', decimal='.', parse_dates=True)

xticks = df.time.unique()


table = df.pivot_table("globalpower", index="time", aggfunc=np.mean)

graph = sns.lineplot(df.time, df.globalpower, data=df)
graph.set_xticks(range(0,24))
graph.set_xticklabels(['01:00','02:00','03:00','04:00','05:00','06:00','07:00','08:00','09:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00','24:00' ])

我知道应该有一种更优雅的方式来列出当天的时间。

输出看起来像这样:

This is my current output

我打印了我的数据头部,它看起来像这样:

Unnamed:    0      date      time  globalpower  voltage  globintensity  submetering1  submetering2  submetering3
0 1600236 1/1/2010 00:00:00 1.790 240.65 7.4 0.0 0.0 18.0
1 1600237 1/1/2010 00:01:00 1.780 240.07 7.4 0.0 0.0 18.0
2 1600238 1/1/2010 00:02:00 1.780 240.15 7.4 0.0 0.0 19.0
3 1600239 1/1/2010 00:03:00 1.746 240.26 7.2 0.0 0.0 18.0
4 1600240 1/1/2010 00:04:00 1.686 240.12 7.0 0.0 0.0 18.0

最佳答案

由于我无权访问您的数据,因此我创建了一个假数据以便使用一些数据。你可以只使用你的df
检查这段代码:

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

N = 1440
time = pd.date_range('2020-01-01', periods = N, freq = 'min')
globalpower = np.random.randn(N)
df = pd.DataFrame({'time': time,
'globalpower': globalpower})

graph = sns.lineplot(df.time, df.globalpower, data = df)
graph.xaxis.set_major_locator(mdates.HourLocator(interval = 1))
graph.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
plt.xticks(rotation = 90)

plt.show()

这给了我这个情节:

enter image description here

您可以调整 x 轴刻度和标签:

  • graph.xaxis.set_major_locator(mdates.HourLocator(interval = 1)) 设置每小时的刻度数
  • graph.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) 将 x 轴标签的格式设置为“小时:分钟”
  • plt.xticks(rotation = 90) 将 x 轴标签旋转 90 度以改善可视化效果

关于python - 我无法将我在 Seaborn 中的线图的 xticks 设置为相应小时的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62540351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com