Reputation: 117
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
Upvotes: 1
Views: 248
Reputation: 13475
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.
Upvotes: 1