Skip to content

colorbar might shrink plots if used with twinxย #8823

Closed
@PrometheusPi

Description

@PrometheusPi

Bug report

Changing plot alignment for different placing of colorbar() in twinx plot

If one combines a pcolormesh plot/hist2d plot with a standard plot by using twinx, the graph from the plot moves to wrong x values if the colorbar is called after the plot call. If it is called before, the alignment is fine.

I am not sure if this is a bug or just wrong usage.
However, one of the resulting graphs has a wrong extent that might lead to misinterpretations.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

data = np.random.random((64, 128)) * 2.0
x = np.arange(128)
y = np.arange(64)
plt.pcolormesh(x, y, data)

#plt.colorbar() --> here it works fine
plt.twinx()
plt.colorbar()
# --> here it gives wrong x values for the orange plot

plt.plot(x, np.sin(x/10), color="orange", lw=4)

plt.show()

Actual outcome

misaligned_twinx_wrong

Next to the confusing layout the plot shows actually a wrong graph:
the orange plot just goes to around x=100 but should go to x=128.
(The axis of the colorbar is correct and shows the range of data.)

Expected outcome

misaligned_twinx_correct

If colorbar is called before twinx, the orange graph has the correct extent.

I am not sure if this is a bug or a wrong usage of matplotlib. In any case the wrong extent of the (in this case) orange plot is a dangerous behavior that might lead to misinterpretations of the data.

Matplotlib version

  • Operating System: Ubuntu 14.04.1
  • Matplotlib Version: 2.0.0rc1
  • Python Version: 3.4.3
  • Jupyter Version: 1.0.0 (I am using %matplotlib inline)
  • Other Libraries: numpy 1.12.0b1

I installed matplotlib via pip.

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