Python - Issue with plotly graph on jupyter notebook - python

I am plotting a graph with plotly on jupyter notebook.
The actual graph should look like this.
However, I am getting this.
Please how can i correct this.
Please see below code:
# Setting layout for our plot
layout = go.Layout(
title='Stock Prices of Tesla',
xaxis=dict(
title='Date',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
),
yaxis=dict(
title='Price',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
)
)
tesla_data = [{'x': tesla['Date'], 'y': tesla['Close']}]
plot = go.Figure(data=tesla_data, layout=layout)
#plot(plot) #plotting offline
iplot(plot)
Please see snapshot of my data

Related

Plotly add dropdown in graph to show multiple graphs

I have plotted 4 types of graphs, bar,scatter,pie,sunburst for a dataset.
data=winner_list
plot = px.bar(winner_list,x="Team",y="Win_count")
plot.show()
when i use the following code, i cannot view the multiple graphs in dropdown, only the first graph is visible
#to give dropdown for plots
plot.update_layout(
updatemenus=[
dict(
buttons=list([
dict(
args=["type", "pie"],
label="Bar Chart",
method="restyle"
),
dict(
args=["type", "scatter"],
label="s chart",
method="restyle"
),
dict(
args=["type", "fig14"],
label="P chart",
method="restyle"
)
]),
direction="down",
),
]
)
plot.show()
how can i place a dropdown in the graph, so that i can view different plots

in plotly show text by default for map when saving to image

I have created a plotly map and added some lat, lon points on it with marker and text and it shows fine when viewed as interactive map on Jupyter notebook but when I'm saving that map as PNG text doesn't show.
here is my code
import plotly.graph_objects as go
mapbox_access_token = open(".mapbox_token").read()
fig = go.Figure(go.Scattermapbox(
lat=['45.5017'],
lon=['-73.5673'],
mode='markers',
marker=go.scattermapbox.Marker(
size=14
),
text=['Montreal'],
))
fig.update_layout(
hovermode='closest',
mapbox=dict(
accesstoken=mapbox_access_token,
bearing=0,
center=go.layout.mapbox.Center(
lat=45,
lon=-73
),
pitch=0,
zoom=5
)
)
fig.show()
Expected output
Output I get when image is saved
i just want all text/annotation visible after saving image
The hover function can be enabled by moving the mouse cursor closer. So if you want to save a graph, that information will not be saved. If you add text information to the map as annotations, it will be saved in the image. The position, text color, font family, and size are specified for the annotation. The display format can be standardized by using texttemplate.
import plotly.graph_objects as go
mapbox_access_token = open("mapbox_api_key.txt").read()
fig = go.Figure(go.Scattermapbox(
lat=['45.5017'],
lon=['-73.5673'],
mode='markers+text',
marker=go.scattermapbox.Marker(
size=14
),
text=['Montreal'],
textfont={'color':'royalblue', 'family':'Arial','size':16},
textposition='bottom right',
texttemplate='(%{lat},%{lon})%{text}'
))
fig.update_layout(
hovermode='closest',
mapbox=dict(
accesstoken=mapbox_access_token,
bearing=0,
center=go.layout.mapbox.Center(
lat=45,
lon=-73
),
pitch=0,
zoom=5
)
)
fig.show()

Plotly: How to save interactive plotly plot as pdf file?

I created an interactive plot using plotly as following:
tracer_red = go.Scatter(x=Xedges_red, y=Yedges_red,
mode='lines',
line= dict(color="#FA0000", width=1),
hoverinfo='none',
showlegend=False)
# positive:
tracer_green = go.Scatter(x=Xedges_green, y=Yedges_green,
mode='lines',
line= dict(color= "#29A401", width=1),
hoverinfo='none',
showlegend=False)
# nodes
tracer_marker = go.Scatter(x=Xnodes, y=Ynodes,
mode='markers+text',
textposition='middle center',
marker=dict(size=node_size,
line=dict(width=0.8, color='black'),
color=node_colour, symbol=node_shape),
hovertext=description,
hoverinfo='text',
text=node_label,
textfont=dict(size=10),
showlegend=False)
axis_style = dict(title='',
titlefont=dict(size=20),
showgrid=False,
zeroline=False,
showline=False,
ticks='',
showticklabels=False)
layout = dict(title='',
width=1300,
height=900,
autosize=False,
showlegend=False,
xaxis=axis_style,
yaxis=axis_style,
hovermode='closest',
plot_bgcolor = '#fff')
fig = dict(data=[tracer_red, tracer_green, tracer_marker], layout=layout)
display(HTML("""<p>Node sizes are proportional to the size of annualised returns.<br>
Node colours signify positive or negative returns since beginning of the timeframe.</p> """))
plot(fig)
I want to save the plot as a pdf file or as a picture.
when I saveing with the "camera" it saveing in a very low resulotion
because I created the fig as a dict, I cant us the command fig.write_image()
do you know other way to save the fig as a pic\PDF file?
Thank you!

Why my map size is too small in Dash-Plotly App?

I am using python libraries called dash and plotly to create an interactive dashboard. I would like to set the map size to let it fill the whole Div. The map is created using mapbox. I tried to set style but not working.
My map looks like this:
I noticed other people’s map fill the whole Div. (See the position of widgets):
Here are my code:
map_data = [
go.Scattermapbox(
lat=df['latitude'],
lon=df['longitude'],
mode='markers',
marker=dict(
cmax=50,
cmin=0,
color=df['depth'],
colorbar=dict(
title='Colorbar'
),
colorscale='YlGnBu',
reversescale=True,
size=5,
# opacity=0.9
),
text=df['depth'],
hoverinfo='text'
)
]
map_layout = go.Layout(
title='Bathymetrical Data',
autosize=True,
hovermode='closest',
xaxis=dict(hoverformat='.5f'),
yaxis=dict(hoverformat='.5f'),
mapbox=dict(
accesstoken=mapbox_access_token,
bearing=0,
center=dict(
lat=lat,
lon=lon
),
pitch=0,
zoom=10,
),
)
figure = {
'data': map_data,
'layout': map_layout
}
Dose anyone know how I can change my code to make my map looks like that?
I think what you're after is layout margins:
map_layout = go.Layout(
title='Bathymetrical Data',
autosize=True,
hovermode='closest',
margin=dict(t=0, b=0, l=0, r=0),
...
note that these margins affect the "inside" of the chart, you can also set the width and height parameters in the layout to change the size of the whole figure. For the width I strongly recommend using the css' grid on this css made for dash.

Can't add time to my X axis on my Plotly chart

I'm creating a Dash webapp where i have a real-time plotly chart. This chart gets updated every second. I would like to add to my X axis time instead of a fixed value. I tried setting tickvals= to dt.now but it doesn't work, since tickvals needs an array. Any advice?
def gen_wind_speed(interval):
trace = Scatter(
y=df['num'],
line=Line(
color='#42C4F7'
),
)
layout = Layout(
height=450,
xaxis=dict(
showgrid=False,
showline=False,
zeroline=False,
fixedrange=True,
tickvals= dt.now
),
margin=Margin(
t=45,
l=50,
r=50
)
)
return Figure(data=[trace], layout=layout)

Categories