5

I'm trying to code pyplot plot that allows different marker styles. The plots are generated in a loop. The markers are picked from a list. For demonstration purposes I also included a colour list. The versions are Python 2.7.9, IPython 3.0.0, matplotlib 1.4.3.

Here's a simple code example:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

x=np.arange(0,10)
y=x**2

markers=['d','-d','-']
colors=['red', 'green', 'blue']

for i in range(len(markers)):
    plt.plot(x, y, marker=markers[i], color=colors[i])
    plt.show()

This manages to produce only the plot for marker='d', for the marker '-d' it returns the error:

...
C:\Users\batman\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\markers.pyc in set_marker(self, marker)
    245                 self._marker_function = self._set_vertices
    246             except ValueError:
--> 247                 raise ValueError('Unrecognized marker style {}'.format(marker))
    248 
    249         self._marker = marker

ValueError: Unrecognized marker style -d

But, I can get it to work by writing plt.plot(x, y, markers[i], color=colors[i]) instead. The color=... in comparison works.

I assume it has something to do with the the special characters. I tried other markers: The markers .,* work. The markers :,-- don't.

I tried prefixing the marker string with r or u (with # -*- coding: utf-8 -*-). Didn't help.

Do I have to escape the marker strings or what's wrong here?

K.Sy
  • 90
  • 1
  • 3
  • 10

1 Answers1

10

markers describe the points that are plotted with plt.plot. So, you can have, for example, 'o', 's', 'D', '^', 'v' '*', '.', ',', etc. See here for a full list of available options.

'-', '--', ':', '-.' are not markers, but linestyles.

So, it depends what you are trying to plot. If you want one Axes object with diamond markers, another with diamond markers and a line, and a final one with just a line, you could do:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

x=np.arange(0,10)
y=x**2

markers=['d','d','None']
lines=['None','-','-']
colors=['red', 'green', 'blue']

for i in range(len(markers)):
    # I've offset the second and third lines so you can see the differences
    plt.plot(x, y + i*10, marker=markers[i], linestyle=lines[i], color=colors[i])

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • Thank you! I indeed missed the difference between `linestyle` and `marker` for `pyplot.plot()` in the documentation. It looks like I got confused from being used to using the `fmt` (keyword-) argument either from `pyplot.errorbar(...,fmt='-d')` (where it has the identifier `fmt`) or `pyplot.plot(...,'-d')` (where it doesn't have that identifier). Add to that that while the doc entry for errorbar contains its call signature, the latter doesn't and mentions `fmt` only in the text. Enough for me to miss it. Now I know. :) – K.Sy Aug 04 '15 at 14:14