Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ValueError: Unrecognized marker style -d" when looping over markers

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?

like image 531
MaximizedAction Avatar asked Aug 04 '15 12:08

MaximizedAction


1 Answers

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

like image 92
tmdavison Avatar answered Sep 21 '22 22:09

tmdavison