matplotlib 如何在python散点图中绘制两个变量的颜色?

9rbhqvlz  于 2023-02-19  发布在  Python
关注(0)|答案(1)|浏览(246)

我有一个数据集与两个不同的变量,我想给每个不同的颜色颜色,有人能帮忙吗?链接到我的数据集:"示例网站"https://github.com/mayuripandey/Data-Analysis/blob/main/word.csv"

import matplotlib.pyplot as plt
import pandas as pd


fig, ax = plt.subplots(figsize=(10, 6))

ax.scatter(x = df['Friends Network-metrics'], y = df['Number of Followers'],cmap = "magma")
plt.xlabel("Friends Network-metrics")
plt.ylabel("Number of Followers")
plt.show()

fquxozlt

fquxozlt1#

不太清楚你想在这里做什么。但我会提供一个解决方案,可能会帮助你一点。
可以使用seaborn来实现变量的颜色。否则,您需要迭代这些点来设置颜色。或者创建一个新列,以便有条件地输入值的颜色。
我不知道你的变量是什么,但你只需要把它放在hue参数中:

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

df = pd.read_csv('https://raw.githubusercontent.com/mayuripandey/Data-Analysis/main/word.csv')

# Use the 'hue' argument to provide a factor variable
sns.lmplot(x='Friends Network-metrics', 
           y='Number of Followers', 
           height=8,
           aspect=.8,
           data=df, 
           fit_reg=False, 
           hue='Sentiment', 
           legend=True)

plt.xlabel("Friends Network-metrics")
plt.ylabel("Number of Followers")
 
plt.show()

这可以为您提供如下视图:

如果你要为其中一个变量寻找颜色比例,你可以做以下的事情。然而,最大值太大,范围也不能使它真正成为一个有效的视觉效果:

import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/mayuripandey/Data-Analysis/main/word.csv')

fig, ax = plt.subplots(figsize=(10, 6))
g = ax.scatter(x = df['Friends Network-metrics'], 
               y = df['Number of Followers'],
               c = df['Friends Network-metrics'],
               cmap = "magma")
fig.colorbar(g)

plt.xlabel("Friends Network-metrics")
plt.ylabel("Number of Followers")
 
plt.show()

因此,您可以调整比例(我还将添加edgecolors = 'black',因为很难看到光图):

import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/mayuripandey/Data-Analysis/main/word.csv')

fig, ax = plt.subplots(figsize=(10, 6))
g = ax.scatter(x = df['Friends Network-metrics'], 
               y = df['Number of Followers'],
               c = df['Friends Network-metrics'],
               cmap = "magma",
               vmin=0, vmax=10000,
               edgecolors = 'black')
fig.colorbar(g)

plt.xlabel("Friends Network-metrics")
plt.ylabel("Number of Followers")
 
plt.show()

相关问题