Skip to content
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

Boxplot resets x-axis limits and ticks #2921

Closed
breedlun opened this issue Mar 24, 2014 · 4 comments · Fixed by #2922
Closed

Boxplot resets x-axis limits and ticks #2921

breedlun opened this issue Mar 24, 2014 · 4 comments · Fixed by #2922
Milestone

Comments

@breedlun
Copy link
Contributor

Boxplot appears to reset the x-axis limits and ticks. For example, the following code

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = plt.subplot()
ax.set_xlim(0, 4)
ax.set_xticks([0, 0.5, 1, 2, 3.5, 4])
ax.set_xticklabels(['a','b','c', '2', '3.5', '4']) 
ax.set_ylim(0, 8)
ax.set_yticks([0, 0.5, 1, 2, 3.5, 4, 8])
plt.savefig('before_boxplot.png')

produces this plot
before_boxplot
But when I execute this code next

y1 = np.random.normal(10, 3, 20)
y2 = np.random.normal(3, 1, 20)
ax.boxplot([y1, y2], positions = [1,2])
plt.savefig('after_boxplot.png')

the plot changes to
after_boxplot
Three things changed:

  1. The x-axis limits used to be 0 to 4. They are now roughly 0.5 to 2.5.
  2. The tick marks used to be at [0, 0.5, 1, 2, 3.5, 4]. They are now at [1, 2].
  3. The tick mark labels used to be ['a','b','c', '2', '3.5', '4']. They are now ['a', 'b']. Based on the changes in (1) and (2) I would have expected the tick mark labels to be ['c', '2'].

The obvious work around is to set these parameters after you have called axes.boxplot(). However, reseting the axes is not consistent with axes.plot(), axes.bar(), axes.imshow(), or any other plotting method I have used. Other methods respect the user specified x limits and tick marks. This behavior also is not consistent with the y-axis behavior in boxplot. Boxplot respected the user specified y limits and tick marks.

I am working with Matplotlib 1.3.0.

@tacaswell
Copy link
Member

Does this also happen on master?

tacaswell added a commit to tacaswell/matplotlib that referenced this issue Mar 24, 2014
@tacaswell
Copy link
Member

You are misunderstanding how ticks/tick labels behave. When you use set_xticks and set_xticklabels you are really creating 'FixedLocatorandFixedFormatter` objects. The former says 'put ticks at these places', the second says 'use this list of strings to generate the tick labels'. What is going on is that box-plot is changing the xticks so that the ticks fall under the boxes (which is 99% of the time the correct behaviour). The first two strings in the tick label list ('a' and 'b') are then used, which is the correct (if not desired) behaviour on the part of the library.

I have made a PR to add a flag to make box_plot managing the xticks optional.

@breedlun
Copy link
Contributor Author

Ok. I agree that 99% of the time the x-ticks should fall under the boxes. I am also ok with boxplot grabbing the first two strings in the tick label list, rather than the two labels that previously corresponded to x =1 and x = 2. Thanks for making xtick management optional.

What about the x-axis limits? In my opinion, boxplot should not adjust user specified axis limits.

@tacaswell
Copy link
Member

The call to setlim is also under the flag (you have to do both or neither, other wise you could end up with most of the boxes not visible).

tacaswell added a commit to tacaswell/matplotlib that referenced this issue Apr 4, 2014
Closes matplotlib#2921
 - added test for managa_xtick option to boxplot
 - added CHANGELOG and whats_new.rst entry for manage_xticks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants