gpt4 book ai didi

python - Seaborn FacetGrid 图例在更改线宽时丢失线型细节

转载 作者:太空宇宙 更新时间:2023-11-04 02:31:20 31 4
gpt4 key购买 nike

如果我通过 FacetGrid().map() 更改 plt.plot 实例的线宽,我将丢失图例中线型的详细信息。在此图中,线型从图表中正确可见 Example 2

但是,如果我增加线宽 z 不再正确显示线型

Example 1

如果你想重现图表,这里是代码,

import numpy as np
import seaborn as sbn
import pandas as pd
import matplotlib.pyplot as plt
x = np.tile(np.linspace(0,10,11),2)
y = np.linspace(1,11,11)
z = np.linspace(2,12,11)
yz = np.append(y,z)
y_or_z = np.repeat(['y','z'],11)
df = pd.DataFrame(data={'x':x,'yz':yz,'y_or_z':y_or_z})
g=sbn.FacetGrid(data = df, size=4, aspect=1.2, hue = 'y_or_z', hue_kws=dict(ls=['-','-.']))
g.map(plt.plot,'x','yz', lw=4).add_legend()
g.savefig('ex1')
g=sbn.FacetGrid(data = df, size=4, aspect=1.2, hue = 'y_or_z', hue_kws=dict(ls=['-','-.']))
g.map(plt.plot,'x','yz').add_legend()
g.savefig('ex2')

最佳答案

您可能会注意到第二个图中的橙色线比蓝色线短。因此图例确实显示了正确的线型,但图例 handle 的长度不足以承载完整的线型模式。

当然,您可以更改 handle 长度,使完整的线型循环适合它,例如

g.add_legend(handlelength=10) 

enter image description here

handlelength 参数记录在 matplotlib legend documentation 中:

handlelength : float or None
The length of the legend handles. Measured in font-size units. Default is None which will take the value from the legend.handlelength rcParam.

关于python - Seaborn FacetGrid 图例在更改线宽时丢失线型细节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49086528/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com