Generate smoother colormap for contour plot

51 views Asked by At

My data set contains approximately thousands of values in four columns X, Y, Z, error of Z -

1.2351e+00  -6.3115e-02 0.0000e+00  0.0000e+00
1.2417e+00  -6.3115e-02 0.0000e+00  0.0000e+00
1.2483e+00  -6.3115e-02 0.0000e+00  0.0000e+00
1.2549e+00  -6.3115e-02 0.0000e+00  0.0000e+00
1.2615e+00  -6.3115e-02 0.0000e+00  0.0000e+00
1.2681e+00  -6.3115e-02 0.0000e+00  0.0000e+00
1.2746e+00  -6.3115e-02 0.0000e+00  0.0000e+00
1.2812e+00  -6.3115e-02 0.0000e+00  0.0000e+00
1.2878e+00  -6.3115e-02 1.8186e+01  1.8186e+01
1.2944e+00  -6.3115e-02 0.0000e+00  0.0000e+00
1.3010e+00  -6.3115e-02 0.0000e+00  0.0000e+00
1.3075e+00  -6.3115e-02 0.0000e+00  0.0000e+00
1.3141e+00  -6.3115e-02 0.0000e+00  0.0000e+00
1.3207e+00  -6.3115e-02 0.0000e+00  0.0000e+00
... ....

I want to plot these data using a contour plot, something like this- (reference image) reference image

I am using the following code -

import numpy as np  
import matplotlib.pyplot as plt
x, y, z, err = np.genfromtxt(r'dataset_gid.txt', unpack=True)


x1, y1 = np.meshgrid(x, y)
cont1 = plt.tricontourf(x, y, z, cmap='seismic')
plt.colorbar(cont1)
plt.show()

I am getting the following plot -

enter image description here

How can I generate a colorbar similar to the above image? I have created a gist for the dataset I am using here.

Update

Using scat=plt.scatter(x, y, c=z, s=3);plt.colorbar(scat) gives me weird lines -

enter image description here

Using plt.tricontourf(x, y, z, levels=50) looks better but the spots are not bright enough.

enter image description here

Is there a way to make the spots brighter like the reference image?

1

There are 1 answers

3
gboffi On

A few preliminaries

import matplotlib.pyplot as plt
import numpy as np
fig = lambda: plt.figure(layout='constrained')
d2 = lambda arr: arr.reshape(77, 843)
a_n_d = np.logical_and

read the data, find extremes

x, y, z, e = np.array([
    [float(x) for x in line.split()]
    for line in open('gist.txt').readlines()]).T

xlim = (x.min(), x.max())
ylim = (y.min(), y.max())

plot a scatter plot of all the data, the "weird lines", (OP comment) are just the data points, that are all placed on a regular grid, loose in x-direction and tight in y-direction.

fig()
scat = plt.scatter(x, y, c=z, s=40, cmap='Blues')
plt.colorbar(scat)

enter image description here

Now, let;s plot a contour plot, with 3 levels

fig()
cont = plt.contour(d2(x), d2(y), d2(z), levels=[10, 40, 160], cmap='Blues', lw=0.2)
plt.colorbar(cont)

enter image description here

In my opinion, the contour plot is less informative than the scatter plot.

Finally, let's try a set of selective scatter plots, where we draw only the points whose z values are comprised in a different range for each subplot

f, axs = plt.subplots(2,2, layout='constrained')
for ax, ix in zip(axs.flat,
                  (z<100, a_n_d(z>=100, z<250), a_n_d(z>=250, z<500), z>500)):
    ax.set_xlim(xlim) ; ax.set_ylim(ylim)
    scat = ax.scatter(x[ix], y[ix], s=10, c=z[ix], cmap='Blues')
    plt.colorbar(scat, ax=ax)

enter image description here

In my opinion, this arrangement is the most informative.

The color scales are different in the different subplots, you can have the same scaling using vmin=, vmax= but, again IMO, it's better with different scales.


Array Shape Clarification

First, I looked at the factors of 64911

$ factor 64911
64911: 3 7 11 281
$

next I had a look into the data, the lines 1–77 have the same y value and different x values, the lines 78–154 have another, common y value and the same 77 x values of lines 1–77.

My conclusion was the data is given over a (7·11)×(3·281) = 77×843 xy grid.

Re your data, a lot of error values are equal to z value reported in the previous column, is this what you expect from your measurements/simulations?