How To Generate The Lineared Color Plot (cplot) With Z Values In Colorbar
Solution 1:
Matplotlib has not a cplot
direct equivalent but you can use a LineCollection
.
With this understanding you have to modify the usual boilerplate adding a specific import
In [1]: import numpy as np
...: import matplotlib.pyplotas plt
...: from matplotlib.collectionsimportLineCollection
Now, generate some data (c
is the 3rd value associated with the (x, y)
point)
In [2]: x = np.linspace(0, 6.3, 64)
...: y = np.sin(x) ; c = np.cos(x)
LineCollection
needs a 3D array, i.e. a list of segments, each segment a list of points, each point a list of coordinates, that we build using this recipe
In [3]: points = np.array([x, y]).T.reshape(-1,1,2)
...: segments = np.concatenate([points[:-1], points[1:]], axis=1)
Now we instantiate the LineCollection
, specifying the colormap that we want and the line width, and immediately after we tell to our instance that its array
(what is mapped to colors) is the array c
In [4]: lc = LineCollection(segments, cmap='plasma', linewidth=3)
...: lc.set_array(c)
and eventually we plot lc
in its own way, call autoscale
because it's needed (try not to call it...) and add a colorbar.
In [5]: fig, ax = plt.subplots()
...: ax.add_collection(lc)
...: ax.autoscale()
...: plt.colorbar(lc);
I know, it's a bit clunky but it works.
Solution 2:
IDL v8 has an easy to use keyword for the PLOT function called VERT_COLORS:
; generate some sample datax = cos(dindgen(100)/20)
y = sin(dindgen(100)/20)
z = dindgen(100)+100; plot the datap = plot(x, y, vert_colors=bytscl(z), rgb_table=39, xrange=[-2,2], yrange=[-2,2], thick=3, /aspect_ratio)
cb = colorbar(range=[min(z), max(z)], target=p)
The z data is scaled to a byte index of the colortable number 39. The colorbar needs to know the data range explicitly.
Post a Comment for "How To Generate The Lineared Color Plot (cplot) With Z Values In Colorbar"