简体   繁体   English

如何在 python 中使用全局颜色条绘制 plot 多个散点图?

[英]How to plot multiple scatter plots with a global colourbar in python?

I have a dataset from which I've calculated a value which I've used in a colourbar for a plot. See below:我有一个数据集,我从中计算了一个值,我在 plot 的颜色栏中使用了这个值。见下文:

在此处输入图像描述

I'm trying to figure out how to combine it so that another parameter is plotted over it (see below:)我试图找出如何组合它以便在其上绘制另一个参数(见下文:) 在此处输入图像描述

I want the colours to be added to the different shapes for each of the classes.我希望将颜色添加到每个类的不同形状中。

Some of the code used is below:使用的一些代码如下:

plt.figure(figsize=[12,10])
plt.scatter(x_offset1, y_offset1, s=2, c=index, cmap='jet', marker='.')
plt.scatter(x_offset2, y_offset2, s=2, c=index, cmap='jet', marker='^')
plt.scatter(x_offset3, y_offset3, s=2, c=index, cmap='jet', marker='v')
plt.scatter(x_offset4, y_offset4, s=2, c=index, cmap='jet', marker='x')

plt.colorbar()

This gives the error:这给出了错误:

ValueError: 'c' argument has 289 elements, which is inconsistent with 'x' and 'y' with size 3.

I know that the 289 elements is the colour data for the whole dataset and the size 3 is the size of the first class of plots but I'm not sure how to plot the colour values of just the 3 points only.我知道 289 个元素是整个数据集的颜色数据,大小 3 是第一个 class 图的大小,但我不确定如何 plot 只有 3 个点的颜色值。

What you are looking for is a slice of the index list.您正在寻找的是索引列表的index Slices work like this:切片是这样工作的:

slice = pizza[:2]

And go in the format of list[start:stop:step] .和 go 格式为list[start:stop:step] So, if your colors go from the first item in the list to the third item , then you would do:所以,如果你的 colors go 从列表中的第一项第三项,那么你会这样做:

plt.scatter(x_offset4, y_offset4, s=2, c=index[1:3], cmap='jet', marker='x')

Assuming index contains ["a", "b", "c", "d"] (just for the sake of an example), index[1:3] will return ["a", "b", "c", "d"] .假设index包含["a", "b", "c", "d"] (仅作为示例), index[1:3]将返回["a", "b", "c", "d"] This operation does not modify the original list, so index will still be ["a", "b", "c", "d"] after index[1:3] .此操作不会修改原始列表,因此indexindex[1:3]之后仍然是["a", "b", "c", "d"] ] 。

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

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