简体   繁体   English

在散点图 plot 上设置 colors 时出现 Matplotlib 错误

[英]Matplotlib error while setting colors on a scatter plot

I'm trying to plot a scatter plot with Matplotlib, but i'm having troubles setting colors.我正在尝试使用 plot 分散 plot 与 Matplotlib,但我在设置 Z62848E3CE582048A77 时遇到了麻烦

Here is my code:这是我的代码:

colors = [(141, 0, 248, 0.4) if x >= 150 and x < 200 else 
          (0, 244, 248, 0.4) if x >= 200 and x < 400 else
          (255, 255, 0, 0.7) if x >= 400 and x < 600 else
          (255, 140, 0, 0.8) if x >= 600 else (255, 0, 0, 0.8) for x in MyData.Qty]

print(len(colors))
ax1.scatter(MyData.Date, MyData.Rate, s=20, c=colors, marker='_')

Basically, i have a column called Qty on my dataframe, and according to that value, the colors is chosen.基本上,我的 dataframe 上有一个名为Qty的列,根据该值,选择 colors。 If Qty is bigger than x, the color will be red and so on, for example.例如,如果数量大于 x,颜色将为红色等。

The previous code will give me the following error:前面的代码会给我以下错误:

'c' argument has 2460 elements, which is inconsistent with 'x' and 'y' with size 615.

And i have no idea why does that happen, because if i try the following code, it will work without any problem:而且我不知道为什么会发生这种情况,因为如果我尝试以下代码,它将毫无问题地工作:

colors = ['red' if x >= 150 and x < 200 else 
          'yellow' if x >= 200 and x < 400 else
          'green' if x >= 400 and x < 600 else
          'blue' if x >= 600 else 'purple' for x in MyData.Qty]

Here is a sample of my data:这是我的数据示例:

    Date  Rate          Qty
0     18  140   207.435145
0     18  141   155.019884
0     18  178  1222.215201
0     18  230   256.010358
0     19  9450  1211.310384

The following will work too:以下也将起作用:

colors = [(1,1,0,0.8) if x>1000 else (1,0,0,0.4) for x in MyData.Qty]

Someone commented (and then deleted) referring to the documentation, but here is the part they were referring to (from plt.scatter ):有人评论(然后删除)参考了文档,但这是他们所指的部分(来自plt.scatter ):

Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped.请注意,c 不应是单个数字 RGB 或 RGBA 序列,因为它与要进行颜色映射的值数组无法区分。 If you want to specify the same RGB or RGBA value for all points, use a 2-D array with a single row.如果要为所有点指定相同的 RGB 或 RGBA 值,请使用具有单行的二维数组。 Otherwise, value- matching will have precedence in case of a size matching with x and y.否则,在大小与 x 和 y 匹配的情况下,值匹配将具有优先权。

But it seems that in addition, from here that matplotlib is expecting the RGB values to be from 0 to 1, rather than 0 to 255. So I just added two lines to a) explicitly convert colors as a numpy 2D array and b) divide the RGB values by 255 (leaving the alpha value untouched). But it seems that in addition, from here that matplotlib is expecting the RGB values to be from 0 to 1, rather than 0 to 255. So I just added two lines to a) explicitly convert colors as a numpy 2D array and b) divide RGB 值乘以 255(保持 alpha 值不变)。

import matplotlib.pyplot as plt
import numpy as np

fig1, ax1 = plt.subplots()

colors = [(141, 0, 248, 0.4) if x >= 150 and x < 200 else 
          (0, 244, 248, 0.4) if x >= 200 and x < 400 else
          (255, 255, 0, 0.7) if x >= 400 and x < 600 else
          (255, 140, 0, 0.8) if x >= 600 else (255, 0, 0, 0.8) for x in MyData['Qty']]

#addition to convert colors
colors = np.array(colors)
colors[:,:3] /= 255

ax1.scatter(MyData['Date'], MyData["Rate"], s=20, c=colors, marker='_')

Removing the scaling (but still converting to 2D array), you will get the same error as you originally experienced, so I guess when it doesn't recognize 0 to 1 scaled RGB values, it tries to just interpret the flattened array and you get the 4x values problem.删除缩放(但仍转换为 2D 数组),您将得到与最初遇到的相同的错误,所以我猜当它无法识别 0 到 1 缩放的 RGB 值时,它会尝试仅解释展平数组并得到4x 值问题。

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

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