区分Python图中的某些点

发布于 2025-01-29 06:29:54 字数 302 浏览 2 评论 0原文

我在python中有一个情节,

a = (1,2,3,4,5)
b = (1,2,3,4,5)
plot(a,b)

我想用独特的颜色点来区分图中的某些x轴点 例如,

c = (2,4)

我尝试了以下几点:

a = (1,2,3,4,5)
b = (1,2,3,4,5)
plot(a,b)
matplotlib.pyplot.scatter(a, c)

但是我遇到了错误“ x和y必须是相同的大小”

I have a plot in Python of the following

a = (1,2,3,4,5)
b = (1,2,3,4,5)
plot(a,b)

I want to differentiate some of the x axis points in the plot with a dots of unique colors
for examples this points

c = (2,4)

I tried the following:

a = (1,2,3,4,5)
b = (1,2,3,4,5)
plot(a,b)
matplotlib.pyplot.scatter(a, c)

But I got the error "x and y must be the same size"

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

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

发布评论

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

评论(1

一个人的旅程 2025-02-05 06:29:54

如果您Google Google“ matplotlib scatter”或类似的东西,则第一个链接之一将是这个,它说该功能给出了“ y vs. x的散点图”。因此,您共享的错误消息是有道理的,因为a的长度大于c的长度。我希望对您有意义,为什么您的原始代码会出现该错误。

您的问题已经足够具体,以至于我知道matplotlib中没有开箱即用的解决方案,因此这需要使用您自己的自定义功能。我会提供一种可能对您有帮助的方法。 我在这里构建答案,以便您可以看到如何解决自己的规格,而不是过于依赖复制&粘贴其他人的代码,因为后者使您更难做到要做您想做的事情。

更简洁地重述问题: matplotlib如何用户绘制一条线,然后将标记放在线的点子集上,由其x值指定?

开始,这是您的程序当前可能的样子:

import matplotlib.pyplot as plt

x_coords = [ ] # fill in x_coords here
y_coords = [ ] # fill in y_coords here
marker_x_coords = [ ] # fill in with x coordinates of points you want to have markers

plt.plot(x_coords, y_coords) # plots the line

#### TODO: plot the markers ####

现在,您拥有该程序的X值您想放上标记的点。您如何获得他们相应的Y值?好吧,您可以制作一个函数,以在x_coords中搜索X值的索引,然后在y_coords的相同索引下给出相应的值:

def getYVals(x_coords_lst, y_coords_lst, marker_x_coords_lst):
    marker_y_coords = [] # start with an empty list
    for x_point in marker_x_coords_lst:
        point_index = x_coords_lst.index(x_point) # get the index of a point in the x list
        marker_y_coords.append(y_coords_lst[point_index]) # add the value of the y list at that index to the list that will be returned
    return marker_y_coords

这是' t最快的方法,但这是最清晰的方法。这是一个替代方案,可以给出相同的结果,但可能会在计算上执行更快的速度(它使用“列表理解”):

def getYVals(x_coords_lst, y_coords_lst, marker_x_coords_lst):
    return [y_coords_lst[x_coords_lst.index(x_val)] for x_val in marker_x_coords_lst]

以上任何一个getyvals函数的输出都可以作为y值的y值标记。可以将其放在y plt.scatter的参数中,并且您已经具有x值,因此您应该从那里开始好!

If you google "matplotlib scatter" or something like that, one of the first links will be this one, which says that the function gives "a scatter plot of y vs. x". So, the error message you shared makes sense, since the length of a is greater than the length of c. I hope it makes sense to you why what your original code gives that error.

Your problem is specific enough that there isn't an out-of-the-box solution in matplotlib that I'm aware of, so this will require the use of your own custom functions. I'll give one approach that might be helpful to you. I'm structuring my answer here so you can see how to solve the issue to your own specifications, instead of relying too heavily on copying & pasting other people's code, since the latter makes it harder for you to do exactly what you want to do.

To restate the problem in more concise terms: How can a matplotlib user plot a line, and then put markers on a subset of the line's points, specified by their x-values?

To begin, here is what your program might look like currently:

import matplotlib.pyplot as plt

x_coords = [ ] # fill in x_coords here
y_coords = [ ] # fill in y_coords here
marker_x_coords = [ ] # fill in with x coordinates of points you want to have markers

plt.plot(x_coords, y_coords) # plots the line

#### TODO: plot the markers ####

Now, you have the x-values of the points you want to put markers on. How might you get their corresponding y-values? Well, you can make a function that searches for the index of the x-value in x_coords, and then gives the corresponding value at the same index of y_coords:

def getYVals(x_coords_lst, y_coords_lst, marker_x_coords_lst):
    marker_y_coords = [] # start with an empty list
    for x_point in marker_x_coords_lst:
        point_index = x_coords_lst.index(x_point) # get the index of a point in the x list
        marker_y_coords.append(y_coords_lst[point_index]) # add the value of the y list at that index to the list that will be returned
    return marker_y_coords

This isn't the fastest method, but it is the clearest on what is happening. Here's an alternative that would give the same results but would likely perform faster computationally (it uses something called "list comprehension"):

def getYVals(x_coords_lst, y_coords_lst, marker_x_coords_lst):
    return [y_coords_lst[x_coords_lst.index(x_val)] for x_val in marker_x_coords_lst]

The output of either of the getYVals function above will work as the y values of the markers. This can be put in the y argument of plt.scatter, and you already have the x values of it, so from there you should be good to go!

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