我无法将 Seaborn 中线图的 xticks 设置为相应小时的值 [英] I am unable to set the xticks of my lineplot in Seaborn to the values of the coresponding hour

查看:37
本文介绍了我无法将 Seaborn 中线图的 xticks 设置为相应小时的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried a lot of diffent methods but I can't get a reasonable xtick labeling. This is the code I wrote.

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' ])

我知道应该有一种更优雅的方式来列出每日时报".

I know there should be a more elegant way to list the Times of the day.

输出如下:

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

I printed the head of my data it looks like this:

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.
检查此代码:

Since I do not have access to your data, I created fake one in order to have some data to work with. You can just use your df.
Check this code:

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()

这给了我这个情节:

您可以使用以下方法调整x轴刻度和标签:

You can adjust the x axis ticks and labels with:

  • 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 度以改善可视化
  • graph.xaxis.set_major_locator(mdates.HourLocator(interval = 1)) to set ticks each hours
  • graph.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) to set the format of the x axis label to "hours:minutes"
  • plt.xticks(rotation = 90) to rotate by 90 degrees the x axis labels in order to improve the visualization

这篇关于我无法将 Seaborn 中线图的 xticks 设置为相应小时的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆