Reputation: 36
I would like to get a plot with more than two different y-axes in seaborn using a pandas dataframe similar to this example for matlotlib: https://matplotlib.org/examples/axes_grid/demo_parasite_axes2.html
As it will be used in a function I want to be flexible in selecting how many and which column of a Pandas dataframe will be ploted.
Unfortunately Seaborn seems to only move the last added scale. Here is what I want to do with a Seaborn sample dataset:
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import seaborn as sns
df=sns.load_dataset("mpg")
df=df.loc[df['model_year']<78]
show=['mpg','displacement','acceleration']
sns.set(rc={'figure.figsize':(11.7,8.27)})
sns.scatterplot('weight',show[0],data=df.reset_index(),style='model_year')
del show[0]
k=1
off=0
for i in show:
a = plt.twinx()
a=sns.scatterplot('weight',i,data=df.reset_index(),ax=a, color=list(mcolors.TABLEAU_COLORS)[k],legend=False,style='model_year')
a.spines['right'].set_position(('outward', off))
a.yaxis.label.set_color(list(mcolors.TABLEAU_COLORS)[k])
k+=1
off+=60
I want to create a function with the possibility to flexible plot different columns. Up to now this seems to be quite complicated in plotly to me (no way of just do a loop). I would also go with plotly, if there is a good way.
Upvotes: 2
Views: 1068
Reputation: 36
I now implemented this using plotly.
import seaborn as sns
import plotly.graph_objects as go
df=sns.load_dataset("mpg")
show=['mpg','displacement','acceleration']
mcolors=[
'#1f77b4', # muted blue
'#ff7f0e', # safety orange
'#2ca02c', # cooked asparagus green
'#d62728', # brick red
'#9467bd', # muted purple
'#8c564b', # chestnut brown
'#e377c2', # raspberry yogurt pink
'#7f7f7f', # middle gray
'#bcbd22', # curry yellow-green
'#17becf' # blue-teal
];
fig = go.Figure()
m=0
for k in df.model_year.unique():
fig.add_trace(go.Scatter(
x = df.loc[df.model_year == k]['weight'],
y = df.loc[df.model_year == k][show[0]],
name = str(k),
mode = 'markers',
marker_symbol=m,
marker_line_width=0,
marker_size=6,
marker_color=mcolors[0],
))
m+=1
layout = {'xaxis':dict(
domain=[0,0.7]
),
'yaxis':dict(
title=show[0],
titlefont=dict(
color=mcolors[0]
),
tickfont=dict(
color=mcolors[0]
),
showgrid=False
)}
n=2
for i in show[1::]:
m=0
for k in df.model_year.unique():
fig.add_trace(go.Scatter(
x = df.loc[df.model_year == k]['weight'],
y = df.loc[df.model_year == k][i],
name = str(k),
yaxis ='y'+str(n),
mode = 'markers',
marker_symbol=m,
marker_line_width=0,
marker_size=6,
marker_color=mcolors[n],
showlegend = False
))
m+=1
layout['yaxis'+str(n)] = dict(
title=i,
titlefont=dict(
color=mcolors[n]
),
tickfont=dict(
color=mcolors[n]
),
anchor="free",
overlaying="y",
side="right",
position=(n)*0.08+0.55,
showgrid=False,
)
n+=1
fig.update_layout(**layout)
fig.show()
Upvotes: 0
Reputation: 3232
There is actually a good way in Plotly, you can see the code example for the picture below, similar to your matplotlib example in this section of the docs.
Upvotes: 1