python - 如何在 seaborn.lineplot 中将所有线条更改为黑色?
问题描述
如何在seaborn中将所有线条更改为黑色?我输入了“color = 'black'但它并没有改变默认值。我还希望whitegrid保留在图表中但它消失了。
data['date'] = pd.to_datetime(data['date']).dt.date
sns.lineplot(data=data, x='date', y='count', hue='new_sentiment', style = 'new_sentiment',
color ='black')
sns.set_style("whitegrid", {
"ytick.major.size": 0.1,
"ytick.minor.size": 0.05,
'grid.linestyle': 'solid',
})
plt.legend(bbox_to_anchor=(1.04,1), loc="upper left")
plt.setp(plt.gca().xaxis.get_majorticklabels(),rotation=90)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%b-%Y'))
plt.xlim([datetime.date(2020, 1, 13), datetime.date(2021, 6, 15)])
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=24))
plt.savefig('04_clean_resentiment_count_2020_2021.tiff', dpi=300, format='tiff', bbox_inches='tight')
解决方案
以官方参考中的折线图为例,我将线的颜色设置为黑色。然而,当它只是一种颜色时,很难识别数据。使用单一颜色阴影进行可视化可能会更好。
将 seaborn 导入为 sns
flights = sns.load_dataset("flights")
sns.set_style("whitegrid", {
"ytick.major.size": 0.1,
"ytick.minor.size": 0.05,
'grid.linestyle': 'solid',
})
g = sns.lineplot(data=flights, x="year", y="passengers", hue='month')
lines = g.get_lines()
[l.set_color('black') for l in lines]
g.legend()
sns.lineplot(data=flights, x="year", y="passengers", hue='month', palette=('Greys'))
推荐阅读
- docker - 在超级账本结构中创建专用网络时出错
- php - 不要用动态路由重写链接生成器
- ios - 无法同时满足约束 iOS
- swift - AVCaptureDevice 无法为我移植了 Catalyst 的 macOS 应用找到任何设备
- c++ - cout<<中的未知编译器错误理论上它可以工作
- google-apps-script - 如何打开 G Suite 附加边栏?
- java - 支持事务关联的 Java 或 Spring 异步任务执行器?
- amazon-web-services - 以编程方式从特定版本下载 s3 文件
- python - 将 .xlsx 文件合并为一个而不覆盖数据
- excel - 如何使用 VBA 在 Excel 中引用“活动/选定”图表数据点