簡體   English   中英

如何在plotly python的所有子圖中添加水平線

[英]how to add horizontal line in all subplots in plotly python

我想在我生成的每個子圖中在0.09 and -0.09處添加水平線。 以下是我的代碼來做到這一點。

  trace1 = go.Scatter(
    x=df1['transaction_date'],
    y=df1['difference'],
    )
  trace2 = go.Scatter(
    x=df2['transaction_date'],
    y=df2['difference'],
)

 trace3 = go.Scatter(
   x=df3['transaction_date'],
   y=df3['difference'],

 )
 trace4 = go.Scatter(
   x=df4['transaction_date'],
   y=df4['difference'],

 )

 fig = tools.make_subplots(rows=2, cols=2,subplot_titles=('DF1 HS', DF2 HSD',
                                                     'DF3 HD', 'DF4 SD',
                                                     ))

 fig.append_trace(trace1, 1, 1)
 fig.append_trace(trace2, 1, 2)
 fig.append_trace(trace3, 2, 1) 
 fig.append_trace(trace4, 2, 2)

然后我想將這 4 個子圖保存為磁盤上的jpeg 我怎么能在python中做到這一點

嘗試使用以下shapes更新fig對象的layout

import plotly.graph_objs as go
from plotly import tools
from plotly.offline import init_notebook_mode, plot

df = pd.DataFrame(np.random.randint(0,100,size=(20,2)),
                  index=pd.date_range(start='2018-08-21',end='2018-09-09'),
                  columns=['A','B'])

trace1 = go.Scatter(x=df.index,y=df['A'],)
trace2 = go.Scatter(x=df.index,y=df['B'],)

fig = tools.make_subplots(rows=2, cols=1,subplot_titles=(['A','B']))

fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 2, 1)

fig['layout'].update(shapes=[{'type': 'line','y0':50,'y1': 50,'x0':str(df.index[0]), 
                              'x1':str(df.index[-1]),'xref':'x1','yref':'y1',
                              'line': {'color': 'red','width': 2.5}},
                             {'type': 'line','y0':50,'y1': 50,'x0':str(df.index[0]), 
                              'x1':str(df.index[-1]),'xref':'x2','yref':'y2',
                              'line': {'color': 'red','width': 2.5}}])

plot(fig,show_link=False,image='jpeg',image_filename='Temp_plot')

該圖將保存為Temp_plot.jpeg 檢查下面的圖像。 保存的情節

這種方法的缺點是我們需要仔細為子圖的xrefyref提供軸值。

你提到你對matplotlib解決方案沒問題:

數據:

dict = {
    "a":np.random.randint(low=-10,high=10,size=20),
    "b":np.random.randint(low=-10,high=10,size=20),
    "c":np.random.randint(low=-10,high=10,size=20),
    "d":np.random.randint(low=-10,high=10,size=20),
}

df = pd.DataFrame(dict)

陰謀:

fig, axes = plt.subplots(2,2, figsize=(20,10), sharex=True, sharey=True)
for i,j in zip(axes.ravel(), list(df)):
    i.plot(df.index, df[j], 'ro')
    i.hlines(y=-3, xmin=0, xmax=22)
    i.hlines(y=3, xmin=0, xmax=22)

fig.savefig("testplot.png")

結果:

在此處輸入圖片說明

我對 Plotly 還很陌生,所以也許 API 剛剛更新,但根據此處文檔,似乎有一個更簡單的解決方案。 只需要使用fig.add_hline()語法,同時指定應該在哪個子圖(col 和 row)上繪制,如下所示:

fig.add_hline(y=1, line_dash="dot", row=1, col=1, line_color="#000000", line_width=2)

該行將指示 Plotly 在位於row = 1的子圖中繪制一條水平線y = 1 row = 1 col = 1

或者,如 dox 中所述,“all”關鍵字可以作為rowcol參數的值傳遞,以指示 plotly 在(等待它...)所有子圖上繪制線!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM