bob.sacamento
bob.sacamento

Reputation: 6651

Can python/matplotlib's confourf be made to plot a limited region of data?

Am trying to make a contour plot with matplotlib's contourf. Is there a way to zoom in on a particular region of my data and leave the rest unplotted? For instance, maybe my horizontal extent goes from -1 to 101, but I just want to plot the data that's in between 0 and 100 inclusive, and I want the boundary of the plot to be drawn at 0 on the left and 100 on the right. I thought the "extent" keyword would do the job, but it is inactive when X and Y data are given. I know that I can mask the extraneous data in various ways, but that leaves the boundaries of the plot drawn beyond my region of interest, which is not what I want. I guess I could also filter or interpolate my data to my region of interest and then give that filtered data to contourf, but if I can just make contourf focus on a particular region, it would be alot easier. Thanks.

Upvotes: 0

Views: 545

Answers (1)

unutbu
unutbu

Reputation: 880577

Perhaps you are looking for plt.xlim:

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1,101,100)
y = np.linspace(-1,101,100)
x, y = np.meshgrid(x, y)
z = x*x+y*y
plt.figure()
plt.xlim(0, 50)
plt.contourf(x, y, z)
plt.show()

enter image description here

Above, plt.xlim(0, 50) was used instead of plt.xlim(0,100) just to emphasize the change. Without plt.xlim(0, 50) the plot looks like this:

enter image description here

Upvotes: 1

Related Questions