使用 np.arange 获取 Matplotlib 散点图中点的大小时如何增加点大小

发布于 2025-01-17 05:20:35 字数 987 浏览 0 评论 0原文

我正在努力让点的大小逐渐增加。我相信我需要向 size 参数传递一个 n 值列表(n 表示数组的大小)。为此,我使用 linspace 函数创建从小到大的值列表,并将其作为参数传递给函数。它通常工作正常,但我有两个问题:

  1. 是否有更优化的方法来编写传递到“颜色”的数组 散点图?请参阅下面我从互联网上找到的代码传递数组的位置,但是 它看起来很笨重。
  2. 如何增加点的大小,使它们都大 2 点左右?请参阅下面的大小变量,我在其中传递数组并传递 row*col。每次我尝试修改此处的大小时,都会引发错误。
    我附上了输出的图像以供参考。

感谢您的帮助。在此处输入图片描述

# function
def plotdata(data):
    fig = plt.figure(figsize=(10,5))   

# plot
    plt.title("2D Data",size="15") 
    plt.xlabel("x") 
    plt.ylabel("y") 
    plt.scatter(x, y, s=size, c=color, marker='o', cmap='copper')
    plt.xlim(-2, 2)
    plt.ylim(-2, 2)

# Creating variables
x = data[["x"]]
y = data[["y"]]

limits=[-2.25, 2.25, -2.25, 2.25]
row,col=5,5
size = np.arange(1, row*col + 1)
color = np.array([np.linspace(0,1, row*col),
                 np.linspace(0,0.3, row*col),
                 np.linspace(0,0.3, row*col)]).T

I'm trying to get the points to gradually increase in size. I believe I need to pass the size argument a list of n values (n representing the size of the array). So to this end, I'm using the linspace function to create the list of values that range from small to larger and passing it into a function as an argument. It's working generally, but I have two questions:

  1. Is there a more optimum way of writing the array that gets passed into the 'color' of the
    scatterplot? See below where I'm passing in an array from code I found on the Internet but
    it looks clunky.
  2. How do I increase the size of the points so they're all about 2 points larger? See below size variable where I'm passing in an array and passing in row*col. Every time I try to tinker with the size here, it throws an error.
    I attached an image of what the output should look like for reference.

Thanks for any assistance.enter image description here

# function
def plotdata(data):
    fig = plt.figure(figsize=(10,5))   

# plot
    plt.title("2D Data",size="15") 
    plt.xlabel("x") 
    plt.ylabel("y") 
    plt.scatter(x, y, s=size, c=color, marker='o', cmap='copper')
    plt.xlim(-2, 2)
    plt.ylim(-2, 2)

# Creating variables
x = data[["x"]]
y = data[["y"]]

limits=[-2.25, 2.25, -2.25, 2.25]
row,col=5,5
size = np.arange(1, row*col + 1)
color = np.array([np.linspace(0,1, row*col),
                 np.linspace(0,0.3, row*col),
                 np.linspace(0,0.3, row*col)]).T

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

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

发布评论

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

评论(1

心凉 2025-01-24 05:20:35

2种可能性:

1.

size = np.arange(1, row*col + 1)

我认为这是错误,我不认为它添加了1,因为图表很奇怪。

  1. (可能)

    plt.scatter(x, y, s=大小, c=颜色, 标记='o', cmap='铜')
    plt.title("二维数据",size="15")
    大小 = np.arange(1, 行*列 + 1) 
    

您多次定义尺寸
祝你好运,自从我使用这个 API 以来已经有一段时间了

2 possibilities:

1.

size = np.arange(1, row*col + 1)

I think this is the error, I don't think it is adding the 1 since the graph is weird.

  1. (likely)

    plt.scatter(x, y, s=size, c=color, marker='o', cmap='copper')
    plt.title("2D Data",size="15")
    size = np.arange(1, row*col + 1) 
    

you define size many times
best of luck, it has been a while since I've used this API though

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