我有一个数据框
Date Category Sum
0 2019-06-03 "25M" 34
1 2019-06-03 "25M" 60
2 2019-06-03 "50M" 23
3 2019-06-04 "25M" 67
4 2019-06-05 "50M" -90
5 2019-06-05 "50M" 100
6 2019-06-06 "100M" 6
7 2019-06-07 "25M" -100
8 2019-06-08 "100M" 67
9 2019-06-09 "25M" 450
10 2019-06-10 "50M" 600
11 2019-06-11 "25M" -9
12 2019-07-12 "50M" 45
13 2019-07-13 "50M" 67
14 2019-07-14 "100M" 130
15 2019-07-14 "50M" 45
16 2019-07-15 "100M" 100
17 2019-07-16 "25M" -90
18 2019-07-17 "25M" 700
19 2019-07-18 "25M" -9
我想创建一个绘图图,显示每个描述的日期在不同“类别”上添加的“总和”,但是要删除没有数据的日期。
代码
df["Date"]=pd.to_datetime(df["Date"], format=("%Y%m%d"))
df=df.sort_values(["Date","Category","Sum"],ascending=False)
df=round(df.groupby(["Date","Category"]).agg({"Sum":"sum"}).reset_index(),1)
fig = px.bar(df, x=df["Date"] , y='Sum',barmode="group",color="Category")
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
buttons=list([
dict(count=1, label="day", step="day", stepmode="todate"),
dict(count=24, label="montly", step="month", stepmode="todate"),
dict(count=1, label="year", step="year", stepmode="todate"),
dict(step="all")
])
))
fig.show()
我正在获得这样的图形,但我想从绘图中删除空的日期
此问题来自以下事实:将您的'Date'
巧妙地解释为dates,在最旧和最新的时间戳之间创建了一个连续的时间段,从而有效地显示了没有关联数据的日期作为间隔。一种解决方案是在日期列中获取第一个和最后一个日期,并在该期间内创建一个[[complete日期列表,然后找出哪些日期no]]有任何观察结果,并将其存储在一个名为dt_breaks
的变量。然后,最后可以将这些日期包括在:中fig.update_xaxes(
rangebreaks=[dict(values=dt_breaks)] # hide dates with no values
)
这将在可视化中删除这些日期,将x值设置为日期格式,以便您可以使用按钮来对数据进行子集设置:和
而且,您已经知道,这里是没有rangebreaks=[dict(values=dt_breaks)]
的相同可视化:
[为了使这项工作尽可能简单,我使用df=df.sort_values(["Date","Category","Sum"],ascending=True)
而不是原始代码段中的df=df.sort_values(["Date","Category","Sum"],ascending=False)
重新排列了日期列
完整代码:
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'Date': {0: '2019-06-03',
1: '2019-06-03',
2: '2019-06-03',
3: '2019-06-04',
4: '2019-06-05',
5: '2019-06-05',
6: '2019-06-06',
7: '2019-06-07',
8: '2019-06-08',
9: '2019-06-09',
10: '2019-06-10',
11: '2019-06-11',
12: '2019-07-12',
13: '2019-07-13',
14: '2019-07-14',
15: '2019-07-14',
16: '2019-07-15',
17: '2019-07-16',
18: '2019-07-17',
19: '2019-07-18'},
'Category': {0: '"25M"',
1: '"25M"',
2: '"50M"',
3: '"25M"',
4: '"50M"',
5: '"50M"',
6: '"100M"',
7: '"25M"',
8: '"100M"',
9: '"25M"',
10: '"50M"',
11: '"25M"',
12: '"50M"',
13: '"50M"',
14: '"100M"',
15: '"50M"',
16: '"100M"',
17: '"25M"',
18: '"25M"',
19: '"25M"'},
'Sum': {0: 34,
1: 60,
2: 23,
3: 67,
4: -90,
5: 100,
6: 6,
7: -100,
8: 67,
9: 450,
10: 600,
11: -9,
12: 45,
13: 67,
14: 130,
15: 45,
16: 100,
17: -90,
18: 700,
19: -9}})
df["Date"]=pd.to_datetime(df["Date"], format=("%Y-%m-%d"))
df=df.sort_values(["Date","Category","Sum"],ascending=True)
df=round(df.groupby(["Date","Category"]).agg({"Sum":"sum"}).reset_index(),1)
dt_all = pd.date_range(start=df['Date'].iloc[0],end=df['Date'].iloc[-1])
dt_obs = [d.strftime("%Y-%m-%d") for d in df['Date']]
dt_breaks = [d for d in dt_all.strftime("%Y-%m-%d").tolist() if not d in dt_obs]
df=df.set_index('Date')
#fig = px.bar(df, x=df.index.strftime("%Y/%m/%d") , y='Sum',barmode="group",color="Category")
fig = px.bar(df, x=df.index , y='Sum',barmode="group",color="Category")
fig.update_xaxes(
#rangebreaks=[dict(values=dt_breaks)] # hide dates with no values
)
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
buttons=list([
dict(count=1, label="day", step="day", stepmode="todate"),
dict(count=24, label="montly", step="month", stepmode="todate"),
dict(count=1, label="year", step="year", stepmode="todate"),
dict(step="all")
])
))
fig.show()