首页 > 解决方案 > Seaborn 图调整“plotting_context”对于图例标记大小没有按预期工作

问题描述

我正在尝试调整 Seaborn 联合图中的图例标记。根据“MaozGelbart”在这个 Github issue上的回答,我希望rcParams为此工作。配置在这里的 matplotib 文档中,我认为legend.markerscale是我感兴趣的参数。

如何更改图例标记比例?下面的这段代码不起作用。但是,它确实适用于legend.fontsize参数。

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')

在此处输入图像描述

这里有一个类似的Stack Overflow 问题,但似乎没有明确的答案。此外,rcParams从理论上讲,设置应该有效

标签: pythonmatplotlibseaborn

解决方案


根据Setting a fixed size for points in legend,调用.set_sizes([...])图例句柄应该可以工作。

对于 ajointplot()这将被称为:

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()

示例图


推荐阅读