繁体   English   中英

如何为一组箱线图添加单个图例标签?

[英]How to add a single legend label for a set of boxplots?

是否有更好的方法为一组箱线图添加单个标签到图例?

下面是一个简单的示例,可以提供所需的结果。 通过创建带有所需标签的不可见线( alpha=0 ),然后通过legendHandles更改alpha来完成此legendHandles 但是,是否可以将所有sns.boxplot()图的单个标签仅传递给sns.boxplot()

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Get the tips dataset and select a subset as an example
tips = sns.load_dataset("tips")
variable_to_bin_by = 'tip'
binned_variable = 'total_bill'
df = tips[ [binned_variable,  variable_to_bin_by] ]  

# Group the data by a list of bins
bins = np.array([0, 1, 2, 3, 4])
gdf = df.groupby( pd.cut(df[variable_to_bin_by].values, bins ) )
data = [ i[1][binned_variable].values for i in gdf]
df = pd.DataFrame( data, index = bins[:-1])   

# Plot the data (using boxplots to show spread of real values)
fig, ax = plt.subplots()
ax = sns.boxplot( data=df.T, ax=ax, color='k')

# Create hidden line with the extra label (to give label to boxplots)
x = np.range(10)
plt.plot(x, x, label='REAL DATA', color='k', alpha=0)

# Now plot some "model fit" lines
models = {'model1': bins+10, 'model2': bins+10*1.5, 'model3': bins*10}
for key in sorted( models.keys() ):
    plt.plot( bins, models[key], label=key )

# Add a legend
leg = plt.legend()

# Update line visibility (alpha)
for legobj in leg.legendHandles:
        legobj.set_alpha( 1 )

# Show the plot
plt.show()

尽管这可以达到预期的效果(如下所示),但我的问题是是否有更好的方法?

成功!

您可以直接使用要在图例中显示的属性(这里是颜色)直接创建一条空线,而不是使用包含某些数据的线,而该数据随后需要在图中不可见然后在图例中可见。 )。

plt.plot([], [], label='REAL DATA', color='k')

这样可以避免在情节和图例中使用Alpha。 完整的示例如下所示:

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Get the tips dataset and select a subset as an example
tips = sns.load_dataset("tips")
variable_to_bin_by = 'tip'
binned_variable = 'total_bill'
df = tips[ [binned_variable,  variable_to_bin_by] ]  

# Group the data by a list of bins
bins = np.array([0, 1, 2, 3, 4])
gdf = df.groupby( pd.cut(df[variable_to_bin_by].values, bins ) )
data = [ i[1][binned_variable].values for i in gdf]
df = pd.DataFrame( data, index = bins[:-1])   

# Plot the data (using boxplots to show spread of real values)
fig, ax = plt.subplots()
ax = sns.boxplot( data=df.T, ax=ax, color="grey")

# Create hidden line with the extra label (to give label to boxplots)
plt.plot([], [], label='REAL DATA', color='k')

# Now plot some "model fit" lines
models = {'model1': bins+10, 'model2': bins+10*1.5, 'model3': bins*10}
for key in sorted( models.keys() ):
    plt.plot( bins, models[key], label=key, zorder=3)

# Add a legend
leg = plt.legend()

# Show the plot
plt.show()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM