In MATLAB™ one can use cplot.m
which can generate colored plot basically looks like 2d plot with 3rd axis (z-axis) value as colorbar. Is there any tool/plotting technique I can use to generate a similar plot in Python or IDL Programming Language?. The previous question on stack overflow dealing with different problem as given in a link.
-
What do you mean with "lineared" ?? – gboffi Nov 17 '19 at 21:53
-
The duplicate does not address the IDL part of the question. See http://www.idlcoyote.com/graphics_tips/coloredline.html for an example solution (use `plots` or `cgplots` if you have the Coyote library). – sappjw Nov 18 '19 at 12:58
2 Answers
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.pyplot as plt
...: from matplotlib.collections import LineCollection
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.

- 22,939
- 8
- 54
- 85
IDL v8 has an easy to use keyword for the PLOT function called VERT_COLORS:
; generate some sample data
x = cos(dindgen(100)/20)
y = sin(dindgen(100)/20)
z = dindgen(100)+100
; plot the data
p = 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.

- 346
- 5
- 12
-
1The colorbar should show me the values on the z-axis. but it showing else h=colorbar(p). any idea on bringing z-axis value on the colorbar. – Pavan Nov 20 '19 at 04:29
-