Skip to content

setting log major ticks leaves interfering minor tick labels #8386

Closed

Description

@fabrivelas

Bug report

contourf produces wrongly formatted plot labels for logarithmic plots when there is only one order of magnitude or less of range of data (eg. 10 to 100 or 100 to 1000).

Code for reproduction

import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter
import numpy as np
t = np.linspace(0,1000,1001)
p = np.array([10,20,30,40,50,60,70,80,90,100])
data = np.random.random_sample((len(p),len(t)))
pressure,time = np.meshgrid(p,t)

fig, ax = plt.subplots(nrows=2)
ax1, ax2 = tuple(ax)
ax1.contourf(time,pressure,data.T)
ax1.set_yscale('log')
ax1.set_yticks([10,30,100])
ax1.get_yaxis().set_major_formatter(StrMethodFormatter('{x:.0f}'))
ax2.plot(p)
ax2.set_yscale('log')
ax2.set_yticks([10,30,100])
ax2.get_yaxis().set_major_formatter(StrMethodFormatter('{x:.0f}'))
plt.show()

Actual outcome
Using matplotlib 2.0.0 the labelling of the y-axis of the contour plot is messed up compared to the y-axis of the line plot.
logtest2
Remark: The default color map was used.

Expected outcome

Using matplotlib 1.5.1 the labelling of the y-axis of the contour plot is as beautiful as the labelling of the y-axis of the line plot.
logtest2
Remark: The default color map was used.

Matplotlib version

matplotlib 2.0.0 (installed using pip)
python 3.6.1 (installed using brew)
macos

Activity

  1. phobson commented on Mar 27, 2017

    @phobson
    Member

    Thanks for the reproducible example. I can confirm this.

    In the meantime, as a work around you can add:

    from matplotlib.ticker import StrMethodFormatter, NullFormatter
    ax1.yaxis.set_major_formatter(StrMethodFormatter('{x:.0f}'))
    ax1.yaxis.set_minor_formatter(NullFormatter())

    loglabels

  2. efiring commented on Mar 27, 2017

    @efiring
    Member

    This has nothing to do with contourf; the reason the the two axes in the example have different y-axis behavior is that their ylims differ.

    I don't think this is a bug at all; it is an inevitable consequence of the design change in v2.0, in which we label some or all minor ticks on a log scale when less than one decade is shown.

    The underlying design problem is that the locating and formatting of major and minor ticks is handled independently, so when coordination is needed it must be done manually. In this case that means explicitly setting the minor formatter. Even that is not really enough, though, because it still leaves a minor tick landing on a major tick, which is unintended and not always invisible.

  3. phobson commented on Mar 27, 2017

    @phobson
    Member

    @efiring thanks for the insight. feel free to remove the bug label if you think that's appropriate

  4. changed the title [-]contourf: wrong format for logarithmic plot labels[/-] [+]setting log major ticks leaves interfering minor tick labels[/+] on Mar 27, 2017
  5. efiring commented on Mar 27, 2017

    @efiring
    Member

    I'm going to close this. We are overwhelmed with open issues. Leaving this one open is not going to make it any more likely that we will make the major changes required to solve the fundamental underlying design problem.

  6. fabrivelas commented on Mar 28, 2017

    @fabrivelas
    Author
  7. efiring commented on Mar 29, 2017

    @efiring
    Member

    @fabrivelas, the behavior is the same in plt.plot when the axis limits are the same. The axis limits differ in your example because image-like artists set tight limits, while functions like plot provide a margin by default. This is a major part of the style change in 2.0, and will not be changed.

    The new default was chosen after careful consideration, and with a clear rationale: it solves a problem in a very common use case in which no modifications to the default values are made. In your case you are overriding defaults, so it is not unreasonable that you need to override the minor formatter and/or locator at the same time. We are thinking about a major overhaul that would provide an API in which there would be better coordination between major and minor ticks and formats, but if this is done at all, it will be quite some time in the future.

    The overlapping of major and minor labels is a consequence of each label being positioned relative to its tick marker; I think this is essential, and must not be changed.

  8. fabrivelas commented on Mar 30, 2017

    @fabrivelas
    Author

    @efiring: Thank you for the clarification. I was not aware that plot (function) was behaving differently from contourf (artist). I might be a bit obstinate, but when I change the default format for the major tick labels, should then not the format also change (automatically) for the minor tick labels, if they appear automatically? Or in stated differently: I have to be aware that the behaviour changes when only a decade is plotted because suddenly there appear differently formatted labels.

  9. efiring commented on Mar 31, 2017

    @efiring
    Member

    The difference between plot and contourf is not between functions and artists, but between the types of things being drawn. Contour, pcolormesh, imshow fill a frame; one rarely wants a margin. Line plots, scatter plots, etc. often benefit from a margin, so in the 2.0 style change we adopted the use of a small margin for them by default.

    See my earlier comment about a possible major overhaul. In the meantime, you are stuck with mpl as it is and as it evolves, and yes, you have to be aware of the good and the bad aspects of the way it works.

  10. fabrivelas commented on Apr 3, 2017

    @fabrivelas
    Author

    @efiring Thank you again. It would be nice though, if the automatically included tick labels had the same format as the major tick labels ;-) I actually like the new default i.e. the choice of the positions of the minor tick labels automatically included...

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions