[使用绘图逐年绘制数据

问题描述 投票:0回答:1

我的数据看起来像这样:-

Month    Year       Value
Jan      2015       2.8          
Jan      2015       2.0   
Mar      2016       0.9    
Feb      2015       3.1  
Mar      2016       4.2   
Feb      2015       2.1    
Mar      2016       2.3       
Feb      2015       1.1
Apr      2016       1.3
Apr      2016       0.5

现在,我想绘制折线图,​​但是通过使用此代码,我得到了此输出。 [enter image description here以下

我使用的代码:-

df = rslt_bb.sort_values(by='Year')
trace = go.Scatter(
x = df["Year"],
y = df["Value"],
mode='markers+lines'
)

layout = go.Layout(
#title='Distribution by year',
xaxis=dict(title='Year'),
yaxis=dict(title='Value'),
showlegend=True    
)

fig = dict(data=[trace], layout=layout)
offline.iplot(fig)

我希望绘图不像是在同一行中汇总所有普通年份,我想散布。全年,单个数据应分别可见,如下所示,enter image description here

python pandas plotly
1个回答
0
投票

您需要创建datetime对象才能绘制Time Series。所以代替

x = df["Year"]

用途

x = pd.to_datetime(df.Year*100+df.Month, format='%Y%m')
© www.soinside.com 2019 - 2024. All rights reserved.