0

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()
Community
  • 1
  • 1
O.rka
  • 29,847
  • 68
  • 194
  • 309

1 Answers1

1

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

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()
tom10
  • 67,082
  • 10
  • 127
  • 137
  • is there any way to get the colors to match the markers? – O.rka Jul 11 '15 at 01:29
  • 1
    @O.rka: Yes there is, but it's a different question (and I'm not even sure what the question is exactly when the markers change colors on the line). I've already answered the question you originally posted. I'd prefer you accept it (if it answers your original question) and then post your new question as a new question (which I'd be interested in answering as well). But, I don't want to keep chasing this question, answering more and different things until you finally get your question right. – tom10 Jul 11 '15 at 14:02
  • you're right, I didn't notice that the colors were changing until after the fact. thanks for answering that one. sorry about the way I went about that . I upvoted it. thanks for your patience – O.rka Jul 11 '15 at 15:31