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

发布于 2025-01-30 02:50:14 字数 611 浏览 0 评论 0原文

我有一个具有两个不同变量的数据集,我想用不同的颜色给每个变量,任何人可以帮忙吗?链接到我的数据集:“ 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()

”在此处inter

I have a dataset with two different variables, i want to give colors to each with different color, Can anyone help please? Link to my dataset : "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()

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半暖夏伤 2025-02-06 02:50:14

不清楚您想在这里做什么。但是,我将提供一个可能对您有所帮助的解决方案。

可以使用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()

这可以为您提供这样的视图:

sstatic.net/afnk1.png“ rel =“ nofollow noreferrer 寻找其中一个变量的颜色尺度,您将做以下操作。但是,最大值是如此之大,以至于范围也不会真正有效的视觉效果:

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()

“在此处输入图像说明”

因此您可以调整刻度要查看光图):

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()

”在此处输入图像说明”

Not very clear what you want to do here. But I'll provide a solution that may help you a bit.

Could use seaborn to implement the colors on the variables. Otherwise, you'd need to iterate through the points to set the color. Or create a new column that conditionally inputs a color for a value.

I don't know what your variable is, but you just want to put that in for the hue parameter:

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()

This can give you a view like this:

enter image description here

If you were looking for color scale for one of the variables though, you would do the below. However, the max value is so big that the range also doesn't make it really an effective visual:

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()

enter image description here

So you could adjust the scale (I'd also add edgecolors = 'black' as its hard to see the light plots):

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()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文