如何更改散点图的颜色

时间:2020-06-10 12:07:54

标签: python pandas matplotlib seaborn

数据集在下面

sino,cost,profit,product
1,567510,359807,600
2,256203,154802,459
3,523447,664310,655
4,201928,449702,778
5,270359,913417,659

代码在下面

import pandas as pd
import seaborn as sn
df = pd.read_csv('1.csv')
df1 = df[['cost','profit','product']]
sns.pairplot(df1)
  • 我的图在下面。

  • 该图的分析内容是什么

  • 第一个成本如何更改颜色为红色,蓝色为利润,产品为绿色

enter image description here

1 个答案:

答案 0 :(得分:0)

sns.pairplot()返回轴的网格。然后,您可以使用axes.diag_axes访问对角线图形,然后访问可以根据需要更改其颜色的色块(条)。

以下代码完成了工作。

import matplotlib as mpl

df1 = df[['cost','profit','product']]
axes = sns.pairplot(df1)
colors = ['r', 'b', 'g']

for col, ax in zip(colors, axes.diag_axes):
    barlist=filter(lambda x: isinstance(x, mpl.patches.Rectangle), ax.get_children())
    for bar in barlist:
        bar.set_color(col)

enter image description here