python-3.x - 如何在seaborn regplot中自动交替或循环线条样式?

标签 python-3.x matplotlib seaborn

我希望同一张图表上的 21 行数据能够通过图例更容易解释。例如,也许我可以使所有其他图例条目/行都用破折号而不是连续线显示。我对 Seaborn 和 Matplotlib 的混合使用让我感到困惑 - 我不知道如何以交替的方式将破折号放入其中。

products = list(data_cleaned.columns)
print('products: \n',products)
for i, product in enumerate(products):
    subset = data_cleaned[data_cleaned[product]>0][product]
    sns.distplot(subset,hist=False,kde=True,kde_kws={'linewidth':3},label=product)
    if i%2 == 0:
        plt.plot(subset,'-', dashes = [8, 4, 2, 4, 2, 4])

sns.set(rc = {'figure.figsize':(25,10)})
#sns.palplot()
palette_to_use = sns.color_palette("hls", 21)
sns.set_palette(palette_to_use)
#cmap = ListedColormap(sns.color_palette().as_hex())
plt.legend(prop={'size': 16}, title = 'Product')
plt.title('Density Plot with Multiple Products')
plt.xlabel('log10 of monthly spend')
plt.ylabel('Density')

这是我当前的输出: enter image description here

最佳答案

您可以在 distplot 中的 kde_kws= {'linestyle': '--'} 中提供线条样式参数,就像使用 linewidth 一样 - 在 '-' 和 '--' 之间交替线条样式以达到您想要的效果。

示例

import numpy as np; np.random.seed(10)
import seaborn as sns; sns.set(color_codes=True)
mean, cov = [0, 2], [(1, .5), (.5, 1)]
x, y = np.random.multivariate_normal(mean, cov, size=50).T
ax = sns.distplot(x, hist = False, kde=True, kde_kws={'linestyle': '--'})

关于python-3.x - 如何在seaborn regplot中自动交替或循环线条样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55461832/

相关文章:

python - Google Colab 在下载数据集或将新包导入 colab notebook 时是否会使用我的互联网流量?

Python 3 Tkinter - 更改具有相同名称的多个条目的状态

python - 可视化特征图: IndexError: too many indices for array

python - 是否可以使用 matplotlib 进行加法混合?

python - Seaborn 的 No numeric types to aggregate 错误

python - 如何将 seaborn/matplotlib 轴刻度标签从数字格式化为数千或数百万? (125,436 至 125.4K)

python - 在 Seaborn Jointplot 上注释异常值

python - 将列表中的值附加到字典中

Python设置: "empty" element,输入不正确

python - 2D 数组中的 Matplotlib 4D 数据