2

I am creating some functions to create quality figures of a standard form using matplotlib. I typically create line plots, contour plots, etc. Is there a way to pass parameters (axis limits, tick locations, etc.) to matplotlib that instructs matplotlib to use the default values of the parameters? For example

I create a class

class my_class(object):
    xlimit = *something matplotlib reads as "use default"*
    ylimit = *something matplotlib reads as "use default"*
    linwidth = *something matplotlib reads as "use default"*

I create an instance of this class, but I only want to specify the xlimit

inst = myclass()
inst.xlimit = [0,1]

And I leave the ylimit unchanged, so matplotlib treats it as default.

I then have a function

def plot_func(x,y,fig,inst):

    ax = fig.gca()
    ax.set_xlim(inst.xlimit)
    ax.set_ylim(inst.ylimit)
    ax.plot(x,y,linewidth=inst.linwidth)

    return fig

Which I can call and pass my class instance. So far this is working for me just fine, but I have to set values for all my plotting parameters in my_class().

Is there a way to set these values in my_class() to something, so that if I decide I just want matplotlib's default calculations for things like limits, ticks, labels, etc. I can still pass these 'variables' to my function plot_func() where they will all be set and I can customize what I want, and leave everything else as default?

Just to avoid these answers, I am aware of matplotlib's rc settings, this is not what I am looking for.

2

2 Answers 2

1

Since almost all things in matplotlib are set by kwargs, you could define all your possible optional settings as None, unless you are going to set them manually.

Then, when you call ax.plot or ax.set_xlim, you can pass in your optional setting, so if the function receives the null value you get the default setting, otherwise you get your manually defined option.

class my_class(object):
    xlimit = (None, None)
    ylimit = (None, None)
    linewidth = None
    color = None


def plot_func(x,y,fig,inst):

    ax = fig.gca()
    ax.plot(x, y, linewidth=inst.linewidth, color=inst.color)
    ax.set_xlim(inst.xlimit)
    ax.set_ylim(inst.ylimit)

    return fig

Then, if we call this with the default settings, we get:

x = range(5)
y = range(5)

fig, ax = plt.subplots(1)
inst1 = my_class()
plot_func(x, y, fig, inst1)

fig.savefig('default_options.png')

enter image description here

Otherwise, we can change any settings we like:

ax.cla()

inst2 = my_class()
inst2.xlimit = [-2, 8]
inst2.linewidth = 5
inst2.color = 'r'

plot_func(x, y, fig, inst2)

fig.savefig('custom_options.png')

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

This is a very feasible solution. It may make for a messy function with all the if comparisons but it absolutely get the job done. Thanks!
I went to implement this and realized there is still a sticking point. When parameters are set inside the plt.plot() command (such as linewidth, color, contour levels, etc.) there is no way to one-by-one check to see which parameters are being set and which are still default. I have edited my question.
Ah I had tried null argument with axis limits but inside a list not tuple, that makes sense now. Thanks for your help.
1

The key to this question is the ability to use None as a default. You can create your own class, but for simpler things, such as only setting axis limits, you can simply do something like the code below, no need to create and instantiate a special class.

    def plot_func(x,y,xlimit=[None, None],ylimit =[None, None]):

    ax = fig.gca()
    ax.plot(x, y)
    ax.set_xlim(xlimit)
    ax.set_ylim(ylimit)

    return fig

You can either call this with plot_func(x,y) when you simply want to plot without setting limits or plot_func(x,y,xlimit=[0,10]) assuming you want to limit the x range between 0 and 10 or some other arbitrary range

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.