Plotly Express - ho to plot a stacked bar chart of single variable - python

I have a dataframe like this
df = pd.DataFrame({'name':['a', 'b', 'c', 'd', 'e'], 'value':[54.2, 53.239, 43.352, 36.442, -12.487]})
df
I'd like to plot a simple stacked bar chart like the one below whit plotly.express
How can a I do that?
I've seen on documentation several examples but none of them solved my problem
Thank you

It's a little wordy, but you can set a single value for the x axis, in this case zero. Then you just need to tweak your dimension, lables, and ranges.
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'name':['a', 'b', 'c', 'd', 'e'], 'value':[54.2, 53.239, 43.352, 36.442, -12.487]})
df['x'] = 0
fig = px.bar(df, x='x', y='value',color='name', width=500, height=1000)
fig.update_xaxes(showticklabels=False, title=None)
fig.update_yaxes(range=[-50,200])
fig.update_traces(width=.3)
fig.show()

The bar chart's only ever going to have one column? That seems like an odd use-case for a bar chart, but...
What I would do is create one trace per "name", filtering df as trace_df=df[df['name']==name], and then make a Bar for each of those, something like this:
import plotly.graph_objects as go
trace_dfs = [df[df['name']==name] for name in df['name']]
bars = [
go.Bar(
name=name,
x=['constant' for _ in trace_frame['value']],
y=trace_frame['value'],
)
for trace_frame in trace_dfs
]
fig = go.Figure(
data=bars,
barmode='stack'
)
Granted, that's not plotly_express, it's core plotly, which allows a lot more control. If you want multiple stacked bars for different values, you'll need separate labels and separate values for x and y, not the two-column DF you described. There are several more examples here and a full description of the available bar chart options here.

Related

Plotly scatter matrix without all rows

I want to create a scatter plot matrix (matrix of scatter plots of multiple variables to see the correlation between each pair). However, I would like to remove some variables from the rows (but keep them in the columns).
With the following code, I'm able to get the complete scatter plot matrix (with all variables):
import numpy as np
import pandas as pd
import plotly.graph_objects as go
df = pd.DataFrame(
np.random.randn(1000, 5),
columns=['A', 'B', 'C', 'M1', 'M2']
)
fig = go.Figure(
data=go.Splom(
dimensions=[dict(label=c, values=df[c]) for c in df.columns],
text=df.index,
marker=dict(
size=3,
color=df['M1'],
colorscale='Bluered',
),
)
)
fig.show()
I would like to have the same plot, but only with the rows corresponding to M1 and M2, something like that:
Is this possible with plotly?
Note: I want to get an interactive HTML output, so just cropping the image won't work in this case.

Plotly Express - plot subset of dataframe columns by default and the rest as option

I am using plotly express to plot figures this way :
fig = px.line(df,
x=df.index,
y=df.columns
)
It displays the graph properly and shows all the columns by default (as lines in the graph) with option to uncheck (or check) them to disable showing whatever we want if needed.
What I would like is to show the same graph but by default uncheking some of the columns initially and keep the option to check or uncheck them for visualization.
This means that I cannot take only a subset of columns as new data frame to show as the other columns are still relevant.
Did not find anything in the documentation unfortunately...
Thank you in advance.
You can use the visible property of the traces to state it is only in the legend. Below shows all columns in the figure then first two columns are set as visible, all other columns are only in the legend.
import plotly.express as px
import pandas as pd
import numpy as np
# simulate dataframe
df = pd.DataFrame(
{c: np.random.uniform(0, 1, 100) + cn for cn, c in enumerate("ABCDEF")}
)
fig = px.line(df, x=df.index, y=df.columns)
# for example only display first two columns of data frame, all others can be displayed
# by clicking on legend item
fig.for_each_trace(
lambda t: t.update(visible=True if t.name in df.columns[:2] else "legendonly")
)

How to set specific color to some bars in a plotly bar graph?

I'm trying to set different colors for some bars in a plotly express bar graph:
import plotly.express as px
import pandas as pd
data = {'Name':['2020/01', '2020/02', '2020/03', '2020/04',
'2020/05', '2020/07', '2020/08'],
'Value':[34,56,66,78,99,55,22]}
df = pd.DataFrame(data)
color_discrete_sequence = ['#ec7c34']*len(df)
color_discrete_sequence[5] = '#609cd4'
fig=px.bar(df,x='Name',y='Value',color_discrete_sequence=color_discrete_sequence)
fig.show()
My expectations were that one (the sixth one) bar had a different color, however I got this result:
What am I doing wrong?
This happens because color in px.bar is used to name a category to illustrate traits or dimensions of a dataset using a colorscale. Or in you your case, rather a color cycle since you're dealing with a categorical / discrete case. color_discrete_sequence is then used to specify which color sequence to follow. One way to achieve your goal using your setup here, is to simply define a string variable with unique values, for example like df['category'] [str(i) for i in df.index], and then use:
fig=px.bar(df,x='Name',y='Value',
color = 'category',
color_discrete_sequence=color_discrete_sequence,
)
Plot:
If df['category'] is a numerical value, color_discrete_sequence will be ignored, and a default continuous sequence will be applied:
If anything else is unclear, don't hesitate to let me know.
Complete code:
import plotly.express as px
import pandas as pd
data = {'Name':['2020/01', '2020/02', '2020/03', '2020/04',
'2020/05', '2020/07', '2020/08'],
'Value':[34,56,66,78,99,55,22]}
df = pd.DataFrame(data)
df['category'] = [str(i) for i in df.index]
# df['category'] = df.index
color_discrete_sequence = ['#ec7c34']*len(df)
color_discrete_sequence[5] = '#609cd4'
fig=px.bar(df,x='Name',y='Value',
color = 'category',
color_discrete_sequence=color_discrete_sequence,
)
fig.show()

How do I resize my Plotly bar height and show only bar’s edge (in subplot)?

this is my first foray into Plotly. I love the ease of use compared to matplotlib and bokeh. However I'm stuck on some basic questions on how to beautify my plot. First, this is the code below (its fully functional, just copy and paste!):
import plotly.express as px
from plotly.subplots import make_subplots
import plotly as py
import pandas as pd
from plotly import tools
d = {'Mkt_cd': ['Mkt1','Mkt2','Mkt3','Mkt4','Mkt5','Mkt1','Mkt2','Mkt3','Mkt4','Mkt5'],
'Category': ['Apple','Orange','Grape','Mango','Orange','Mango','Apple','Grape','Apple','Orange'],
'CategoryKey': ['Mkt1Apple','Mkt2Orange','Mkt3Grape','Mkt4Mango','Mkt5Orange','Mkt1Mango','Mkt2Apple','Mkt3Grape','Mkt4Apple','Mkt5Orange'],
'Current': [15,9,20,10,20,8,10,21,18,14],
'Goal': [50,35,21,44,20,24,14,29,28,19]
}
dataset = pd.DataFrame(d)
grouped = dataset.groupby('Category', as_index=False).sum()
data = grouped.to_dict(orient='list')
v_cat = grouped['Category'].tolist()
v_current = grouped['Current']
v_goal = grouped['Goal']
fig1 = px.bar(dataset, x = v_current, y = v_cat, orientation = 'h',
color_discrete_sequence = ["#ff0000"],height=10)
fig2 = px.bar(dataset, x = v_goal, y = v_cat, orientation = 'h',height=15)
trace1 = fig1['data'][0]
trace2 = fig2['data'][0]
fig = make_subplots(rows = 1, cols = 1, shared_xaxes=True, shared_yaxes=True)
fig.add_trace(trace2, 1, 1)
fig.add_trace(trace1, 1, 1)
fig.update_layout(barmode = 'overlay')
fig.show()
Here is the Output:
Question1: how do I make the width of v_current (shown in red bar) smaller? As in, it should be smaller in height since this is a horizontal bar. I added the height as 10 for trace1 and 15 for trace2, but they are still showing at the same heights.
Question2: Is there a way to make the v_goal (shown in blue bar) only show it's right edge, instead of a filled out bar? Something like this:
If you noticed, I also added a line under each of the category. Is there a quick way to add this as well? Not a deal breaker, just a bonus. Other things I'm trying to do is add animation, etc but that's for some other time!
Thanks in advance for answering!
Running plotly.express wil return a plotly.graph_objs._figure.Figure object. The same goes for plotly.graph_objects running go.Figure() together with, for example, go.Bar(). So after building a figure using plotly express, you can add lines or traces through references directly to the figure, like:
fig['data'][0].width = 0.4
Which is exactly what you need to set the width of your bars. And you can easily use this in combination with plotly express:
Code 1
fig = px.bar(grouped, y='Category', x = ['Current'],
orientation = 'h', barmode='overlay', opacity = 1,
color_discrete_sequence = px.colors.qualitative.Plotly[1:])
fig['data'][0].width = 0.4
Plot 1
In order to get the bars or shapes to indicate the goal levels, you can use the approach described by DerekO, or you can use:
for i, g in enumerate(grouped.Goal):
fig.add_shape(type="rect",
x0=g+1, y0=grouped.Category[i], x1=g, y1=grouped.Category[i],
line=dict(color='#636EFA', width = 28))
Complete code:
import plotly.express as px
from plotly.subplots import make_subplots
import plotly as py
import pandas as pd
from plotly import tools
d = {'Mkt_cd': ['Mkt1','Mkt2','Mkt3','Mkt4','Mkt5','Mkt1','Mkt2','Mkt3','Mkt4','Mkt5'],
'Category': ['Apple','Orange','Grape','Mango','Orange','Mango','Apple','Grape','Apple','Orange'],
'CategoryKey': ['Mkt1Apple','Mkt2Orange','Mkt3Grape','Mkt4Mango','Mkt5Orange','Mkt1Mango','Mkt2Apple','Mkt3Grape','Mkt4Apple','Mkt5Orange'],
'Current': [15,9,20,10,20,8,10,21,18,14],
'Goal': [50,35,21,44,20,24,14,29,28,19]
}
dataset = pd.DataFrame(d)
grouped = dataset.groupby('Category', as_index=False).sum()
fig = px.bar(grouped, y='Category', x = ['Current'],
orientation = 'h', barmode='overlay', opacity = 1,
color_discrete_sequence = px.colors.qualitative.Plotly[1:])
fig['data'][0].width = 0.4
fig['data'][0].marker.line.width = 0
for i, g in enumerate(grouped.Goal):
fig.add_shape(type="rect",
x0=g+1, y0=grouped.Category[i], x1=g, y1=grouped.Category[i],
line=dict(color='#636EFA', width = 28))
f = fig.full_figure_for_development(warn=False)
fig.show()
You can use Plotly Express and then directly access the figure object as #vestland described, but personally I prefer to use graph_objects to make all of the changes in one place.
I'll also point out that since you are stacking bars in one chart, you don't need subplots. You can create a graph_object with fig = go.Figure() and add traces to get stacked bars, similar to what you already did.
For question 1, if you are using go.Bar(), you can pass a width parameter. However, this is in units of the position axis, and since your y-axis is categorical, width=1 will fill the entire category, so I have chosen width=0.25 for the red bar, and width=0.3 (slightly larger) for the blue bar since that seems like it was your intention.
For question 2, the only thing that comes to mind is a hack. Split the bars into two sections (one with height = original height - 1), and set its opacity to 0 so that it is transparent. Then place down bars of height 1 on top of the transparent bars.
If you don't want the traces to show up in the legend, you can set this individually for each bar by passing showlegend=False to fig.add_trace, or hide the legend entirely by passing showlegend=False to the fig.update_layout method.
import plotly.express as px
import plotly.graph_objects as go
# from plotly.subplots import make_subplots
import plotly as py
import pandas as pd
from plotly import tools
d = {'Mkt_cd': ['Mkt1','Mkt2','Mkt3','Mkt4','Mkt5','Mkt1','Mkt2','Mkt3','Mkt4','Mkt5'],
'Category': ['Apple','Orange','Grape','Mango','Orange','Mango','Apple','Grape','Apple','Orange'],
'CategoryKey': ['Mkt1Apple','Mkt2Orange','Mkt3Grape','Mkt4Mango','Mkt5Orange','Mkt1Mango','Mkt2Apple','Mkt3Grape','Mkt4Apple','Mkt5Orange'],
'Current': [15,9,20,10,20,8,10,21,18,14],
'Goal': [50,35,21,44,20,24,14,29,28,19]
}
dataset = pd.DataFrame(d)
grouped = dataset.groupby('Category', as_index=False).sum()
data = grouped.to_dict(orient='list')
v_cat = grouped['Category'].tolist()
v_current = grouped['Current']
v_goal = grouped['Goal']
fig = go.Figure()
## you have a categorical plot and the units for width are in position axis units
## therefore width = 1 will take up the entire allotted space
## a width value of less than 1 will be the fraction of the allotted space
fig.add_trace(go.Bar(
x=v_current,
y=v_cat,
marker_color="#ff0000",
orientation='h',
width=0.25
))
## you can show the right edge of the bar by splitting it into two bars
## with the majority of the bar being transparent (opacity set to 0)
fig.add_trace(go.Bar(
x=v_goal-1,
y=v_cat,
marker_color="#ffffff",
opacity=0,
orientation='h',
width=0.30,
))
fig.add_trace(go.Bar(
x=[1]*len(v_cat),
y=v_cat,
marker_color="#1f77b4",
orientation='h',
width=0.30,
))
fig.update_layout(barmode='relative')
fig.show()

How to efficiently plot a large number of line shapes where the points are connected two by two?

I need to plot a very large number of segments with plotly. Contrary to a regular scatter plot where all points can be connected, here I need to only connect points two by two.
I considered different options:
adding line shapes to the plot; apparently relatively slow
creating a large number of line plots with only two points
Would there be a more suitable method? Possibly a single scatter plot where only every other couple of points are connected.
I'm looking for an efficient way to produce the plot in Python but also for good rendering performances.
This answer builds on the suggestion in the comment from Maximilian Peters, as well as Jezraels approach to insert a new row after every nth row.
A key part is also to include fig.update_traces(connectgaps=False)
Plot:
Complete code:
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
# dataframe, sample
np.random.seed(123)
cols = ['a','b','c', 'd', 'e', 'f', 'g']
X = np.random.randn(50,len(cols))
df=pd.DataFrame(X, columns=cols)
df=df.cumsum()
df['id']=df.index
# dataframe with every nth row containing np.nan
df2 = (df.iloc[1::2]
.assign(id = lambda x: x['id'] + 1, c = np.nan)
.rename(lambda x: x + .5))
df1 = pd.concat([df, df2], sort=False).sort_index().reset_index(drop=True)
df1.loc[df1.isnull().any(axis=1), :] = np.nan
df1
# plotly figure
colors = px.colors.qualitative.Plotly
fig = go.Figure()
for i, col in enumerate(df1.columns[:-1]):
fig.add_traces(go.Scatter(x=df1.index, y=df1[col],
mode='lines+markers', line=dict(color=colors[i])))
fig.update_traces(connectgaps=False)
fig.show()

Categories

Resources