Thanks to visit codestin.com
Credit goes to github.com

Skip to content

y-axis label no longer obeys x position in 2.0 #7946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
robroc opened this issue Jan 25, 2017 · 26 comments
Closed

y-axis label no longer obeys x position in 2.0 #7946

robroc opened this issue Jan 25, 2017 · 26 comments
Labels
Good first issue Open a pull request against these issues if there are no active ones! New feature status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action topic: ticks axis labels

Comments

@robroc
Copy link

robroc commented Jan 25, 2017

Using matplotlib with Seaborn's FacetGrid. Before upgrade to 2.0, setting the x,y position of the y-axis label worked. Now only y-position is respected.

import seaborn as sns
import matplotlib.pyplot as plt

g = (sns.FacetGrid(df, col="prenom", hue="sexe", col_wrap=3, size=3, sharey=False, legend_out=True)
 .map(plt.plot, "annee", "freq", ms=3, linewidth=3))

g.set(xlim=(1980, 2015), xticks=[1980, 1990, 2000, 2010], ylim=0)
g.set_titles('{col_name}')
g.fig.subplots_adjust(top = 0.91, wspace=0.3, hspace=0.3)
g.set_titles('{col_name}', size=13, fontweight='bold', ha='left')
g.set_axis_labels('', 'BABIES')

### This is the line that is not working ###
g.set_ylabels(rotation=0, position=(0, 1.04), color='grey')

g.fig.suptitle('Names that changed gender', x = 0.1, ha='left')
sns.plt.legend(loc='upper left',bbox_to_anchor=(1.5,1))
sns.despine()

It doesn't mater if I put 0 or 999999999 as the x value in the position parameter. The label is always in the same horizontal place.

Matplotlib version 2.0, Python 3.5 on Mac OSX El Capitan in Jupyter notebook
Matplotlib installed via conda update matplotlib

@WeatherGod
Copy link
Member

WeatherGod commented Jan 25, 2017 via email

@robroc
Copy link
Author

robroc commented Jan 25, 2017

You're right, my apologies. Here's an example in pure matplotlib. The x position is still ignored.

roberto = noms[noms.prenom == 'roberto'.upper()]
roberto = roberto.pivot_table('freq', index='annee', columns='sexe')

fig, ax = plt.subplots()
ax.title.set_text('Babies named Roberto')
ax.plot(roberto)
ax.axes.set_ylabel('BABIES', rotation=0, position=(2000,1.05))

baaaaaaelftksuqmcc

@tacaswell tacaswell added this to the 2.0.1 (next bug fix release) milestone Jan 25, 2017
@NelleV NelleV added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Jan 25, 2017
@NelleV
Copy link
Member

NelleV commented Jan 26, 2017

Here is the full code to reproduce the problem:

import matplotlib.pyplot as plt
import numpy as np

roberto = np.random.randint(0, 100, size=(10, ))

fig, ax = plt.subplots()
ax.title.set_text('Babies named Roberto')
ax.plot(roberto)
ax.set_ylabel('BABIES', rotation=0, position=(2000,1.05))

@NelleV
Copy link
Member

NelleV commented Jan 26, 2017

I have the same results on matplotlib 1.5 and matplotlib 2.0.
Which version of matplotlib are you using?

@NelleV NelleV removed the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Jan 26, 2017
@robroc
Copy link
Author

robroc commented Jan 27, 2017 via email

@dstansby
Copy link
Member

dstansby commented Feb 1, 2017

After a play around I've found that by setting position=(x,y), y is in figure co-ordinates, so y=0.5 puts the label halfway up the axis. Setting x=2000 (if it worked) would probably put the legend way off the the right of the figure.

I can't seem to be getting it working in 1.5.3 either, do you know what version you upgraded from?

@robroc
Copy link
Author

robroc commented Feb 1, 2017 via email

@dstansby
Copy link
Member

dstansby commented Feb 1, 2017

Hmm, I'm not convinced setting x location of a y-label or y location of an x-label ever worked. The relevant line which sets the label location I think is https://github.com/matplotlib/matplotlib/blame/master/lib/matplotlib/axis.py#L2161 which hasn't changed for at least 5 years.

It might be worth another dev having a look... In the meantime I'd recommend just adding a normal text box to the plot which should give you full control over position.

@dstansby dstansby modified the milestones: 2.1 (next point release), 2.0.1 (next bug fix release) Mar 18, 2017
@dstansby
Copy link
Member

Punting this to 2.1 as no-one has disagreed (yet) with my assertion that this never worked.

@dstansby
Copy link
Member

After a bit of a hunt, I think this is a duplicate of #7112

@tacaswell
Copy link
Member

Closing as I am not sure this ever worked.

@Harrypotterrrr
Copy link

Hi, is there any solution? I use the latest version of Matplotlib 3.2.1, but set_ylabels(position=(x,y)) where x parameter still not works.

@jklymak
Copy link
Member

jklymak commented Apr 21, 2020

@Harrypotterrrr This would be a feature request at this point. We just did a similar dance for title (#17127), so its possible to allow manual placement, but its not implemented for set_ylabel.

@jklymak
Copy link
Member

jklymak commented Apr 21, 2020

I'm going to re-open as something that would be nice to support. Though, really, if you are going to specify x, y, then why not just use text?

@jklymak jklymak reopened this Apr 21, 2020
@jklymak jklymak modified the milestones: v2.1, v3.4.0 Apr 21, 2020
@jklymak jklymak added topic: ticks axis labels Good first issue Open a pull request against these issues if there are no active ones! labels Apr 21, 2020
@jklymak
Copy link
Member

jklymak commented Apr 21, 2020

Marking as good first issue because someone could follow #17127 pretty easily....

@Harrypotterrrr
Copy link

@jklymak Thanks for your help! I have found a solution to this tricky but simple problem.
I managed to set the absolute coordinates of labels by using set_label_coords() but now I figured out a better solution, to use labelpad parameter of set_x/ylabel() method which could deal with overlap problem.

@jklymak
Copy link
Member

jklymak commented Apr 21, 2020

There should not be an overlap problem. Perhaps you can share your code, maybe in a different issue.

@fperonaci
Copy link

fperonaci commented Oct 16, 2020

Hi, in my case I would like to manually align the ylabel of ax2 with the ylabel of ax1, without modifying the latter.
This is strictly related to what was already discussed here. The following would seem pretty reasonable to me:

coords = ax1.yaxis.get_label_coords()
ax2.yaxis.set_label_coords(coords)

However, the YAxis.get_label_coords method is not implemented.
A workaround would be:

coords = ax1.yaxis.get_label().get_position()

But in this case the x coordinate is junk.

  1. Can you think of a workaround for this?
  2. I suggest some improvements:
    2a. The method YAxis.get_label_coords should be implemented.
    2b. Yaxis.get_label().get_position should return the true position.
    2c. Yaxis.get_label().set_position should work as discussed previously.

@jklymak
Copy link
Member

jklymak commented Oct 16, 2020

The postion of the ylabel is not known until a draw is made so calling fig.canvas.draw() first is needed.

@jklymak
Copy link
Member

jklymak commented Oct 16, 2020

BTW, I assume we are all aware of: https://matplotlib.org/gallery/pyplots/align_ylabels.html

@fperonaci
Copy link

@jklymak I imagined there was a similar problem. Still, as a user it is frustrating not to have the getter method corresponding to the setter method, as well as it is frustrating to obtain a junk coordinate out of Yaxis.get_label().get_position.

I am aware of align_ylabels but it seems that this takes as reference the label which is farthest from the axis. In my case I want to align according to the one which is closest to the axis.

@fperonaci
Copy link

I understand that label coordinates change dynamically, in particular upon changing the tick labels, I guess. But isn't it so also for other quantities? For example, the limits change according to the lines which are plotted. Still, at any time I can call ax.get_xlim() and I get the limits at that time.

@jklymak
Copy link
Member

jklymak commented Oct 16, 2020

Sure, because we trigger a recalc when users ask for the x/ylims. But every other possible thing that can get changed at draw time doesn't necessarily get a recalc just because no one has put the hooks in. However, in this case, you basically have to recalculate everything, so you may as well just do a full draw.

I'm curious why you want to align with the closest label. That will presumably overwrite the tick labels on the axes you are moving the label on.

@fperonaci
Copy link

fperonaci commented Oct 16, 2020

I understand, thanks for the explanation.

Well, it's because tick labels have different width, e.g. the label for the value -1 is very large compared to, say, the labels for 1,2,3, etc. In my case I only have one label with the minus sign which is pretty far from the axis label. So it is OK to have the label nominally overlapping the tick labels, since in practice it does not (they are physically far apart). Actually, it looks better to have it closer to the axis.

For what concerns me, now I have understood how this works, and that is fine.
However, maybe it is good to get rid of some inconsistencies on the long run.

So for example, compare:

ax = plt.gca()
ax.xaxis.set_label_text('x-label')
ax.xaxis.set_label_coords(0.5,0.5)

with

ax = plt.gca()
ax.set_xlabel('x-label',position=(0.5,0.5))

It seems reasonable that the two snippets should do the same, but they do not.

@jklymak
Copy link
Member

jklymak commented Oct 16, 2020

Sure, I agree - thats the suggested fix, is for someone to make the automatic label repositioning over-rideable with manual values if desired, like we did in #17127 for the title.

Copy link

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Jan 26, 2024
@github-actions github-actions bot added the status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. label Feb 26, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good first issue Open a pull request against these issues if there are no active ones! New feature status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action topic: ticks axis labels
Projects
None yet
Development

No branches or pull requests

9 participants