159

I have two lists, dates and values. I want to plot them using matplotlib. The following creates a scatter plot of my data.

import matplotlib.pyplot as plt

plt.scatter(dates,values)
plt.show()

plt.plot(dates, values) creates a line graph.

But what I really want is a scatterplot where the points are connected by a line.

Similar to in R:

plot(dates, values)
lines(dates, value, type="l")

, which gives me a scatterplot of points overlaid with a line connecting the points.

How do I do this in python?

brno792
  • 6,479
  • 17
  • 54
  • 71

4 Answers4

204

I think @Evert has the right answer:

plt.scatter(dates,values)
plt.plot(dates, values)
plt.show()

Which is pretty much the same as

plt.plot(dates, values, '-o')
plt.show()

You can replace -o with another suitable format string as described in the documentation. You can also split the choices of line and marker styles using the linestyle= and marker= keyword arguments.

Hannes Ovrén
  • 21,229
  • 9
  • 65
  • 75
  • 6
    'linestyle' was the good keyword search clue for me to path through the (gynormous) matplotlib docs. – Reb.Cabin Feb 14 '17 at 19:00
  • 2
    @aaronsnoswell Size and color only makes sense for the points, which you can still plot via `scatter`, as per my first example. The question does not say anything about a varying line thickness or color, so I think your criticism is a bit unfair, really. – Hannes Ovrén Aug 24 '18 at 09:31
  • The keyword argument you want is not `linestyle` but `marker`. I'll add an answer because, somehow, this has not come up for this questin. – eric Mar 16 '21 at 21:43
  • Thank you for the correction @eric. I have updated the answer to describe format strings, marker and linestyle. – Hannes Ovrén Mar 23 '21 at 08:09
  • @HannesOvrén no worries that wasn't aimed at you as much as the first comment. :) – eric Mar 23 '21 at 15:22
  • Thanks, this is exactly what I was looking for! Using that, I can set a different marker style to a dedicated "special" point in my plot. – Patrick Apr 09 '21 at 21:33
39

For red lines an points

plt.plot(dates, values, '.r-') 

or for x markers and blue lines

plt.plot(dates, values, 'xb-')
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
  • It looks like you, Evert and kigurai are all right. You can call both scatter() and plot() before calling show(). Or just call plot() with the line and point attributes as Steve Barnes described. Thanks – brno792 Nov 21 '13 at 22:31
21

In addition to what provided in the other answers, the keyword "zorder" allows one to decide the order in which different objects are plotted vertically. E.g.:

plt.plot(x,y,zorder=1) 
plt.scatter(x,y,zorder=2)

plots the scatter symbols on top of the line, while

plt.plot(x,y,zorder=2)
plt.scatter(x,y,zorder=1)

plots the line over the scatter symbols.

See, e.g., the zorder demo

Use Me
  • 449
  • 5
  • 4
4

They keyword argument for this is marker, and you can set the size of the marker with markersize. To generate a line with scatter symbols on top:

plt.plot(x, y, marker = '.', markersize = 10)

To plot a filled spot, you can use marker '.' or 'o' (the lower case letter oh). For a list of all markers, see:
https://matplotlib.org/stable/api/markers_api.html

eric
  • 7,142
  • 12
  • 72
  • 138