简体   繁体   English

Python散点图多个具有相同颜色的x点和y点

[英]Python Scatter Plot Multiple x points and y points with same color

Usually scatter plots use one x value, one y value.通常散点图使用一个 x 值,一个 y 值。 The point is colored using az value.该点使用 az 值着色。 My scatter plot requirement is different.我的散点图要求不同。 I have a list of x points, list of y points to be plotted using az value or one color.我有一个 x 点列表,y 点列表,要使用 az 值或一种颜色绘制。 My code:我的代码:

 samdf = pd.DataFrame({'x':[[1,2,3],4,5,6],
                     'y':[[10,20,30],40,50,60],
                     'z':[7,8,9,10]})
 plt.scatter(samdf['x'],samdf['y'],c=samdf['z'],cmap='jet')
 plt.colorbar()
 plt.show()

Present output:当前输出:

ValueError: setting an array element with a sequence.

Sorry I don't have the reputation to comment.对不起,我没有评论的声誉。

The problem isn't caused by matplotplib but rather the dictionary you defined when initializing the dataframe.问题不是由 matplotplib 引起的,而是由您在初始化数据帧时定义的字典引起的。

You have defined your DataFrame as您已将 DataFrame 定义为

samdf = pd.DataFrame({'x':[[1,2,3],4,5,6],
                     'y':[[10,20,30],40,50,60],
                     'z':[7,8,9,10]})

This is impossible each row in each column should be of a primitive type.每列中的每一行都应该是原始类型是不可能的。 This is equivalent to the following table:这相当于下表:

x       | y          | z 
-------------------------
[1,2,3] | [10,20,30] | 7
-------------------------
4       | 40         | 8
5       | 50         | 9
...

which is not a valid table.这不是一个有效的表。

What is that dictionary even trying to achieve?那本字典甚至试图达到什么目的?

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

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