Skip to content
Advertisement

python use argparse for passing optional arguments for plotting

I would like to pass optional arguments to a matplotlib script running from the command line.

I have the following:

JavaScript

However, currently I am not able to do so. Is there any way to pass under the optional arguments other parameters to the plot functions? Set colors, markers, etc.. ?

Best Regards

Advertisement

Answer

If you will run as

JavaScript

then you will get list

JavaScript

and you can use split("=") and dict()

JavaScript

to get it as dictionary

JavaScript

And then you can use ** to put dictionary

JavaScript

and it will accept it as correct parameters.


Minimal working code:

For test I put argumenst directly in sys.argv – so I can run it without writing parameters in console.

EDIT: it needs dictionary default={} instead of default=[]

JavaScript

Console:

JavaScript

EDIT:

I found also method to create own class based on argparse.Action and later assign it to action in add_param(..., action=...)

JavaScript
JavaScript

Full working code

JavaScript

Source:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement