【问题标题】:matplotlib.pyplot scatterplot lines using lists for x-coordinates, y-coordinates, and colorsmatplotlib.pyplot 使用 x 坐标、y 坐标和颜色列表的散点图线
【发布时间】:2015-09-28 13:48:21
【问题描述】:

Matplotlib connect scatterplot points with line - Python

检查了这一点,他们的解决方案非常简单,使用 plt.plot(x_coordinates, y_coordinates, '-o') 但我有一个我正在使用的颜色列表,所以我不能使用这种方法。它们是 RGB 颜色。 (也不知道为什么同一系列颜色会交替出现)

如何用与标记颜色相同的线连接这些点?

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()

【问题讨论】:

  • 你的 x 和 y 坐标是不是故意怪怪的?

标签: python matplotlib graph plot line


【解决方案1】:

可以在散点图后面画线,设置zorder保证线在点后面。

import matplotlib.pyplot as plt
import random

x_coordinates = [(range(1,65))]*2
y_coordinates = [[random.randint(0,10) for i in range(0,64)]*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.plot(x_coordinates[0],y_coordinates[0][:len(x_coordinates[0])],'-', 
                          zorder=2, , color=(.7,)*3)
plt.scatter(x_coordinates,y_coordinates,c=color_map, s=60, zorder=3)

plt.show()

【讨论】:

  • 有没有办法让颜色与标记相匹配?
  • @O.rka:是的,但这是一个不同的问题(我什至不确定当标记在线上改变颜色时问题到底是什么)。我已经回答了您最初发布的问题。我希望您接受它(如果它回答了您的原始问题),然后将您的新问题作为新问题发布(我也有兴趣回答)。但是,我不想继续追问这个问题,回答更多不同的问题,直到你最终回答正确为止。
  • 你说得对,直到事后我才注意到颜色发生了变化。谢谢你回答那个。对我的做法感到抱歉。我投了赞成票。感谢您的耐心等待
猜你喜欢
  • 2015-07-23
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
  • 1970-01-01
相关资源
最近更新 更多