Setting line attributes programmatically in matplotlib

Joe Gavin

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 
armatita

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python Seaborn Matplotlib setting line style as legend

From Dev

setting a solid line using dashes keyword argument in matplotlib?

From Dev

create xml with attributes programmatically

From Dev

Setting an alias with attributes in PowerShell

From Dev

Setting runtime attributes on NSLayoutConstraint

From Dev

Thymeleaf: Setting attributes with namespace

From Dev

Setting file attributes in Swift

From Dev

Setting runtime attributes on NSLayoutConstraint

From Dev

jQuery setting attributes with functions

From Dev

setting mdButton attributes conditionally

From Dev

Programmatically setting TextView background

From Dev

Setting the value of a PopUpMenuButton programmatically?

From Dev

Setting stylus variables programmatically

From Dev

Setting GoogleMapOptions programmatically

From Dev

Setting defaultValue for Preference programmatically

From Dev

Setting textSize programmatically

From Dev

Programmatically setting button image

From Dev

Setting button style programmatically

From Dev

Setting gradient programmatically

From Dev

Programmatically setting a list identifier

From Dev

Setting defaultValue for Preference programmatically

From Dev

Setting HubSection ContentTemplate programmatically

From Dev

Setting GoogleMapOptions programmatically

From Dev

Setting titles programmatically in a UITabBarController

From Dev

Setting LayoutParams programmatically

From Dev

Programmatically setting SearchView to be open

From Dev

Programmatically setting TextView background

From Dev

Setting LayoutParams programmatically not working

From Dev

Setting the view programmatically