[英]How to set a static set of x-axis values and legend for a Plotly Bar Graph?
[英]Set alignment for Plotly Bar graph x axis
嗨,我正在嘗試更改條形圖上柱線的起始和終止位置。 我的目標是擁有一個跨越整個月的酒吧。 現在我的代碼是:
cumulative = go.Bar(y=self.gb[self.y_column],
x=self.gb.index,
name='Cumulative',
hoverinfo='x+y',
hovertemplate="Month: %{x} <br>Cumulative Tank " + self.units + ": %{y}",
opacity=0.6,
marker=dict(color='rgb(158,202,225)',
line=dict(color='rgb(8,48,107)',
width=1.5,
),
)
)
我目前在本月底設置了xtick。 如果我可以使條形與xtick右對齊,則可以使用自定義條形寬度來使其達到月底。 我認為我無法計算該月的中間日期,因為在31天的月份中,我將有一天的間隔或一天的間隔。 任何幫助將不勝感激!
我在這個問題上發表的一個陰謀性的論壇帖子: https ://community.plot.ly/t/set-alignment-of-vertical-bar-on-x-axis/23185
這不是解決方案,但我很滿意
因此,我決定使用實線圖來解決此問題。 這是代碼,它是一個幫助函數,用於計算每月累積值和繪圖填充區域散布對象:
def cumulative_df(df, date_term, y_term, v_print):
v_print('Making the Monthly Sum DF')
df = df[[date_term, y_term]]
df = df.set_index(date_term)
gb = df.groupby(pd.Grouper(freq="M")).sum()
starts = []
for index, row in gb.iterrows():
new_start = index - pd.offsets.MonthBegin(1, normalize=True)
starts.append([new_start, row.values[0]])
starts_df = pd.DataFrame(starts, columns=[date_term, y_term])
starts_df = starts_df.set_index(date_term)
gb = gb.append(starts_df, sort=False)
gb.sort_values(date_term, inplace=True)
return gb
並果斷地調用:
cumulative = [go.Scatter(y=self.gb[self.y_column],
x=self.gb.index,
name='Cumulative',
hoverinfo='x+y',
hovertemplate="Month: %{x} <br>Cumulative Meter " + self.units + ": %{y}",
fill='tozeroy',
fillcolor=self.fill_color,
line={'color': self.cum_line_color,}
)]
scatters = cumulative + scatters
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.