matplotlib.pyplot scatterplot lines using lists for x-coordinates, y-coordinates, and colors

独自空忆成欢 提交于 2019-12-25 17:08:42

问题


Matplotlib connect scatterplot points with line - Python

Checked this out and their solution was very simple, use plt.plot(x_coordinates, y_coordinates, '-o') but I have a list of colors that i'm using so I can't use this method. They are RGB colors. (Also not sure why colors are alternating within the same series)

How can I connect these points with lines that are the same color as the markers?

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

回答1:


You can plot the line behind the scatter plot, and set the zorder to ensure that the line is behind the points.

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


来源:https://stackoverflow.com/questions/31330136/matplotlib-pyplot-scatterplot-lines-using-lists-for-x-coordinates-y-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!