Reputation: 8247
I want to add horizontal line at 0.09 and -0.09
in every subplot I am generating in plotly. Following is my code to do that.
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)
Then I want to save this 4 subplots as jpeg
on disk. How can I do that in python
Upvotes: 5
Views: 12609
Reputation: 4450
I'm pretty new to Plotly so maybe the API has just been updated, but it seems there is a much simpler solution, per the documentation here. One need only use the fig.add_hline()
syntax while specifying which subplot (col and row) it should be drawn on, as such:
fig.add_hline(y=1, line_dash="dot", row=1, col=1, line_color="#000000", line_width=2)
This line will instruct Plotly to draw a horizontal line y = 1
on the subplot located at row = 1
; col = 1
.
Alternatively, as noted in the dox, the "all" keyword can be passed as a value for either the row
or col
argument to instruct plotly to draw the line on (wait for it...) all the subplots!
Upvotes: 8
Reputation: 3309
matplotlib
solution:Data:
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)
Plot:
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")
Result:
Upvotes: 1
Reputation: 13255
Try updating layout
of fig
object with shapes
as below:
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')
The plot will be saved as Temp_plot.jpeg
. Check the image below.
The downside of this method is we need to carefully give axes values to xref
and yref
with respect to subplots.
Upvotes: 3