首页 > 解决方案 > python seaborn.distplot 不正确的图例

问题描述

在 seaborn.distplot 中使用不同的线条样式时,我得到了不正确的图例

这是我的代码:

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

URL = "https://media.githubusercontent.com/media/WillKoehrsen/Data-Analysis/master/univariate_dist/data/formatted_flights.csv"
df = pd.read_csv(URL, index_col=0)
airlines = df['name'].unique().tolist()

LINE_STYLES = ['solid', 'dashed', 'dashdot', 'dotted']
plt.figure(figsize=(15,8))

plt.title('Histogram of Arrival Days', fontsize=18)
plt.xlabel('Delay (min)', fontsize=18)
plt.ylabel('Flights', fontsize=18)

for i, airline in enumerate(airlines):
  sns.distplot(df[df['name'] == airline]['arr_delay'], label=airline, bins=int(180/5), 
                    hist=False, kde_kws={'linewidth': 3, 'shade':True, 'linestyle':LINE_STYLES[i%4]})

情节如下所示: 在此处输入图像描述

阿拉斯加航空公司的传说应该是-。(如美国航空公司)但它是 - (如联合航空公司)

标签: pythonmatplotlibseaborndensity-plot

解决方案


啊,我明白了。solid所以这条线太粗了,无法观察到和之间的差异dashdot。如果使用 eg 是正确的'linewidth': 1

在此处输入图像描述

或者,您可以创建自己的线条样式,例如

LINE_STYLES = ['solid', (0,(3,1)), (0,(4,2,1,2)), 'dotted']

在此处输入图像描述


推荐阅读