0

I have data (link) in the form given below:

Input data format

Header: 'x' 'y'  'a'  'b'
x1  , y1  , ... (data)
x2  , y2  , ... (data)
x3  , y3  , ... (data)
 :     :           :
xn-2, yn-2, ... (data)
xn-1, yn-1, ... (data)
xn  , yn  , ... (data)

They are non uniformly spaced grids, and I want to plot a filled contour that is colored by a, b in this case. Because of the arrangement of the points and non uniformity, I cannot use np.meshgrid (correct me if I am wrong). How do I plot a column vector as a contour with non uniform grid points that are also column vectors?

MWE

import numpy as np
import matplotlib.pyplot as plt

data = np.genfromtxt('./plot_data.dat', skip_header=1, dtype = None, delimiter = '\t')
test = np.column_stack([data[:,0],data[:,1],data[:,3]])
plt.imshow(test)
plt.xlim([np.min(data[:,0]), np.max(data[:,0])])
plt.ylim([np.min(data[:,1]), np.max(data[:,1])])        
plt.show()
Tom Kurushingal
  • 6,086
  • 20
  • 54
  • 86

1 Answers1

1

numpy.meshgrid should have no problem with non uniform 1-d domains. However, your 2-D data are irregularly distributed in the data file (see plots at the end of post). This has other issues. However, as suggested by @Thomas-Kühn, matplotlib.pyplot.tricontour and matplotlib.pyplot.tricontourf can handle your data (below I use tricontourf):

import numpy as np
import matplotlib.pyplot as plt

x,y,a,b = np.loadtxt('./plot_data.dat', skiprows=1, delimiter = '\t').T
plt.tricontourf(x,y,a)

Result of the a and b data are on the left and right (note the black separates the two figures, and white indicates a lack of data):

a on left, b on right

Due to the sparsity of your data, a scatter plot with matplotlib.pyplot.scatter(x,y,c=a) may be useful too (of course with adequate x and y axes labels):

scatter plot, a on left, b on right

Or combining the filled contour plot with some representation of where the points are (combining the previous tricontourf with a simple plot of the domain:

a on left and b on right

Finally, you may like, matplotlib.pyplot.hexbin:

enter image description here

jyalim
  • 3,289
  • 1
  • 15
  • 22