Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 修改seaborn中的x记号标签_Python_Data Visualization_Seaborn - Fatal编程技术网

Python 修改seaborn中的x记号标签

Python 修改seaborn中的x记号标签,python,data-visualization,seaborn,Python,Data Visualization,Seaborn,我正在尝试将x-tick标签的格式修改为日期格式(%m-%d) 我的数据由特定日期段内的每小时数据值组成。我试图绘制14天的数据。然而,当我运行时,我得到的x标签完全混乱 有没有办法在x轴上只显示日期和跳过小时值?有没有办法修改x记号,让我可以跳过几个小时的标签,只显示日期的标签?我用的是seaborn 根据评论的建议,我编辑了我的代码,如下所示: fig, ax = plt.pyplot.subplots() g = sns.barplot(data=data_n,x='datetime',

我正在尝试将x-tick标签的格式修改为日期格式(%m-%d)

我的数据由特定日期段内的每小时数据值组成。我试图绘制14天的数据。然而,当我运行时,我得到的x标签完全混乱

有没有办法在x轴上只显示日期和跳过小时值?有没有办法修改x记号,让我可以跳过几个小时的标签,只显示日期的标签?我用的是seaborn

根据评论的建议,我编辑了我的代码,如下所示:

fig, ax = plt.pyplot.subplots()
g = sns.barplot(data=data_n,x='datetime',y='hourly_return')
g.xaxis.set_major_formatter(plt.dates.DateFormatter("%d-%b"))
但我得到了以下错误:

ValueError: DateFormatter found a value of x=0, which is an illegal 
date; this usually occurs because you have not informed the axis that 
it is plotting dates, e.g., with ax.xaxis_date()
在检查datetime列后,我得到了该列数据类型的以下输出:

0     2020-01-01 00:00:00
1     2020-01-01 01:00:00
2     2020-01-01 02:00:00
3     2020-01-01 03:00:00
4     2020-01-01 04:00:00
          ...        
307   2020-01-13 19:00:00
308   2020-01-13 20:00:00
309   2020-01-13 21:00:00
310   2020-01-13 22:00:00
311   2020-01-13 23:00:00
Name: datetime, Length: 312, dtype: datetime64[ns]
我怀疑是x记号,所以当我运行
g.get_xticks()
[它获取x轴上的记号]时,我得到了作为序号的输出。有人能告诉我为什么会这样吗?

1。用x轴日期时间绘制线图的方法 您可以尝试按如下方式更改x轴格式吗

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import dates

## create dummy dataframe
datelist = pd.date_range(start='2020-01-01 00:00:00', periods=312,freq='1H').tolist()
#create dummy dataframe
df = pd.DataFrame(datelist, columns=["datetime"])
df["val"] = [i for i in range(1,312+1)]
df.head()
下面是数据帧信息

绘图

fig, ax = plt.subplots()
chart = sns.lineplot(data=df, ax=ax, x="datetime",y="val")
ax.xaxis.set_major_formatter(dates.DateFormatter("%d-%b"))
输出:

2.利用seaborn和x轴日期时间绘制条形图的方法 如果绘制条形图,则上述方法存在问题。所以,将使用下面的代码

fig, ax = plt.subplots()
## barplot
chart = sns.barplot(data=df, ax=ax,x="datetime",y="val")

## freq of showing dates, since frequency of datetime in our data is 1H. 
## so, will have every day 24data points
## Trying to calculate the frequency by each day 
## (assumed points are collected every hours in each day, 24)
## set the frequency for labelling the xaxis
freq = int(24)
# set the xlabels as the datetime data for the given labelling frequency,
# also use only the date for the label
ax.set_xticklabels(df.iloc[::freq]["datetime"].dt.strftime("%d-%b-%y"))
# set the xticks at the same frequency as the xlabels
xtix = ax.get_xticks()
ax.set_xticks(xtix[::freq])
# nicer label format for dates
fig.autofmt_xdate()

plt.show()
输出:


当我执行该命令时,我的错误率越来越低:“DateFormatter发现了一个x=0的值,这是一个非法的日期;这通常是因为您没有通知axis它正在打印日期,例如,使用ax。xaxis_date()'表示x值不在
datetime
对象中。将x列转换为datetime对象为
pd。转换为\u datetime(df[“xaxis”])
@NaveenGabriel如果不隐式传递x轴,则可以将数据帧索引值更改为
datetime对象
。我的数据帧列肯定是date-time对象类型。您能在此处提供x轴值吗?