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

Plots using twinx draw on top of figure frame #2472

Merged
merged 1 commit into from Oct 14, 2013

Conversation

mdboom
Copy link
Member

@mdboom mdboom commented Oct 1, 2013

While attempting to plot two figures on the same x axis, I noticed that plots were drawing pixels on top of the figure frame. This only seems to happen from Axes derived from a twinx() call. Sample code below should reproduce the problem.

In the image below, the green plot and fill is on top of the frame on the left, bottom, and right.
overlay

Currently using matplotlib 1.3.0 through Anaconda x64 1.9.1 on Windows

import numpy, matplotlib
fig, aAxes = matplotlib.pyplot.subplots(1, 1, figsize = (11, 8.5))

data = numpy.array([[1000, 1100, 1200, 1250, 1300, 1340, 1400, 1500, 1600, 1650, 1700,
                     1750, 1800, 1804, 1850, 1900, 1910, 1920, 1927, 1930, 1940, 1950,
                     1960, 1970, 1974, 1980, 1987, 1990, 1999, 2000, 2005, 2007, 2008,
                     2011],
                    [ 310,  301,  360,  400,  360,  443,  350,  425,  545,  470,  600,
                      791,  970, 1000, 1262, 1650, 1750, 1860, 2000, 2070, 2300, 2519,
                     2982, 3692, 4000, 4435, 5000, 5263, 6000, 6070, 6500, 6576, 6707,
                     7000]])
LogPop = aAxes
LogPop.set_xlabel('Year')

LinPop = LogPop.twinx()
LinPop.set_ylabel('World Population, Billions')
LogPop.set_ylabel('World Population, Logarithmically Scaled')
LinPop.set_xlim(1000, 2010)
LinPop.set_ylim(0, 8)
LogPop.set_ylim(0.091, 13.3)

LinPop.set_xscale('linear')
LogPop.set_yscale('log', nonposy="clip")

pLogPop, = LogPop.plot(data[0], data[1]/1E3, label = 'Logarithmic', color = '#BEAED4')
fLogPop = LogPop.fill_between(data[0], data[1]/1E3, color = '#BEAED4', alpha=.8)

pLinPop, = LinPop.plot(data[0], data[1]/1E3, label = 'Linear', color = '#7FC97F')
fLinPop = LinPop.fill_between(data[0], data[1]/1E3, color = '#7FC97F', alpha=.5)

assert(LinPop.get_zorder() == LogPop.get_zorder())

LinPop.grid()

aAxes.legend((pLinPop, pLogPop), (pLinPop.get_label(), pLogPop.get_label()), loc=2)

@Redoubts
Copy link
Author

As a related issue, the twinx Axis is always on top of the base Axis. In this case, the green chart will always be on top of the purple chart, regardless of the zorder applied in the plot commands.

@mdboom
Copy link
Member

mdboom commented Oct 1, 2013

A possible fix is attached.

@Redoubts
Copy link
Author

Redoubts commented Oct 2, 2013

Looks good, diff fixes it on my end.

mdboom added a commit that referenced this pull request Oct 14, 2013
Plots using twinx draw on top of figure frame
@mdboom mdboom merged commit 95cfcae into matplotlib:v1.3.x Oct 14, 2013
pelson added a commit that referenced this pull request Oct 29, 2013
* upstream/v1.3.x:
  Update version to 1.3.1
  tiny correction of matplotlibrc.template
  minor docstring fix in dates.py
  Updated the docs of pyplot.gca.
  Fix #2472: Draw the spines on top of twin axes
  changes to c++ because gcc 4.8 seems to allow things the compiler on travis will not.
  added class variable `_fill_color` to RendererAgg to hold the color to use in `clear` calls to the render_base.
  patch to fix issue #2473.

Conflicts:
	lib/matplotlib/__init__.py
	lib/matplotlib/axes/_axes.py
@mdboom mdboom deleted the twin-spines-on-top branch August 7, 2014 13:49
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 this pull request may close these issues.

None yet

2 participants