Issue
I am trying to adjust the legend markers in a Seaborn jointplot. Based on the answer by "MaozGelbart" on this Github issue, I expected rcParams
to work for this. The configurations are in the matplotib documentation here, and I think legend.markerscale
is the parameter I am interested in.
How can I change the legend marker scale? This code below does not work. However, it does work for the legend.fontsize
parameter.
import pandas as pd
import seaborn as sns
d = {
'x': [3,2,5,1,1,0],
'y': [1,1,2,3,0,2],
'cat': ['a','a','a','b','b','b']
}
df = pd.DataFrame(d)
with sns.plotting_context(rc={"legend.fontsize": 'x-small', "legend.markerscale": 0.5}):
g = sns.jointplot(data=df, x='x', y='y', hue='cat')
There is a similar Stack Overflow question here but it looks like there is no clear answer. Besides, setting rcParams
should work, in theory
Solution
According to Setting a fixed size for points in legend, calling .set_sizes([...])
on the legend handles should work.
For a jointplot()
this would be called as:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
d = {'x': [3, 2, 5, 1, 1, 0],
'y': [1, 1, 2, 3, 0, 2],
'cat': ['a', 'a', 'a', 'b', 'b', 'b']}
df = pd.DataFrame(d)
with sns.plotting_context(rc={"legend.fontsize": 'x-large'}):
g = sns.jointplot(data=df, x='x', y='y', hue='cat')
for h in g.ax_joint.get_legend().legendHandles:
h.set_sizes([10])
plt.show()
Answered By - JohanC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.