loop through x and y axes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

loop through x and y axes

Andrei Berceanu
Hi all,

Please consider this code:

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=[14,4])

for ax in (ax1, ax2):
    ax.set_xlabel(labels[0])
    ax.set_ylabel(labels[1])

This could be simplified if I could just loop though the x and y axes of each subplot, something like:

for index, ax in enumerate((ax1, ax2)):
    ax.set_label[index](labels[index])

Is there some way of achieving this in Matplotlib? From what I found, it seems one can only refer to the axes using x and y, and so I don't know how to loop over both of them.

Thank you,
Andrei
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: loop through x and y axes

Joe Kington
It sounds like you want to use the ``set_label_text`` method of the ``Axis`` instances directly?  Perhaps something like:

import matplotlib.pyplot as plt

labels = ['xlabel', 'ylabel']

fig, axes = plt.subplots(ncols=2)
for ax in axes:
    for axis, label in zip([ax.xaxis, ax.yaxis], labels):
        axis.set(label_text=label)

fig.tight_layout()
plt.show()


However, that's not really much cleaner than doing:


import matplotlib.pyplot as plt

fig, axes = plt.subplots(ncols=2)
for ax in axes:
    ax.set(xlabel='xlabel', ylabel='ylabel')

fig.tight_layout()
plt.show()

However, I might be misunderstanding your question entirely...  Hope that helps a bit, anyway.
-Joe

On Fri, Oct 27, 2017 at 6:17 AM, Andrei Berceanu <[hidden email]> wrote:
Hi all,

Please consider this code:

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=[14,4])

for ax in (ax1, ax2):
    ax.set_xlabel(labels[0])
    ax.set_ylabel(labels[1])

This could be simplified if I could just loop though the x and y axes of each subplot, something like:

for index, ax in enumerate((ax1, ax2)):
    ax.set_label[index](labels[index])

Is there some way of achieving this in Matplotlib? From what I found, it seems one can only refer to the axes using x and y, and so I don't know how to loop over both of them.

Thank you,
Andrei
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: loop through x and y axes

Jody Klymak
In reply to this post by Andrei Berceanu

How about, as recently pointed out by @anntzer on github:

```python
fig, axs = plt.subplots(1, 2, figsize=[14, 4])
plt.setp(axs, ‘xlabel’, labels[0], ‘ylabel’, labels[1])
```

or, suppose you don’t want to repeat labels except on outer

```python
fig, axs = plt.subplots(3, 3, figsize=[14, 4])
plt.setp(axs[-1,:], ‘xlabel’, labels[0])
plt.setp(axs[0,:], ‘ylabel’, labels[1])
```

Cheers,  Jody


> On 27 Oct 2017, at  4:17 AM, Andrei Berceanu <[hidden email]> wrote:
>
> Hi all,
>
> Please consider this code:
>
> fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=[14,4])
>
> for ax in (ax1, ax2):
>    ax.set_xlabel(labels[0])
>    ax.set_ylabel(labels[1])
>
> This could be simplified if I could just loop though the x and y axes of each subplot, something like:
>
> for index, ax in enumerate((ax1, ax2)):
>    ax.set_label[index](labels[index])
>
> Is there some way of achieving this in Matplotlib? From what I found, it seems one can only refer to the axes using x and y, and so I don't know how to loop over both of them.
>
> Thank you,
> Andrei
> _______________________________________________
> Matplotlib-users mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/matplotlib-users

--
Jody Klymak    
http://web.uvic.ca/~jklymak/





_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users