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
Related
I would like to create a plotly plot where I can change the value of the plotly express color argument via a button. I am using plotly.express.scatter for this.
For example, the initial plot shown is px.scatter(df, "sepal_length", "sepal_width", color="species"). Changing from "species" to "petal length" in a dropdown menu would update the plot so that instead color="petal_length". If it makes a difference, "species" uses the default discrete color sequence while "petal_length" uses the default continuous color scale.
The code I have so far makes the initial plot and dropdown buttons, but choosing the buttons has no effect. I don't understand how to get the plotly express color argument passed through this Plotly.update interface.
import plotly.express as px
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")
fig = px.scatter(df, "sepal_length", "sepal_width", color="species")
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(
args=["color", "species"],
label="species",
method="update"
),
dict(
args=["color", "petal_length"],
label="petal length",
method="update"
),
]),
showactive=True,
x=0.05,
xanchor="left",
y=1.06,
yanchor="top"
),
]
)
fig.update_layout(
annotations=[
dict(text="color", x=0.015, xref="paper", y=1.05, yref="paper",
align="left", showarrow=False),
])
fig.show()
For color='species', three graphs are internally created for each categorical variable. And color='petal_length' consists of a single graph data. So, it is possible to handle this by setting the drop-down to show/hide the graph. The actual code reuses the data created by express.scatter. The data for each graph will be configured in a graph object; three will be shown and one will be hidden. Set the button to restyle as a function of the button.
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")
fig1 = px.scatter(df, "sepal_length", "sepal_width", color="species")
fig2 = px.scatter(df, "sepal_length", "sepal_width", color="petal_length")
fig = go.Figure()
fig.add_trace(go.Scatter(fig1.data[0], visible=True))
fig.add_trace(go.Scatter(fig1.data[1], visible=True))
fig.add_trace(go.Scatter(fig1.data[2], visible=True))
fig.add_trace(go.Scatter(fig2.data[0], visible=False))
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(
args=["visible", [True,True,True,False]],
label="species",
method="restyle"
),
dict(
args=["visible", [False,False,False,True]],
label="petal length",
method="restyle"
),
]),
showactive=True,
x=0.05,
xanchor="left",
y=1.2,
yanchor="top"
),
]
)
fig.update_layout(
annotations=[
dict(text="color", x=0.01, xref="paper", y=1.16, yref="paper",
align="left", showarrow=False),
])
fig.update_layout(xaxis_title_text='sepal_length', yaxis_title_text='sepal_width', legend_title_text='species')
fig.show()
I'm pretty new using plotly in python. I managed to plot a box chart with my dataframe in plotly like this:
box chart
The box plot shows the entire department's performance. I wish to add a few buttons that filter or narrow down the result. For example:
Team 1 button - filter on JH, DT, MB, SC
Team 2 button - filter on NP, DH, MZ, SB
Team 3 button - filter on KT, BL, SM,LW
and so on
I read through the plotly Figure reference (https://plotly.com/python/reference/layout/updatemenus/#layout-updatemenus-items-updatemenu-buttons-items-button-args)
and managed to add the buttons with args=["Claim_Handler"] where ["Claim_Handler"] is the column name in my dataframe. However the button does not perform any action when I click on it.
Where did I do wrong?
Here is the code for the graph:
fig2 = px.box(DF2, x='Claim_Handler', y='Days_to_close',hover_data=["Claim#"])
fig2.update_layout(
title='Average days to close for Claims Closed in last 5 years',
xaxis = dict(
rangeslider = dict(
visible=True,
thickness=0.05
)
),
yaxis = dict(
),
barmode='stack',
paper_bgcolor='#FFFFFF',
showlegend=True
)
fig2.update_layout(
updatemenus=[
dict(
type = "buttons",
direction = "left",
buttons=list([
dict(
args=["Claim_Handler"],
label="DH",
method="update"
),
dict(
args=["Claim_Handler"],
label="DT",
method="update"
)
])
),]
)
fig2.show(renderer="iframe")
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()
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.
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)