8000 contour and contourf treat levels differently · Issue #10572 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
8000
8000

contour and contourf treat levels differently #10572

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

Closed
boeleman opened this issue Feb 23, 2018 · 4 comments · Fixed by #11412
Closed

contour and contourf treat levels differently #10572

boeleman opened this issue Feb 23, 2018 · 4 comments · Fixed by #11412
Milestone

Comments

@boeleman
Copy link

Bug report

Bug summary

When setting levels that span a wider range of values than the data set, contourf correctly picks the right colors to fill the areas between the contour lines. contour does not use the same colors.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

levels = np.arange(-100, 100, 10)

x = np.loadtxt(open('x.txt','rb'),delimiter=',')
y = np.loadtxt(open('y.txt','rb'),delimiter=',')
z = np.loadtxt(open('z.txt','rb'),delimiter=',')

#CS = plt.contour(x, y, z, levels)
CS = plt.contourf(x, y, z, levels)

CB = plt.colorbar(CS)

plt.show()

The data is attached.

Actual outcome

When plotting with contour instead of contourf the colors range is scaled according to the maximum and minimum of the used data.

Expected outcome

I would expect contour to respect the range set in levels.

Matplotlib version

  • Operating system: Debian unstable
  • Matplotlib version: 2.1.1-2
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version:
  • Jupyter version (if applicable):
  • Other libraries:

z.txt
y.txt
x.txt

Installed from Debian package

@boeleman
Copy link
Author

@dstansby
Copy link
Member
dstansby commented Apr 8, 2018

Could you post an example that uses generated data instead of data in files? Maybe using np.random.rand?

@boeleman
Copy link
Author

This is an example with generated data. This script plots two figures. One with a range of 100:-100 and one 40:-40. They should both be 100:-100.

import numpy as np
import matplotlib.pyplot as plt

levels = np.arange(-100, 100, 10)

# Data to plot.
x, y = np.meshgrid(np.arange(10), np.arange(10))
z = 50*np.sin(0.5 * x) * np.cos(0.52 * y)

plt.figure(1)
CS = plt.contourf(x, y, z, levels)
CB = plt.colorbar(CS)

plt.figure(2)
CS = plt.contour(x, y, z, levels)
CB = plt.colorbar(CS)

plt.show()

@timhoffm
Copy link
Member

Note: If you want to have levels in the range -100:100, you have to to use either levels = np.arange(-100, 110, 10) or levels = np.linspace(-100, 100, 21).

I can reproduce the issue. QuadContourSet handles levels for the filled and the line contour case differently, as can seen from CS.levels. It's the full range for filled (contourf), but only the range for the plotted lines for contour.

Not sure which behavior is intended, but they should be the same.

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.

4 participants
0