简体   繁体   English

使用列表为matplotlib中的系列指定颜色?

[英]Using lists to assign colors to a series in matplotlib?

I thought the easiest way to make a scatterplot with N different series (like in excel) would be to have 3 lists:i) x_coordinates would have N lists containing the X positions; 我认为制作具有N个不同系列的散点图的最简单方法(如在excel中)将具有3个列表:i)x_coordinates将具有包含X位置的N个列表; ii) y_coordinates would also have N lists containing the Y positions; ii)y_coordinates还有N个包含Y位置的列表; and iii) color_map would have N elements containing the different RGB colors per series. iii)color_map将包含N个元素,每个元素包含不同的RGB颜色。

My problem is that the order gets weird on plotting the colors for the markers. 我的问题是,在绘制标记的颜色时,顺序变得奇怪。 Why is this happening and how do I fix it? 为什么会发生这种情况,我该如何解决?

import matplotlib.pyplot as plt
import random
x_coordinates = [(range(1,4))]*2
y_coordinates = [[3,4,5],[2,2,2]]
color_map = []
for i in range(0,len(x_coordinates)):
    r = lambda: random.randint(0,255)
    rgb_hex = ('#%02X%02X%02X' % (r(),r(),r()))
    color_map.append(rgb_hex)
plt.scatter(x_coordinates,y_coordinates,c=color_map)
plt.show()

Lists for x/y coordinates and colors 列出x / y坐标和颜色

[[1, 2, 3], [1, 2, 3]] #x
[[3, 4, 5], [2, 2, 2]] #y
['#E6764F', '#A12678'] #colors

在此输入图像描述

The colors are specified per point, not per series. 每个点指定颜色,而不是每个系列。 The coloring you see is that it uses the first color for the first point of the first series, then the next color for the next point of that series, then cycles back to the first color for the third point of the first series, and so on. 您看到的颜色是它使用第一个颜色作为第一个系列的第一个点,然后是该系列的下一个点的下一个颜色,然后循环回到第一个系列的第三个点的第一个颜色,所以上。

I don't know of any way to specify the colors per series with the data in the format you show. 我不知道以任何方式使用您显示的格式指定每个系列的颜色。 You'll have to make a single sequence with all the colors, you want, eg: 您需要使用所需的所有颜色制作单个序列,例如:

# make first series red and second series blue
color_map = ["#FF0000"]*3 + ["#0000FF"]*3

Alternatively, you could transpose your x and y data so that the series are in columns rather than rows (which I think is how matplotlib interprets them anyway). 或者,您可以转置您的x和y数据,以便系列是列而不是行(我认为matplotlib无论如何都会解释它们)。 Then specifying two colors will do what you want. 然后指定两种颜色将做你想要的。

I think the basic problem is that you have to rethink if you really need the scatter function. 我认为基本问题是如果你真的需要scatter功能,你必须重新考虑。 From what you posted I think it would be enough to use plot repeatedly. 从您发布的内容来看,我认为重复使用plot就足够了。 scatter produces a series of colored markers (with the possibility of assigning varying colors and sizes to the markers), whereas plot just plots lines with a single color. scatter产生一系列彩色标记(可以为标记指定不同的颜色和大小),而plot只是用单一颜色绘制线条。 However, these lines can also have markers at the points (with a single color). 但是,这些线条也可以在点处具有标记(使用单一颜色)。 So the way to approach this is to create a line plot per series. 因此,解决这个问题的方法是为每个系列创建一个线图。 (If you don't want the line, you can set the linestyle to 'none' . But wrt your previous question I believe that you want to do that anyways) (如果你不想要这一行,你可以将linestyle设置为'none' 。但是,相信你以前的问题,我相信你想要做到这一点)

NB: I also applied some cosmetics to your code: 1) there is no need to create hex strings for the colors. 注意:我还在代码中应用了一些化妆品:1)不需要为颜色创建十六进制字符串。 Simply use lists/tuples with 3 elements scaled from 0 to 1. 2) You can simply save the random function to r. 只需使用列表/元组,其中3个元素从0到1缩放.2)您可以简单地将随机函数保存到r。 No need to create a lambda function. 无需创建lambda函数。

import matplotlib.pyplot as plt
import random

# x and y coordinates
x_coordinates = [range(1,4)]*2
y_coordinates = [[3,4,5],[2,2,2]]

# colors (one per series --> 2 in this example)
colors = []
r = random.random
for i in range(0,len(x_coordinates)):
    rgb =  (r(),r(),r())
    colors.append(rgb)

# iterate over the series
for x, y, c in zip(x_coordinates, y_coordinates, colors):
    plt.plot(x, y, color=c, linestyle='-', marker='o', markeredgecolor='k',
            markersize=10)

# show the figure
plt.margins(x=0.1, y=0.1)
plt.show()

Result: 结果: 在此输入图像描述

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

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