RFE: shortcut for [xyz|]label and [xyz]lim?

Hi

Hope this is the right place to post a request for enhancement.

I often create a bunch of relatively basic plots using matplotlib and
the commands to set the labels and limits take up more space than the
actual plotting commands (figure, plot, show), so I was wondering if
there is a shorter way of doing this (I couldn't find one) and if not,
if a shortcut notation could be added.

Here are some code lines I use at the moment:

3d plot:

ax.set_xlabel('Time [$\mu$s]')
ax.set_xlim3d(-0.2, 0.9)
ax.set_ylabel('Bias [V]')
ax.set_ylim3d(-100, 100)
ax.set_zlabel('Voltage[V]')
ax.set_zlim3d(-0.3, 0.4)

2d plot:

plt.xlabel('Time [$\mu$s]')
plt.ylabel('Voltage [V]')
plt.xlim(0, 100)
plt.ylim(0, 50)

proposed syntax:

# Z being optional
plt.labels(X='Time [$\mu$s]', Y='Bias [V]', Z='Voltage[V]')
plt.limits(X=[-0.2, 0.9], Y=[-100,100], Z=[-0.3, 0.4])

label could also have a **kwargs that would be handed on to all
[xyz]label, in case one needs to set fontsize for all labels.

label could also have an optional title=''.

limits could test for 2d or 3d plots and call the correct functions
automatically.

Arun

IIRC, you can use plt.setp() for this purpose:
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.setp

Essentially, anything that would come after the “set_” part of an object’s method can be a keyword. So, I think this would work:

plt.setp(ax, xlim=[-0.2, 0.9], ylim=[-100,100], zlim=[-0.3, 0.4])

plt.setp(ax, xlabel=‘Time [$\mu$s]’, ylabel=‘Bias [V]’, zlabel=‘Voltage[V]’)

Note, you no longer need to say “xlim3d” and the likes, it is just “xlim”, “ylim” and “zlim” (as of v1.1, IIRC).

Again, completely untested, and off the top of my head.

Cheers!

Ben Root

···

On Wed, Feb 5, 2014 at 3:49 PM, Arun Persaud <apersaud@…1185…> wrote:

Hi

Hope this is the right place to post a request for enhancement.

I often create a bunch of relatively basic plots using matplotlib and

the commands to set the labels and limits take up more space than the

actual plotting commands (figure, plot, show), so I was wondering if

there is a shorter way of doing this (I couldn’t find one) and if not,

if a shortcut notation could be added.

Here are some code lines I use at the moment:

3d plot:

ax.set_xlabel(‘Time [$\mu$s]’)

ax.set_xlim3d(-0.2, 0.9)

ax.set_ylabel(‘Bias [V]’)

ax.set_ylim3d(-100, 100)

ax.set_zlabel(‘Voltage[V]’)

ax.set_zlim3d(-0.3, 0.4)

2d plot:

plt.xlabel(‘Time [$\mu$s]’)

plt.ylabel(‘Voltage [V]’)

plt.xlim(0, 100)

plt.ylim(0, 50)

proposed syntax:

Z being optional

plt.labels(X=‘Time [$\mu$s]’, Y=‘Bias [V]’, Z=‘Voltage[V]’)

plt.limits(X=[-0.2, 0.9], Y=[-100,100], Z=[-0.3, 0.4])

label could also have a **kwargs that would be handed on to all

[xyz]label, in case one needs to set fontsize for all labels.

label could also have an optional title=’’.

limits could test for 2d or 3d plots and call the correct functions

automatically.

Arun


Managing the Performance of Cloud-Based Applications

Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.

Read the Whitepaper.

http://pubads.g.doubleclick.net/gampad/clk?id=121051231&iu=/4140/ostg.clktrk


Matplotlib-devel mailing list

Matplotlib-devel@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

<snip>

Just to elaborate on what Ben said, all matplotlib artists have a "set"
method. E.g.:

    ax.set(xlim=[min, max], ylim=[min, max], xlabel='blah')

"plt.setp" basically just calls "set", but it will also operate on
sequences of artists. Therefore you can do things like:

fig, axes = plt.subplots(nrows=2, ncols=2)
plt.setp(axes.flat, aspect=2, ...)

Some people prefer the "Tk-style" set method to using "setp" if you're
operating on a single artist.

Keep in mind that it also works for other artists, not just axes. At any
rate, "setp" and the "set" method are certainly handy to know about!

Cheers,
-Joe

···

On Wed, Feb 5, 2014 at 3:46 PM, Benjamin Root <ben.root@...553...> wrote:

IIRC, you can use plt.setp() for this purpose:
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.setp

Essentially, anything that would come after the "set_" part of an object's
method can be a keyword. So, I think this would work:
plt.setp(ax, xlim=[-0.2, 0.9], ylim=[-100,100], zlim=[-0.3, 0.4])
plt.setp(ax, xlabel='Time [$\mu$s]', ylabel='Bias [V]',
zlabel='Voltage[V]')

Hi

Just to elaborate on what Ben said, all matplotlib artists have a "set"
method. E.g.:

    ax.set(xlim=[min, max], ylim=[min, max], xlabel='blah')

"plt.setp" basically just calls "set", but it will also operate on
sequences of artists. Therefore you can do things like:

great! exactly what I was looking for :slight_smile:

Thanks

Arun