Like ggplot2, can we have separate legends for Color, Symbol, etc. for a Plotly Express visualization?
from pydataset import data
import plotly.express as px
mtcars = data('mtcars')
mtcars.am = mtcars.am.astype('category')
mtcars.gear = mtcars.gear.astype('category')
plt = px.scatter(mtcars, x = 'mpg', y='qsec', color ='disp', symbol = 'am', size = 'wt')
plt.show()
Here, no separate legends are generated.
Also, If one of the legends has continuous value strip, then other legends become invisible.
Now, compare this with GGPLOT (I have used python package plotnine):
ggplot(mtcars, aes(x = 'mpg', y='qsec', color ='disp', shape = 'am', size = 'wt')) + geom_point()
So, simple and complete. How can we get an output like this using Plotly?
Thank you in advance.
I think your latest attempt looks pretty good. And personally I don't see the need for the size of legend elements to reflect sizes in the figure itself as long as the details otherwise are clear. Here's a little setup to adjust your legend
and colorbar
:
fig.layout.legend.y = 1.05
fig.layout.legend.x = 1.035
fig.layout.coloraxis.colorbar.y = 0.35
from pydataset import data
import plotly.express as px
mtcars = data('mtcars')
mtcars.am = mtcars.am.astype('category')
mtcars.gear = mtcars.gear.astype('category')
fig = px.scatter(mtcars, x = 'mpg', y='qsec', color ='disp', symbol = 'am', size = 'wt')
fig.layout.legend.y = 1.05
fig.layout.legend.x = 1.035
fig.layout.coloraxis.colorbar.y = 0.35
fig.show()