Drop down menu for Plotly graph - python

Here is my dataframe:
df = pd.DataFrame({"Date":["2020-01-27","2020-02-27","2020-03-27","2020-04-27", "2020-05-27", "2020-06-27", "2020-07-27",
"2020-01-27","2020-02-27","2020-03-27","2020-04-27", "2020-05-27", "2020-06-27", "2020-07-27"],
"A_item":[2, 8, 0, 1, 8, 10, 4, 7, 2, 15, 5, 12, 10, 7],
"B_item":[1, 7, 10, 6, 5, 9, 2, 5, 6, 1, 2, 6, 15, 8],
"C_item":[9, 2, 9, 3, 9, 18, 7, 2, 8, 1, 2, 8, 1, 3],
"Channel_type":["Chanel_1", "Chanel_1", "Chanel_1", "Chanel_1", "Chanel_1", "Chanel_1", "Chanel_1",
"Chanel_2", "Chanel_2", "Chanel_2", "Chanel_2", "Chanel_2", "Chanel_2", "Chanel_2"]
})
I want to plot a group Bar chart with the dropdown filter on the Channel_type col. That's what I am trying:
trace2 = go.Bar(x=df["Date"], y=df[["B_item"]])
trace3 = go.Bar(x=df["Date"], y=df[["C_item"]])
list_updatemenus = [{'label': 'All',
'method': 'update',
'args': [{'visible': [True, True]}, {'title': 'All'}]},
{'label': 'Chanel_1',
'method': 'update',
'args': [{'visible': [True, False]}, {'title': 'Chanel_1'}]},
{'label': 'Chanel_2',
'method': 'update',
'args': [{'visible': [False, True]}, {'title': 'Chanel_2'}]}]
data = [trace1,trace2,trace3]
layout=go.Layout(title='Distribution of Sales by Region',updatemenus=list([dict(buttons= list_updatemenus)]),width=1000,height=800,barmode='group')
fig = go.Figure(data,layout)
fig.show()
And not getting the desired output:Plot 1
As it filters the graph by the "A_item", "B_item" and "C_item" while I would like to filter it by the Channel_type col as mentioned.
So the ideal result would be the below graph, but with the dropdown menu that changes the graph based on Channel_type :
Plot 2
I am able to solve the problem with Ipywidgets in the Jupyter notebook, but it’s not really working for my particular task. Here is the code:
from plotly import graph_objs as go
import ipywidgets as w
from IPython.display import display
x = 'Date'
y1 = 'A_item'
y2 = 'B_item'
y3 = 'C_item'
trace1 = {
'x': df[x],
'y': df[y1],
'type': 'bar',
'name':'A_item'
}
trace2={
'x': df[x],
'y': df[y2],
'type': 'bar',
'name':'B_item'
}
trace3 = {
'x': df[x],
'y': df[y3],
'type': 'bar',
'name':'C_item',
}
data = [trace1, trace2, trace3]
# Create layout for the plot
layout=dict(
title='Channels',
width=1200, height=700, title_x=0.5,
paper_bgcolor='#fff',
plot_bgcolor="#fff",
xaxis=dict(
title='Date',
type='date',
tickformat='%Y-%m-%d',
gridcolor='rgb(255,255,255)',
zeroline= False,
),
yaxis=dict(
title='My Y-axis',
zeroline= False
)
)
fig = go.FigureWidget(data=data, layout=layout)
def update_fig(change):
aux_df = df[df.Channel_type.isin(change['new'])]
with fig.batch_update():
for trace, column in zip(fig.data, [y1, y2, y3]):
trace.x = aux_df[x]
trace.y = aux_df[column]
drop = w.Dropdown(options=[
('All', ['Chanel_1', 'Chanel_2']),
('Chanel_1', ['Chanel_1']),
('Chanel_2', ['Chanel_2']),
])
drop.observe(update_fig, names='value')
display(w.VBox([drop, fig]))
And here is the output:
The problem is that I am not able to wrap the VBox into an HTML file and save the dropdown menu. Also, it isn’t working in the Python shell as it is intended for the Jupyter notebook, and I need to share it.
So the ideal result would be to wrap the last figure within the Plotly fig only without the ipywidgets.
Any help be really appreciated!
Thank you!

The most important thing to note is that for go.Bar, if you have n dates in the x parameter and you pass a 2D array of dimension (m, n) to the y parameter of go.Bar, Plotly understands to create a grouped bar chart with each date n having m bars.
For your DataFrame, something like df[df['Channel_type'] == "Channel_1"][items].T.values will reshape it as needed. So we can apply this to the y field of args that we pass the to the buttons we make.
Credit to #vestland for the portion of the code making adjustments to the buttons to make it a dropdown.
import pandas as pd
import plotly.graph_objects as go
df = pd.DataFrame({"Date":["2020-01-27","2020-02-27","2020-03-27","2020-04-27", "2020-05-27", "2020-06-27", "2020-07-27",
"2020-01-27","2020-02-27","2020-03-27","2020-04-27", "2020-05-27", "2020-06-27", "2020-07-27"],
"A_item":[2, 8, 0, 1, 8, 10, 4, 7, 2, 15, 5, 12, 10, 7],
"B_item":[1, 7, 10, 6, 5, 9, 2, 5, 6, 1, 2, 6, 15, 8],
"C_item":[9, 2, 9, 3, 9, 18, 7, 2, 8, 1, 2, 8, 1, 3],
"Channel_type":["Channel_1", "Channel_1", "Channel_1", "Channel_1", "Channel_1", "Channel_1", "Channel_1",
"Channel_2", "Channel_2", "Channel_2", "Channel_2", "Channel_2", "Channel_2", "Channel_2"]
})
fig = go.Figure()
colors = ['#636efa','#ef553b','#00cc96']
items = ["A_item","B_item","C_item"]
for item, color in zip(items, colors):
fig.add_trace(go.Bar(
x=df["Date"], y=df[item], marker_color=color
))
# one button for each df column
# slice the DataFrame and apply transpose to reshape it correctly
updatemenu= []
buttons=[]
for channel in df['Channel_type'].unique():
buttons.append(dict(method='update',
label=channel,
args=[{
'y': df[df['Channel_type'] == channel][items].T.values
}])
)
## add a button for both channels
buttons.append(dict(
method='update',
label='Both Channels',
args=[{
'y': df[items].T.values
}])
)
# some adjustments to the updatemenu
# from code by vestland
updatemenu=[]
your_menu=dict()
updatemenu.append(your_menu)
updatemenu[0]['buttons']=buttons
updatemenu[0]['direction']='down'
updatemenu[0]['showactive']=True
fig.update_layout(updatemenus=updatemenu)
fig.show()

Related

Drawing custom error bars when using plotly subplots

This question is closely related to an earlier one that I posted. I would like to draw confidence intervals for each bar within subplots of a figure, using the information from two columns in my data frame describing the upper and lower limit of each confidence interval. I tried to use the solution from that earlier post, but it does not seem to be applicable when one wants to use different colors and/or different rows in order to draw subplots for the figure.
For example, the following code does not produce the right confidence intervals. For instance, the CI of the 3rd bar in the second row should go from 11 to 5:
import pandas as pd
import plotly.express as px
df = pd.DataFrame(
{"x": [0, 1, 2, 3, 0, 1, 2, 3],
"y": [6, 10, 2, 5, 8, 9, 10, 11],
"ci_upper": [8, 11, 2.5, 4, 9, 10, 11, 12],
"ci_lower": [5, 9, 1.5, 3, 7, 6, 5, 10],
"state": ['foo','foo','foo','foo','bar','bar','bar','bar'],
"color": ['0','0','1','1','0','0','1','1']}
)
fig = px.bar(df, x="x", y="y",facet_row='state',color='color').update_traces(
error_y={
"type": "data",
"symmetric": False,
"array": df["ci_upper"] - df["y"],
"arrayminus": df["y"] - df["ci_lower"],
}
)
fig.update_yaxes(dtick=1)
fig.show(renderer='png')
it's the same technique but solution needs to consider it's multiple traces (4 in this example)
encoded in hovertemplate of each trace are the facet and color. Extract these and filter data down to appropriate rows
then build instruction for error bars as with simpler condition
import pandas as pd
import plotly.express as px
df = pd.DataFrame(
{
"x": [0, 1, 2, 3, 0, 1, 2, 3],
"y": [6, 10, 2, 5, 8, 9, 10, 11],
"ci_upper": [8, 11, 2.5, 4, 9, 10, 11, 12],
"ci_lower": [5, 9, 1.5, 3, 7, 6, 5, 10],
"state": ["foo", "foo", "foo", "foo", "bar", "bar", "bar", "bar"],
"color": ["0", "0", "1", "1", "0", "0", "1", "1"],
}
)
fig = px.bar(df, x="x", y="y", facet_row="state", color="color")
fig.update_yaxes(dtick=1)
def error_facet(t):
# filter data frame based on contents of hovertemplate
d = df.query(
" and ".join(
[
f"{q.split('=')[0]}==\"{q.split('=')[1]}\""
for q in t.hovertemplate.split("<br>")[0:2]
]
)
)
t.update(
{
"error_y": {
"type": "data",
"symmetric": False,
"array": d["ci_upper"] - d["y"],
"arrayminus": d["y"] - d["ci_lower"],
}
}
)
fig.for_each_trace(error_facet)
fig

Dash in python - callback error about scatter plot and dropdown

I'm studying dash library.
This code showing the scatter plot when i select column in the data frame.
This works without any problem, but call back error occurs on the web page.
on the web, callback error updating spas-graph.figure
i can't understand why this error occurs.
[import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
import pandas as pd
df = pd.DataFrame({
'depth' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'upper_value' : [1, 4, 6, 2, 6, 8, 9, 10, 4, 2],
'middle_value' : [5, 3, 7, 8, 1, 2, 3, 1, 4, 8],
'down_value' : [6, 2, 1, 10, 5, 2, 3, 4, 2, 7]
})
col_list = df.columns[1:4]
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Dropdown(
id = 'select-cd',
options = [
{'label' : i, 'value' : i}
for i in col_list
]
),
dcc.Graph(id = 'spas-graph')
])
#app.callback(
Output('spas-graph', 'figure'),
[Input('select-cd', 'value')]
)
def update_figure(selected_col):
return {
'data' : [go.Scatter(
x = df[selected_col],
y = df['depth'],
mode = 'lines + markers',
marker = {
'size' : 15,
'opacity' : 0.5,
'line' : {'width' : 0.5, 'color' : 'white'}
}
)],
'layout' : go.Layout(
xaxis={'title': 'x_scale'},
yaxis={'title': 'y_scale'},
hovermode='closest'
)
}
if __name__ == '__main__':
app.run_server(debug=True)
You have not defined the value parameter in your dropdown method. So when the server starts the first input it picks up is a None value.
You can solve it in two ways:
Add a default value in the Dropdown:
Handle None value in the callback method
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
import pandas as pd
df = pd.DataFrame({
'depth' : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'upper_value' : [1, 4, 6, 2, 6, 8, 9, 10, 4, 2],
'middle_value' : [5, 3, 7, 8, 1, 2, 3, 1, 4, 8],
'down_value' : [6, 2, 1, 10, 5, 2, 3, 4, 2, 7]
})
col_list = df.columns[1:4]
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Dropdown(
id = 'select-cd',
options = [
{'label' : i, 'value' : i}
for i in col_list
],
value = col_list[0]
),
dcc.Graph(id = 'spas-graph')
])
#app.callback(
Output('spas-graph', 'figure'),
[Input('select-cd', 'value')]
)
def update_figure(selected_col):
if selected_col is None:
selected_col = col_list[0]
return {
'data' : [go.Scatter(
x = df[selected_col],
y = df['depth'],
mode = 'lines + markers',
marker = {
'size' : 15,
'opacity' : 0.5,
'line' : {'width' : 0.5, 'color' : 'white'}
}
)],
'layout' : go.Layout(
xaxis={'title': 'x_scale'},
yaxis={'title': 'y_scale'},
hovermode='closest'
)
}

Plotly: How to change variable/label names for the legend in a plotly express line chart?

I want to change the variable/label names in plotly express in python. I first create a plot:
import pandas as pd
import plotly.express as px
d = {'col1': [1, 2, 3], 'col2': [3, 4, 5]}
df = pd.DataFrame(data=d)
fig = px.line(df, x=df.index, y=['col1', 'col2'])
fig.show()
Which yields:
I want to change the label names from col1 to hello and from col2 to hi. I have tried using labels in the figure, but I cannot get it to work:
fig = px.line(df, x=df.index, y=['col1', 'col2'], labels={'col1': "hello", 'col2': "hi"})
fig.show()
But this seems to do nothing, while not producing an error. Obviously I could achieve my goals by changing the column names, but the actual plot i'm trying to create doesn't really allow for that since it comes from several different dataframes.
The answer:
Without changing the data source, a complete replacement of names both in the legend, legendgroup and hovertemplate will require:
newnames = {'col1':'hello', 'col2': 'hi'}
fig.for_each_trace(lambda t: t.update(name = newnames[t.name],
legendgroup = newnames[t.name],
hovertemplate = t.hovertemplate.replace(t.name, newnames[t.name])
)
)
Plot:
The details:
Using
fig.for_each_trace(lambda t: t.update(name = newnames[t.name]))
...you can change the names in the legend without ghanging the source by using a dict
newnames = {'col1':'hello', 'col2': 'hi'}
...and map new names to the existing col1 and col2 in the following part of the figure structure (for your first trace, col1):
{'hovertemplate': 'variable=col1<br>index=%{x}<br>value=%{y}<extra></extra>',
'legendgroup': 'col1',
'line': {'color': '#636efa', 'dash': 'solid'},
'mode': 'lines',
'name': 'hello', # <============================= here!
'orientation': 'v',
'showlegend': True,
'type': 'scatter',
'x': array([0, 1, 2], dtype=int64),
'xaxis': 'x',
'y': array([1, 2, 3], dtype=int64),
'yaxis': 'y'},
But as you can see, this doesn't do anything with 'legendgroup': 'col1', nor 'hovertemplate': 'variable=col1<br>index=%{x}<br>value=%{y}<extra></extra>' And depending on the complexity of your figure, this can pose a problem. So I would add legendgroup = newnames[t.name] and hovertemplate = t.hovertemplate.replace(t.name, newnames[t.name])into the mix.
Complete code:
import pandas as pd
import plotly.express as px
from itertools import cycle
d = {'col1': [1, 2, 3], 'col2': [3, 4, 5]}
df = pd.DataFrame(data=d)
fig = px.line(df, x=df.index, y=['col1', 'col2'])
newnames = {'col1':'hello', 'col2': 'hi'}
fig.for_each_trace(lambda t: t.update(name = newnames[t.name],
legendgroup = newnames[t.name],
hovertemplate = t.hovertemplate.replace(t.name, newnames[t.name])
)
)
Add the "name" parameter: go.Scatter(name=...)
Source https://plotly.com/python/figure-labels/
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
y=[0, 1, 2, 3, 4, 5, 6, 7, 8],
name="Name of Trace 1" # this sets its legend entry
))
fig.add_trace(go.Scatter(
x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
y=[1, 0, 3, 2, 5, 4, 7, 6, 8],
name="Name of Trace 2"
))
fig.update_layout(
title="Plot Title",
xaxis_title="X Axis Title",
yaxis_title="X Axis Title",
legend_title="Legend Title",
font=dict(
family="Courier New, monospace",
size=18,
color="RebeccaPurple"
)
)
fig.show()
This piece of code is more concise.
import pandas as pd
import plotly.express as px
df = pd.DataFrame(data={'col1': [1, 2, 3], 'col2': [3, 4, 5]})
series_names = ["hello", "hi"]
fig = px.line(data_frame=df)
for idx, name in enumerate(series_names):
fig.data[idx].name = name
fig.data[idx].hovertemplate = name
fig.show()
If you're looking for something even more concise, this function does the job-
def custom_legend_name(new_names):
for i, new_name in enumerate(new_names):
fig.data[i].name = new_name
Then before fig.show(), just pass a list consisting of the names you want, to the function, like this custom_legend_name(['hello', 'hi'])
Here's what the complete code would look like-
def custom_legend_name(new_names):
for i, new_name in enumerate(new_names):
fig.data[i].name = new_name
import pandas as pd
import plotly.express as px
d = {'col1': [1, 2, 3], 'col2': [3, 4, 5]}
df = pd.DataFrame(data=d)
fig = px.line(df, x=df.index, y=['col1', 'col2'])
custom_legend_name(['hello','hi'])
fig.show()

Why does setting hue in seaborn plot change the size of a point?

The plot I am trying to make needs to achieve 3 things.
If a quiz is taken on the same day with the same score, that point needs to be bigger.
If two quiz scores overlap there needs to be some jitter so we can see all points.
Each quiz needs to have its own color
Here is how I am going about it.
import seaborn as sns
import pandas as pd
data = {'Quiz': [1, 1, 2, 1, 2, 1],
'Score': [7.5, 5.0, 10, 10, 10, 10],
'Day': [2, 5, 5, 5, 11, 11],
'Size': [115, 115, 115, 115, 115, 355]}
df = pd.DataFrame.from_dict(data)
sns.lmplot(x = 'Day', y='Score', data = df, fit_reg=False, x_jitter = True, scatter_kws={'s': df.Size})
plt.show()
Setting the hue, which almost does everything I need, results in this.
import seaborn as sns
import pandas as pd
data = {'Quiz': [1, 1, 2, 1, 2, 1],
'Score': [7.5, 5.0, 10, 10, 10, 10],
'Day': [2, 5, 5, 5, 11, 11],
'Size': [115, 115, 115, 115, 115, 355]}
df = pd.DataFrame.from_dict(data)
sns.lmplot(x = 'Day', y='Score', data = df, fit_reg=False, hue = 'Quiz', x_jitter = True, scatter_kws={'s': df.Size})
plt.show()
Is there a way I can have hue while keeping the size of my points?
It doesn't work because when you are using hue, seaborn does two separate scatterplots and therefore the size argument you are passing using scatter_kws= no longer aligns with the content of the dataframe.
You can recreate the same effect by hand however:
x_col = 'Day'
y_col = 'Score'
hue_col = 'Quiz'
size_col = 'Size'
jitter=0.2
fig, ax = plt.subplots()
for q,temp in df.groupby(hue_col):
n = len(temp[x_col])
x = temp[x_col]+np.random.normal(scale=0.2, size=(n,))
ax.scatter(x,temp[y_col],s=temp[size_col], label=q)
ax.set_xlabel(x_col)
ax.set_ylabel(y_col)
ax.legend(title=hue_col)

python - Plotting bar graph side by side on the same graph with seaborn

I need to try to plot 3 bars on the same graph. I have 2 dataframes set up right now. My first dataframe was created off a JSON file seen here.
My second dataframe was created in the code below:
def make_bar_graph():
with open('filelocation.json') as json_file:
data = json.load(json_file)
df = pd.DataFrame([])
for item in data["Results"]["Result"]:
df = df.append(pd.DataFrame.from_dict(kpi for kpi in item["KPI"]))
df.reset_index(level=0, inplace= True)
df.rename(columns={0: 'id', 1: 'average', 2:'std. dev', 3: 'min', 4:
'median', 5:'max'}, inplace=True)
wanted_x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
wanted_y = [5, 5, .500, .500, .500, 1, 1, 5, 5, .500, .500, .500, 1, 1]
kpi = ['kpi1', 'kpi2', 'kpi3', 'kpi4', 'kpi5', 'kpi6', 'kpi7', 'kpi8', 'kpi9', 'kpi10', 'kpi11', 'kpi12',
'kpi13', 'kpi14']
df2 = pd.DataFrame(dict(x=wanted_x, y=wanted_y, kpi=kpi))
sns.set()
sns.set_context("talk")
sns.axes_style("darkgrid")
h = sns.barplot(x='id', y ='average', data=df.ix[0:13], label='Test
on 4/30/2018', color='b')
g = sns.barplot(x='id', y='average', data=df.ix[14:27], label='Test
on 6/4/2018', color='r')
k = sns.barplot("x", "y", data=df2, label='Desired Results', color='y')
plt.legend()
plt.xlabel('KPI number')
plt.ylabel('Time(s)')
plt.show()
This is the graph I get from that:
Graph1
I need the bars to be next to each other, separated by id (or KPI, id number and KPI number are the same things). I'm not sure how to rework my dataframe to do this

Categories

Resources