1

I have a module for standardized plotting, and would like to pass it a tuple with line attributes for each line in the plot. Ideally it would work like the code fragment below...where I've abstracted the tuple as a simple string for simplicity. This generates an error because it doesn't know how to parse my string.

import matplotlib.pyplot as plt
x=range(10)
y=range(10)
myStyle = "'b-',linewidth=2.0"
plt.figure(1)
plt.plot(x,y,myStyle)

ValueError: Unrecognized character ' in format string

Can that be implemented in another way? Or..is there an alternate solution (akin to Matlab) where I assign the line a handle and access its line attributes in a loop like this?

myStyl = (["color=b","linestyle='-'","linewidth=1.5"],  )
lh = plt.plot(x,y)
for ii in range(len(myStyle[0]))
    plt.setp(lh,myStyle[0][ii])          #<----what goes here 
Cœur
  • 37,241
  • 25
  • 195
  • 267
Joe Gavin
  • 117
  • 1
  • 7

1 Answers1

1

You can use, for example, eval:

import matplotlib.pyplot as plt
x=range(10)
y=range(10)
myStyle = "'b-',linewidth=2.0"
plt.figure(1)
eval("plt.plot(x,y,"+myStyle+")")
plt.show()

But be careful with this. Check a question like this to be more informed about this option.

Community
  • 1
  • 1
armatita
  • 12,825
  • 8
  • 48
  • 49
  • @armattia: indeed this is a good solution. I finally found documentation on setting line attributes but upon testing it appears that the keywords must be explicit. . [Documentation is here.](http://matplotlib.org/examples/pylab_examples/set_and_get.html) – Joe Gavin Apr 16 '16 at 14:47