【问题标题】:How to assign different color to different points in a scatter plot containing different lengths of variables?如何为包含不同长度变量的散点图中的不同点分配不同的颜色?
【发布时间】:2017-11-14 15:14:06
【问题描述】:

我正在尝试使用 python matplotlib 绘制具有不同可变长度的散点图。对于以下数据

Days = [1,2,3]
Values = ([2,3,4,5],[3,4,5,6,7],[2,3])

我可以使用以下程序绘制所有点

w = [[Days[i]] * len(Values[i]) for i in range(len(Values))]

x_to_plot = [item for sublist in w for item in sublist]
y_to_plot = [item for sublist in Values for item in sublist].

如何为几个点分配不同的颜色以保持所有其他点的颜色相同?

【问题讨论】:

  • 您可以手动指定颜色,例如对红点使用plt.plot(some_points, '.r')。然后,您只需将数据分成您需要的 2 组,并用 2 种不同的颜色分别绘制它们。
  • 你的程序,如图所示,没有绘制任何东西。

标签: python excel matplotlib colors statistics


【解决方案1】:

您可以使用scatterc 参数根据颜色图对点进行着色。例如。要将y == 5 - x 的所有点着色为红色,请使用

import matplotlib.pyplot as plt

Days = [1,2,3]
Values = ([2,3,4,5],[3,4,5,6,7],[2,3])

w = [[Days[i]] * len(Values[i]) for i in range(len(Values))]

x = [item for sublist in w for item in sublist]
y = [item for sublist in Values for item in sublist]

c = [0 if i+j == 5 else 1 for i, j in zip(x,y)]
plt.scatter(x,y, c=c, cmap="bwr_r")

plt.show()

【讨论】:

    猜你喜欢
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    相关资源
    最近更新 更多