Tom Johnson
Tom Johnson

Reputation: 2164

Weird matplotlib contour plot axis labels shows two sets of values

I'm using matplotlib 3.3.4 and generating a basic contour plot. But when I do so, the X and Y axis labels are showing my desired range (e.g. 0 to pi) but there's also an extra set of labels showing up that appear to be some sort of normalized values (0 to 1). The following code reproduces this:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, np.pi, 40)
y = np.linspace(0, np.pi, 40)
z = np.sin(x[:, None])**2 + np.sin(y)**2

fig, ax = plt.subplots(figsize=(10,10))
ax = fig.add_subplot(111)
ax.contour(x, y, z)

and produces a plot like the one below. I see axis labels at the expected values [0, 0.5, 1.0, 1.5, 2.0, 2.5, and 3.0]. But there's another set [0, 0.2, 0.4, 0.6, 0.8, 1.0] that's coming from somewhere.

After reviewing the contour() example at Contour Example I realize I should probably be calling np.meshgrid() rather than doing the extra axis stuff to produce z above.

Any clues as to what is causing this odd axis label behavior? enter image description here

Upvotes: 1

Views: 595

Answers (1)

sacuL
sacuL

Reputation: 51425

You're adding two subplots, one via plt.subplots, and one via add_subplot. Remove one of them and the figure will only have one set of ticks:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, np.pi, 40)
y = np.linspace(0, np.pi, 40)
z = np.sin(x[:, None])**2 + np.sin(y)**2

fig, ax = plt.subplots(figsize=(10,10))
ax.contour(x, y, z)

Upvotes: 1

Related Questions