I am having an issue with graph resizing due to legends when using pyplot. This is the code:
random_x = df['column1']
random_y = df['column2']
random_x1 = df['column1']
random_y1 = df['column3']
trace = go.Scatter(
x = random_x,
y = random_y,
name='abcdefghijklmnop......'
)
trace1 = go.Scatter(
x = random_x1,
y = random_y1,
name='abcdefghijklmnopadfdsfsdff......'
)
data = [trace,trace1]
iplot(data, filename='basic-line')
It gives me the graph but since my legend characters are long, it reduces the size of my actual graph. I want the legends to either come at the bottom or go further to the top
layout = go.Layout(
legend=dict(
orientation="h")
)
figure=go.Figure(data=data, layout=layout)
iplot(figure,filename='basic-line')
Related
In case of 2d charts in plotly we can set standoff of axis label by setting standoff property (example: https://community.plotly.com/t/adding-space-beetween-axis-title-and-values/32218), but in case of 3d plots there is no property standoff because axis have to specified as dictionary values of scene, the problem is custom ticks labels overlap with axis label:
import plotly.graph_objects as go
import numpy as np
ticktext = ["tick labels X"] * 3
layout = go.Layout(
scene = dict(
xaxis = dict(
title=dict(text='xaxis'
#, standoff=20 # don't work
),
tickvals=list(range(len(ticktext))),
ticktext=ticktext,
),
yaxis = dict(
title='yaxis',
tickvals=list(range(len(ticktext))),
ticktext=ticktext,
),
zaxis = dict(
title='zaxis',
)
),
)
# chart
data = np.array([[1,2,3],[3,1,2],[3,1,2]])
plotly_input_data = []
plotly_input_data.append(go.Surface(z = data + 1, showscale=False, opacity=0.9))
plotly_input_data.append(go.Surface(z = data**2-6, showscale=False, opacity=1.0))
fig = go.Figure(data=plotly_input_data, layout = layout)
fig.show()
This is '4.13.0' version of plotly.
Edition
Also using fig.layout.xaxis.title.standoff = 20 does not work.
Hi I am having trouble getting a lineplot to display properly. I have two axes and 5 line plots on one figure. The first y-axis limit cannot be set. I tried setting the range property to [0,2], however it doesn't do anything and continues to show from -2 to 4. I want the straight linear plot to overlay directly on top of the other 4 line plots and I don't know why the x-axis starts from -5. Can someone help fix the issue?
fig = go.Figure()
xs = np.linspace(0,12.5,plot_df.shape[0])
for cn in plot_df.columns:
ys = plot_df[cn].to_numpy()
fig.add_trace(go.Scatter(x=xs, y=ys,
mode='lines',
name=cn)
)
fig.add_trace(go.Scatter(x=xs, y=xs,
mode='lines',
name='mob. grad', yaxis="y2")
)
fig.update_layout(
title = "UV and Mobile phase trace of {}".format(field_dict['sample_name']),
xaxis = dict(
title = "Minutes",
domain=[0.2, 1]
),
yaxis = dict(
scaleanchor = "x",
title = "UV Abs",
range = [0,2],
position = 0.19
),
yaxis2 = dict(
title = "Mobile Phase (%)",
anchor="free",
domain=[0.1,1],
overlaying="y",
side="left",
position=0.08,
range=[0,100])
)
fig.show()
I just removed scaleanchor = "x" and it showed up properly.
I have a Scatter chart and I need to trace the bisector line on it. I'm wondering if there is a automated way on Plotly to generate that, or a simplified way to calculate the equation in python to generate the list of values and trace the line.
My code:
trace1 = go.Scatter(
x = eixo_x, # My list of values for 'x'
y = eixo_y, # My list of values for 'y'
mode = 'markers',
marker = dict(color = 'rgb(0, 176, 240)')
)
dados = [trace1]
layout = go.Layout(
title = 'Unitários Observados | Unitários Estimados',
autosize = False,
width = 1000,
height = 500,
xaxis = dict(
showgrid = True),
yaxis = dict(
tickmode = 'array',
showgrid = True
)
)
fig = go.Figure(data = dados, layout = layout)
fig.show()
My Plot:
Now I need to plot a line on diagonal, the bisector.
EDIT
There are a few questions about bisector, but not for Plotly.
EDIT 2
In geometry, bisection is the division of something into two equal or congruent parts, usually by a line, which is then called a bisector. Wikipedia
Example:
I didn't use an equation or an automated function from Plotly but the following code solved my problem temporarily:
I've setted the line limits for x and y getting the min and max from each list:
# Trace 2
line_x = [min(eixo_x), max(eixo_x)]
line_y = [min(eixo_y), max(eixo_y)]
And added a second trace for it:
trace2 = go.Scatter(
x = line_x,
y = line_y,
mode = 'lines',
marker = dict(color = 'rgb(127, 127, 127)'),
line = dict(dash = 'dash')
)
Result:
I am trying to replicate this excel line chart in python using plotly.
Is there any way to add the high-low lines between the two line graphs in Plotly?
Thanks
Just an update on this post. Plotly doesn't seem to have a property to draw lines between points of 2 line plots. So I made the connecting lines as an array of trace and then plotted them on the same figure. Here's a snapshot of resulting plot
:
trace_1 = go.Scatter(x=x_arr, y=y1_arr, name='plot1', line=dict(color = ('royalblue')), mode='lines+markers')
trace_2 = go.Scatter(x=x_arr, y=y2_arr, name='plot2', line=dict(color = ('orange')), mode='lines+markers')
layout_1 = go.Layout(
height=420,
width=800,
title=go.layout.Title(
text='title',
),
xaxis=go.layout.XAxis(
title='x axis',
),
yaxis=go.layout.YAxis(
title='y axis',
)
)
data = []
trace_3_arr = np.array([])
for i in range(0, len(x_arr)):
trace_i = go.Scatter(x=[x_arr[i], x_arr[i]], y=[y1_arr[i], y2_arr[i]], line=dict(color = ('black'), width=1), showlegend=False)
trace_3_arr = np.append(trace_3_arr, trace_i)
data.append(trace_i)
data.append(trace_1)
data.append(trace_2)
fig = go.Figure(data=data, layout=layout_1)
plot(fig)
I generate daily two histograms from data, one with the needed values and the other with the reached values for different stations. I want to plot these histograms side by side, like the bottom pink example in Plotly here (see link for source code). However, since both histograms are generated daily, I need to add a time slider to the graph, like the bottom example 'Simple Slider' from Plotly (see link for source code).
My problem is that the first example uses
fig = dict(data=data, layout=layout)
plotly.offline.plot(fig, filename='Sine Wave Slider')
to plot the histogram, while for the slider the following is used:
import plotly.graph_objs as go
fig = go.Figure(data=data, layout=layout)
plotly.offline.plot(fig, filename='styled histogram')
My (not functioning) code right now is looking like this, where I try to plot the same 2 histograms 3 times. How can I change the code to generate a figure that uses both histograms (both with different random data) and the slider at the same time?
import plotly
import plotly.graph_objs as go
import numpy as np
x0 = np.random.randn(500)
x1 = np.random.randn(500)+1
trace1 = go.Histogram(
x=x0,
histnorm='count',
name='control',
autobinx=False,
xbins=dict(
start=-3.5,
end=3.0,
size=0.5
),
marker=dict(
color='#FFD7E9',
),
opacity=0.75
)
trace2 = go.Histogram(
x=x1,
name='experimental',
autobinx=False,
xbins=dict(
start=-2.0,
end=5,
size=0.5
),
marker=dict(
color='#EB89B5'
),
opacity=0.75
)
data = [trace1, trace2]
layout = go.Layout(
title='Sampled Results',
xaxis=dict(
title='Value'
),
yaxis=dict(
title='Count'
),
bargap=0.2,
bargroupgap=0.1
)
steps = []
for i in range(len(trace1)):
step = dict(
method = 'restyle',
args = ['visible', [False] * len(trace1)],
)
step['args'][1][i] = True # Toggle i'th trace to "visible"
steps.append(step)
sliders = [dict(
active = 20,
currentvalue = {"prefix": "Frequency: "},
pad = {"t": 3},
steps = steps
)]
layout = dict(sliders=sliders)
fig = dict(data=data, layout=layout)
plotly.offline.plot(fig, filename='Histogram Slider')
You could create a list of histograms, let's say 3 days (total_days = 3, odd numbers are experimental, even numbers are control).
Only the first traces are shown (visible = day < 1).
Each step in the slider shows/hides another pair of traces.
import plotly
import numpy as np
plotly.offline.init_notebook_mode()
total_days = 3
data = list()
for day in range(total_days):
data.append(plotly.graph_objs.Histogram(
x=np.random.randn(500) + day * 0.5,
histnorm='count',
name='Day {}, control'.format(day),
visible=day < 1
)
)
data.append(plotly.graph_objs.Histogram(
x=np.random.randn(500) + day,
histnorm='count',
name='Day {}, experimental'.format(day),
visible=day < 1
)
)
steps = list()
for i in range(total_days):
step = dict(
method='restyle',
args=['visible', [False] * total_days * 2],
label='Day {}'.format(i)
)
step['args'][1][i * 2] = True
step['args'][1][i * 2 + 1] = True
steps.append(step)
sliders = [dict(
active=0,
steps=steps
)]
layout = dict(sliders=sliders)
fig = dict(data=data, layout=layout)
plotly.offline.iplot(fig)