[英]How to accurately plot the legend of multi-layer plot (boxplot and lineplot)?
import numpy as np
import seaborn as sns
from matplotlib.patches import Patch
from matplotlib.lines import Line2D
data_box = np.random.random((10, 3))
data_line = np.random.random(3)
ax = sns.boxplot(data=data_box, color='red', saturation=0.5)
sns.lineplot(data=data_line, color='blue')
legend_elements = [Line2D([0], [0], color='blue', lw=4, label='box'),
Patch(facecolor='red', edgecolor='grey', linewidth=1.5,
label='line')]
ax.legend(handles=legend_elements, fontsize='xx-large')
我將線圖疊加到箱線圖上,如上圖所示,並使用 matplotlib 手動繪制圖例。
但是seaborn設置了顏色的飽和度,其默認值為0.75(我設置為0.5,區別很明顯)。 所以matplotlib生成的圖例顏色是不准確的。 有沒有辦法改變matplotlib圖例的飽和度? 或者我怎樣才能准確地繪制圖例顏色,除了設置saturation=1
。
import numpy as np
import seaborn as sns
from matplotlib.patches import Patch
from matplotlib.lines import Line2D
data_box = np.random.random((10, 3))
data_line = np.random.random(3)
fig, ax = plt.subplots()
ax = sns.boxplot(data=data_box, color='red', saturation=0.5)
sns.lineplot(data=data_line, color='blue')
legend_elements = [Line2D([0], [0], color='blue', lw=4, label='box'),
Patch(facecolor=sns.desaturate('red',0.5), edgecolor='grey', linewidth=1.5,
label='line')]
ax.legend(handles=legend_elements, fontsize='xx-large')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.