Issue
Let me begin this query by admitting that I am very new to Python. I want to create contour plot of the data in Python so as to automate the process, which otherwise can be easily carried out using Surfer. I have 1000s of such data files, and creating manually could be very tedious. The data I'm using looks like follows, which is a dataframe with 0, 1 and 2 headers and 1,2,..279 as index:
0 1 2
0 3 -1 -0.010700
1 4 -1 0.040100
2 5 -1 0.061000
3 6 -1 0.052000
4 7 -1 0.013100
.. .. .. ...
275 30 -9 -1.530100
276 31 -9 -1.362300
277 32 -9 -1.190200
278 33 -9 -1.083600
279 30 -10 -1.864600
[280 rows x 3 columns]
Here,
x=data[0]
y=data[1]
z=data[2]
As contour function pf matplotlib requires z to be a 2D array; this is where the confusion begins. Following several solutions of stackoverflow queries, I did the following:
import numpy as np
x=np.array(x)
y=np.array(y)
z=np.array(z)
X, Y = np.meshgrid(x, y)
import scipy.interpolate
rbf = scipy.interpolate.Rbf(x, y, z, function='cubic')
Z=rbf(X,Y)
lmin=data[2].min()
lmax=data[2].max()
progn=(lmax-lmin)/20
limit=np.arange(lmin,lmax,progn)
fig, ax = plt.subplots(figsize=(6,2)) #x ranges between 3 to 57, y -1 to -10
ax.contour(X,Y,Z,limit)
ax.set_title('Contour Plot')
plt.show()
With the above code this plot is derived.
However, it is not desired and if once can see through the surfacial noise lines then there are ordered contour lines underneath, which actually is desired as seen from the contour plot generated by surfer here.
I'd like to reiterate that the same data was used in generating the surfer plot.
Any help in creating the desired plot shall be highly appreciated.
Solution
Thanks to @JohanC for the answer. I'd like to put his suggestion to perspective with my query.
ax.contour
replaced by ax.tricontour
solves my situation. And ax.tricontourf
gets the contour fill done. Therefore, the last segment of my code would be:
fig, ax = plt.subplots(figsize=(6,2)) #x ranges between 3 to 57, y -1 to -10
ax.tricontour(X,Y,Z,limit)
ax.tricontourf(X,Y,Z,limit)
ax.set_title('Contour Plot')
plt.show()
Answered By - Chandan Dey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.