Skip to content

Boxplot resets x-axis limits and ticks #2921

Closed

Description

@breedlun

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.

Activity

  1. tacaswell commented on Mar 24, 2014

    @tacaswell
    Member

    Does this also happen on master?

  2. added a commit that references this issue on Mar 24, 2014
    f594eb3
  3. tacaswell commented on Mar 24, 2014

    @tacaswell
    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.

  4. added a commit that references this issue on Mar 24, 2014
    bdda1d5
  5. added this to the v1.4.0 milestone on Mar 24, 2014
  6. breedlun commented on Mar 24, 2014

    @breedlun
    ContributorAuthor

    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.

  7. tacaswell commented on Mar 24, 2014

    @tacaswell
    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).

  8. added a commit that references this issue on Apr 4, 2014
    8c9f4d4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions