简体   繁体   English

区分python图中的一些点

[英]Differentiate some points in a python plot

I have a plot in Python of the following我在 Python 中有一个情节如下

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我想用一些独特的颜色来区分图中的一些 x 轴点,例如这个点

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"但我收到错误“x 和 y 必须相同大小”

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".如果你用谷歌搜索“matplotlib scatter”或类似的东西,第一个链接之一就是这个,它表示该函数给出了“y 与 x 的散点图”。 So, the error message you shared makes sense, since the length of a is greater than the length of c .因此,您共享的错误消息是有道理的,因为a的长度大于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.您的问题非常具体,以至于我知道matplotlib中没有开箱即用的解决方案,因此这将需要使用您自己的自定义函数。 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?用更简洁的术语重申这个问题: matplotlib用户如何绘制一条线,然后将标记放在线点的子集上,由它们的 x 值指定?

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.现在,您有了要放置标记的点的 x 值。 How might you get their corresponding y-values?您如何获得它们对应的 y 值? 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 :好吧,您可以创建一个函数,在 x_coords 中搜索 x 值的索引,然后在x_coords的同一索引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.上述任一getYVals函数的输出都将用作标记的 y 值。 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!这可以放在plt.scattery参数中,并且你已经有了它的x值,所以从那里你应该很高兴!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM