-1

I have a problem that needs your help. Now I have a grid data. It is composed of multiple different values. The structure is shown below. actual graphics

In fact, the graph I want to get should be composed of smooth filled surface. As shown below. The graphics i want

I provided a specific data set. Please refer. Sample data set。 this dataset is in asc format defined by "surfer" software. You can use "pykrige" to read. Use the following code to render

import sys
import matplotlib.pyplot as plt
import pykrige.kriging_tools as kt

def func(cmd):    
   ascFile = cmd[1]

   ascFileContent = kt.read_asc_grid(ascFile)
   Z = ascFileContent[0]
   X = ascFileContent[1]
   Y = ascFileContent[2]
   print(Z)

   fig = plt.figure()    
   ax = fig.add_subplot()

   contour = plt.contourf(X,Y,Z)
   #contour = plt.pcolormesh(X,Y,Z)
   plt.show()


if __name__ == "__main__":

   cmd=["CONTOUR_KYZQ"]
   #gridFile
   cmd.append(r"D:\out.grd")
   func(cmd)

I used various methods, such as pcolormesh, contourf. The resulting graphics are jagged. How can I solve this problem. please help me. Thanks a lot.

Henry
  • 3
  • 2
  • Welcome to Stack Overflow! Please take a moment to read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). You need to provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) that includes a toy dataset (refer to [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)) – Diziet Asahi Jun 28 '20 at 09:29
  • Thank you for your attention to my question – Henry Jun 28 '20 at 09:40
  • Thanks for adding the data. Could you please explain what format it's in? Also, a smaller sample that could be included as part of the question itself would be great. – Roy2012 Jun 28 '20 at 09:56
  • Thanks for attention. Now I added a piece of code to render the dataset. You can find the jaggedness. I try to use the interpolation algorithm to solve the aliasing problem. But no success – Henry Jun 28 '20 at 10:35
  • I can't read the file. Getting an error: ValueError: not enough values to unpack (expected 2, got 1) – Roy2012 Jun 28 '20 at 11:16
  • I changed the sample data set (the network address is the same), please download the data set again. – Henry Jun 28 '20 at 11:38

1 Answers1

0

use scipy.ndimage.zoom to smooth the image. Here's I'm using smoothing with the number 5, but you can try other numbers are well.

import matplotlib.pyplot as plt
import pykrige.kriging_tools as kt
import scipy.ndimage

ascFileContent = kt.read_asc_grid("out.grd")
Z = ascFileContent[0]
X = ascFileContent[1]
Y = ascFileContent[2]

fig = plt.figure()    
plt.rcParams['figure.figsize'] = [20, 14]


new_z = scipy.ndimage.zoom(Z, 5)

contour = plt.contourf(np.linspace(X[0], X[-1], len(X) * 5), 
                       np.linspace(Y[0], Y[-1], len(Y) * 5), 
                       new_z)


plt.show()

If you zoom into a specific area, as in this code snippet, you'll notice that your data is inherently 'checkered'. So even when smoothed out, there are areas that look very grid-like (see below).

ascFileContent = kt.read_asc_grid("out.grd")
Z = ascFileContent[0]
X = ascFileContent[1]
Y = ascFileContent[2]

fig = plt.figure()    
plt.rcParams['figure.figsize'] = [20, 14]
ZOOM_FACTOR = 5

new_z = scipy.ndimage.zoom(Z, ZOOM_FACTOR)

contour = plt.contourf(np.linspace(X[0], X[-1], len(X) * ZOOM_FACTOR), 
                       np.linspace(Y[0], Y[-1], len(Y) * ZOOM_FACTOR), 
                       new_z)

plt.xlim((106, 107))
plt.ylim((24, 26))

plt.show()

enter image description here

Roy2012
  • 11,755
  • 2
  • 22
  • 35
  • very Cool, thanks for your help. This graphic looks much better. if the graphics can achieve the effect of this example, it will be perfect. That is, the area covered by the grid becomes a smooth curve. https://matplotlib.org/gallery/lines_bars_and_markers/step_demo.html#sphx-glr-gallery-lines-bars-and-markers-step-demo-py – Henry Jun 28 '20 at 12:30
  • I believe that's basically what you're getting, but with your specific data. See my last edit - due to the characteristics of your data, it still looks a little grid-like. – Roy2012 Jun 28 '20 at 12:44
  • (BTW, if it answers you question it would be great if you could accept my answer for future generations). – Roy2012 Jun 28 '20 at 12:46
  • Yes, you found the key to the problem. This data set was manually revised. In fact, people use a Bezier curve to select an area to correct the grid value. So now my problem is to restore the area of ​​these curves by grid point value. – Henry Jun 28 '20 at 13:08
  • Glad I could assist. Please feel free to accept my answer if you think it's helpful . – Roy2012 Jun 28 '20 at 13:27
  • Thank you for your attention, you helped me a lot – Henry Jun 29 '20 at 03:06