[英]How to plot yearly data using plotly?
我試圖以 plotly 繪制每年的值計數,但是我得到了不必要的 xticks。 我的情節情節看起來像:
額外的,xticks 是我不想要的。
我的情節代碼看起來像:
fig = go.Figure()
fig.add_trace(go.Bar(x = data.year.value_counts().index.tolist(),
y = data.year.value_counts().values.tolist(),
marker_color='#330C73',
opacity=0.75))
fig.update_xaxes(tickmode = 'array')
fig.update_layout(bargap=0.2, # gap between bars of adjacent location coordinates
#bargroupgap=0.1, # gap between bars of the same location coordinates,
title={
'text': "Yearly Enrolment",
'y':0.95,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'},
xaxis_title="Year",
yaxis_title="Count",
font=dict(
family="Arial",
size=10
),margin=go.layout.Margin(
l=0, #left margin
r=0, #right margin
b=0, #bottom margin
t=20.5 #top margin
),
width=500,
height=350,
legend=dict(orientation="h", font=dict(size=8)),
paper_bgcolor='rgba(255, 255, 255, 0)',
plot_bgcolor='rgba(255, 255, 255, 0)')
fig.show()
你可以簡單地做
import numpy as np
import pandas as pd
pd.options.plotting.backend = "plotly"
data = pd.DataFrame({"year":np.random.choice([2019, 2020, 2021], 50)})
fig = data["year"].value_counts().plot(
kind="bar",
title="Yearly SME Enrolment",
color=["r", "b", "g"],
labels={"value": "Count",
"index": "Year",
# "color": "Year"
},
)
fig.update_layout(showlegend=False)
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.