Modifying matplotlib patchcollecton3d data - python

How do I modify the xyz data of a 3d scatter plot in matplotlib for fast on-line animations? In other words where do matplotlib patchcollection3d objects save the xyz coordinates, and how do I set them? For example:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np
## generate some random data
pts = np.random.uniform(0,10,(10,20,30))
plt.close('all')
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
patch_collection_instance = ax.scatter(pts[:,0],pts[:,1],pts[:,2], c='m', marker='o')
What do I do next with patch_collection_instance if, for example, I want to translate all points by a random amount?

The coordinates are stored in the attribute _offsets3d. While there is a get_offsets() method and a set_offsets() method, those appear to be inherited from the 2d version and don't work properly for 3d. _offsets3d contains a tuple of x, y, and z coordinate tuples. Let's say you want to shift every point by 10 in the x direction. You'd add 10 to every number in the x-coordinate tuple, then set the _offsets3d to the new tuple.
I am not sure if this is faster than just clearing the figure and calling scatter again with new coordinates, which should have the same effect.
Example code:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np
from copy import copy
## generate some random data
pts = np.random.uniform(0,10,(10,20,30))
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
patch_collection_instance = ax.scatter(pts[:,0],pts[:,1], pts[:,2], c='m', marker='o')
x, y, z = patch_collection_instance._offsets3d
print x
x = [i + 10 for i in x]
offsets = (x, y, z)
patches2 = copy(patch_collection_instance)
patches2._offsets3d = offsets
patches2._facecolor3d = [[0, 0, 1, 1]]
ax.add_collection3d(patches2)
plt.xlim(0, 20)
plt.show()

Related

How to avoid plotting for a zero without deleting the zero entry in the line plot Matplotlib?

I have tried a sample code where I used my figure to plot:
import matplotlib.pyplot as plt
import numpy as np
l = [1.10867,1.10894,1.10914,1.10926,1.10930,0.00000,0.00000,0.00000,0.00000,0.00000,1.10867,1.10894,1.10914,1.10926,1.10930]
x = np.arange(len(l))
y = np.array(l)
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
line1, = ax.plot(x, y, 'r-') # Returns a tuple of line objects, thus the comma
fig.canvas.draw()
fig.canvas.flush_events()
The figure comes out to be the following:
But I am expecting to have a figure this:
The expected figure is obtained when I removed the zeroes from the list. But I want to if there is a way to plot without removing zeroes from the list and still the figure looks like the expected one.
Please share any thoughts.
You can interpolate the points where y's are equal to 0 and plot:
import matplotlib.pyplot as plt
import numpy as np
l = [1.10867,1.10894,1.10914,1.10926,1.10930,0.00000,0.00000,0.00000,0.00000,0.00000,1.10867,1.10894,1.10914,1.10926,1.10930]
x = np.arange(len(l))
y = np.array(l)
y_interp = np.interp(x, x[np.where(y != 0)], y[np.where(y != 0)])
plt.plot(x, y_interp)

3D scatter plot of multiple files with each file having unique color

I have seen this thread but my data are a little different. I want to create a 3D plot of multiple files containing x,y,z coordinates and color code each file with a unique color, not each point coordinate
Code thus far:
import meshio
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import glob
import matplotlib.cm as cm
files = sorted(glob.glob('mesh_files/*.vtk'))
mesh = []
fig = plt.figure(figsize = (16, 10))
ax = plt.axes(projection = '3d')
colors = cm.rainbow(np.linspace(0, 1, 16))
for file in files:
mesh.append(meshio.read(file))
x = [m.points[:, 0] for m in mesh]
y = [m.points[:, 1] for m in mesh]
z = [m.points[:, 2] for m in mesh]
for a,b,c,d in zip(x,y,z,colors):
plt.scatter(a,b,c,color=d)
Background
x, y and z are all lists containing numpy arrays
<<len(x)
16
<<len(x[0])
99937
<<x[0].shape
(99937,)
<<type(x)
<class 'list'>
<<type(x[0])
<class 'numpy.ndarray'>
I believe the issue is with the colors and a possible mismatch in sizes
<<len(colors)
16
<<len(colors[0])
4
Error
RuntimeWarning: invalid value encountered in sqrt
EDIT: I can individually call scatter and manually enter a different color to create the below plot, but this would take forever with 10+ files, so I want it in a loop or function of some sort.
EDIT2: I was able to get this plot, which is nice that the colors are different for each files' data, but the z scale is too small, compared to the first plot, and it looks like data are missing, it should like like the first plot in terms of z depth values, but with 16 unique colors as in the second plot. The first plot is only plotting 3 files manually
If you don't need the meshes afterwards you can avoid allocating a bunch of memory
...
colors = iter(cm.rainbow(np.linspace(0, 1, 16)))
for file in files:
plt.scatter(*meshio.read(file).points.T, c=[next(colors)], label=file)
plt.legend()
plt.show()
or, if you need the meshes afterwards we can use a container
...
meshes = []
colors = iter(cm.rainbow(np.linspace(0, 1, 16)))
for file in files:
meshes.append(meshio.read(file))
plt.scatter(*meshes[-1].points.T, c=[next(colors)], label=file)
plt.legend()
plt.show()
NB scatter in 3D needs x, y and z, all with shape (N,), while meshobj.points has shape (N, 3) so we first transpose it (shape is now (3, N)) and finally we unpack (using the star "*" operator) the 2D array to get the requested three (N,) arrays.
I think you mistake comes from the mesh list that you are updating at every step. You plot the whole mesh list every step, such that your first file is plotted 16 times, in 16 different colors.
The simplest code could be:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import glob
import matplotlib.cm as cm
files = sorted(glob.glob('mesh_files/*.vtk'))
fig = plt.figure(figsize = (16, 10))
ax = plt.axes(projection = '3d')
colors = cm.rainbow(np.linspace(0, 1, len(files)))
for file in files:
data = meshio.read(file).points
x = data[:, 0]
y = data[:, 1]
z = data[:, 2]
plt.scatter(x, y, z, color = colors[files.index(file)])
If you want to store all the points in a list called mesh, you can modify it as :
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import glob
import matplotlib.cm as cm
files = sorted(glob.glob('mesh_files/*.vtk'))
mesh = []
fig = plt.figure(figsize = (16, 10))
ax = plt.axes(projection = '3d')
colors = cm.rainbow(np.linspace(0, 1, len(files)))
for file in files:
mesh.append(meshio.read(file).points)
x = mesh[-1][:, 0]
y = mesh[-1][:, 1]
z = mesh[-1][:, 2]
plt.scatter(x, y, z, color = colors[files.index(file)])
such that you only plot the points corresponding the file you just read at every step.
As was mentioned previously, the problem you're experiencing is which loop the color selection is occurring in.
color = iter(cm.rainbow(np.linspace(0, 1, len(files))))
for file in files:
d = next(color) #set the color for each file instead of inside the loop
mesh.append(meshio.read(file))
x = [m.points[:, 0] for m in mesh]
y = [m.points[:, 1] for m in mesh]
z = [m.points[:, 2] for m in mesh]
for a,b,c in zip(x,y,z):
plt.scatter(a,b,c,color=d)
This code below is currently working for me, for the most part.
I changed plt.scatter... to ax.scatter... and it fixed the z-axis scaling issue that I mentioned in EDIT 2 above.
I also changed to ax = Axes3D(fig)
Thanks to everyone's help! I will work with this for now.
import meshio
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import glob
import matplotlib.cm as cm
files = sorted(glob.glob('path/to/vtk/files/*_mesh.vtk'))
meshes = []
fig = plt.figure(figsize = (16, 10))
ax = Axes3D(fig)
colors = iter(cm.rainbow(np.linspace(0, 1, len(files))))
for fyle in files:
ax.scatter(*meshio.read(fyle).points.T, c=[next(colors)])
plt.legend() #legend isn't plotting, will have to fix this
plt.show()

Adding a colorbar to pyplot [duplicate]

I have a sequence of line plots for two variables (x,y) for a number of different values of a variable z. I would normally add the line plots with legends like this:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
# suppose mydata is a list of tuples containing (xs, ys, z)
# where xs and ys are lists of x's and y's and z is a number.
legns = []
for(xs,ys,z) in mydata:
pl = ax.plot(xs,ys,color = (z,0,0))
legns.append("z = %f"%(z))
ax.legends(legns)
plt.show()
But I have too many graphs and the legends will cover the graph. I'd rather have a colorbar indicating the value of z corresponding to the color. I can't find anything like that in the galery and all my attempts do deal with the colorbar failed. Apparently I must create a collection of plots before trying to add a colorbar.
Is there an easy way to do this? Thanks.
EDIT (clarification):
I wanted to do something like this:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
fig = plt.figure()
ax = fig.add_subplot(111)
mycmap = cm.hot
# suppose mydata is a list of tuples containing (xs, ys, z)
# where xs and ys are lists of x's and y's and z is a number between 0 and 1
plots = []
for(xs,ys,z) in mydata:
pl = ax.plot(xs,ys,color = mycmap(z))
plots.append(pl)
fig.colorbar(plots)
plt.show()
But this won't work according to the Matplotlib reference because a list of plots is not a "mappable", whatever this means.
I've created an alternative plot function using LineCollection:
def myplot(ax,xs,ys,zs, cmap):
plot = lc([zip(x,y) for (x,y) in zip(xs,ys)], cmap = cmap)
plot.set_array(array(zs))
x0,x1 = amin(xs),amax(xs)
y0,y1 = amin(ys),amax(ys)
ax.add_collection(plot)
ax.set_xlim(x0,x1)
ax.set_ylim(y0,y1)
return plot
xs and ys are lists of lists of x and y coordinates and zs is a list of the different conditions to colorize each line. It feels a bit like a cludge though... I thought that there would be a more neat way to do this. I like the flexibility of the plt.plot() function.
(I know this is an old question but...) Colorbars require a matplotlib.cm.ScalarMappable, plt.plot produces lines which are not scalar mappable, therefore, in order to make a colorbar, we are going to need to make a scalar mappable.
Ok. So the constructor of a ScalarMappable takes a cmap and a norm instance. (norms scale data to the range 0-1, cmaps you have already worked with and take a number between 0-1 and returns a color). So in your case:
import matplotlib.pyplot as plt
sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=plt.normalize(min=0, max=1))
plt.colorbar(sm)
Because your data is in the range 0-1 already, you can simplify the sm creation to:
sm = plt.cm.ScalarMappable(cmap=my_cmap)
EDIT: For matplotlib v1.2 or greater the code becomes:
import matplotlib.pyplot as plt
sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=plt.normalize(vmin=0, vmax=1))
# fake up the array of the scalar mappable. Urgh...
sm._A = []
plt.colorbar(sm)
EDIT: For matplotlib v1.3 or greater the code becomes:
import matplotlib.pyplot as plt
sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=plt.Normalize(vmin=0, vmax=1))
# fake up the array of the scalar mappable. Urgh...
sm._A = []
plt.colorbar(sm)
EDIT: For matplotlib v3.1 or greater simplifies to:
import matplotlib.pyplot as plt
sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=plt.Normalize(vmin=0, vmax=1))
plt.colorbar(sm)
Here's one way to do it while still using plt.plot(). Basically, you make a throw-away plot and get the colorbar from there.
import matplotlib as mpl
import matplotlib.pyplot as plt
min, max = (-40, 30)
step = 10
# Setting up a colormap that's a simple transtion
mymap = mpl.colors.LinearSegmentedColormap.from_list('mycolors',['blue','red'])
# Using contourf to provide my colorbar info, then clearing the figure
Z = [[0,0],[0,0]]
levels = range(min,max+step,step)
CS3 = plt.contourf(Z, levels, cmap=mymap)
plt.clf()
# Plotting what I actually want
X=[[1,2],[1,2],[1,2],[1,2]]
Y=[[1,2],[1,3],[1,4],[1,5]]
Z=[-40,-20,0,30]
for x,y,z in zip(X,Y,Z):
# setting rgb color based on z normalized to my range
r = (float(z)-min)/(max-min)
g = 0
b = 1-r
plt.plot(x,y,color=(r,g,b))
plt.colorbar(CS3) # using the colorbar info I got from contourf
plt.show()
It's a little wasteful, but convenient. It's also not very wasteful if you make multiple plots as you can call plt.colorbar() without regenerating the info for it.
Here is a slightly simplied example inspired by the top answer given by Boris and Hooked (Thanks for the great idea!):
1. Discrete colorbar
Discrete colorbar is more involved, because colormap generated by mpl.cm.get_cmap() is not a mappable image needed as a colorbar() argument. A dummie mappable needs to generated as shown below:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
n_lines = 5
x = np.linspace(0, 10, 100)
y = np.sin(x[:, None] + np.pi * np.linspace(0, 1, n_lines))
c = np.arange(1, n_lines + 1)
cmap = mpl.cm.get_cmap('jet', n_lines)
fig, ax = plt.subplots(dpi=100)
# Make dummie mappable
dummie_cax = ax.scatter(c, c, c=c, cmap=cmap)
# Clear axis
ax.cla()
for i, yi in enumerate(y.T):
ax.plot(x, yi, c=cmap(i))
fig.colorbar(dummie_cax, ticks=c)
plt.show();
This will produce a plot with a discrete colorbar:
2. Continuous colorbar
Continuous colorbar is less involved, as mpl.cm.ScalarMappable() allows us to obtain an "image" for colorbar().
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
n_lines = 5
x = np.linspace(0, 10, 100)
y = np.sin(x[:, None] + np.pi * np.linspace(0, 1, n_lines))
c = np.arange(1, n_lines + 1)
norm = mpl.colors.Normalize(vmin=c.min(), vmax=c.max())
cmap = mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.jet)
cmap.set_array([])
fig, ax = plt.subplots(dpi=100)
for i, yi in enumerate(y.T):
ax.plot(x, yi, c=cmap.to_rgba(i + 1))
fig.colorbar(cmap, ticks=c)
plt.show();
This will produce a plot with a continuous colorbar:
[Side note] In this example, I personally don't know why cmap.set_array([]) is necessary (otherwise we'd get error messages). If someone understand the principles under the hood, please comment :)
As other answers here do try to use dummy plots, which is not really good style, here is a generic code for a
Discrete colorbar
A discrete colorbar is produced in the same way a continuous colorbar is created, just with a different Normalization. In this case a BoundaryNorm should be used.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
n_lines = 5
x = np.linspace(0, 10, 100)
y = np.sin(x[:, None] + np.pi * np.linspace(0, 1, n_lines))
c = np.arange(1., n_lines + 1)
cmap = plt.get_cmap("jet", len(c))
norm = matplotlib.colors.BoundaryNorm(np.arange(len(c)+1)+0.5,len(c))
sm = plt.cm.ScalarMappable(norm=norm, cmap=cmap)
sm.set_array([]) # this line may be ommitted for matplotlib >= 3.1
fig, ax = plt.subplots(dpi=100)
for i, yi in enumerate(y.T):
ax.plot(x, yi, c=cmap(i))
fig.colorbar(sm, ticks=c)
plt.show()

Python 4D plot using matplotlib- confusion in reading and plotting the array

I have a list like below -
array1 = [[1,2,3,0.56],[12,5,30,0.23],[10,12,17,89.65]]
This represents co-ordinates- [[x1,y1,z1,c1],[x2,y2,z2,c2],[x3,y3,z3,c3]].
I used 4D plot with 4th dimension[c1,c2,c3] being the color. I am stuck at plotting. I would like to have a color for c1 at co-ordinates [x1,y1,z1] and similarly to other co-ordinates.
I used the below methods -
import matplotlib.pyplot as plt
import numpy as np
1) for p in range(len(array1)-1):
x = np.append([array1[p][0]], array1[p+1][0])
y = np.append([array1[p][1]], array1[p+1][1])
z = np.append([array1[p][2]], array1[p+1][2])
c = np.append([array1[p][3]], array1[p+1][3])
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(x,y,z,c=c,cmap = cmap)
plt.show()
The problem in method 1 is, its not plotting all the elements of the list. I guess there is a mistake in executing append. I am not getting any errors but its just not plotting every data.
2) fig = plt.figure()
ax = fig.gca(projection='3d')
for p in range(len(array1)-1):
ax.scatter(array1[p][0],array1[p][1],array1[p][2],array1[p][3],cmap =
cmap)
plt.show()
So in method 2, I tried to plot iteratively but its giving me 'float object unsubscriptable' error.
So can somebody tell me where I am going wrong. Or is there any other method to do this ?
This is one way of doing it by converting your list to array which allows you to slice all the elements directly as x, y, z and color coordinates.
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
new_array = = np.array(array1)
ax.scatter(new_array[:,0],new_array[:,1],new_array[:,2], c=new_array[:,3], cmap=cm.RdBu, s=100)

Matplotlib - add colorbar to a sequence of line plots

I have a sequence of line plots for two variables (x,y) for a number of different values of a variable z. I would normally add the line plots with legends like this:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
# suppose mydata is a list of tuples containing (xs, ys, z)
# where xs and ys are lists of x's and y's and z is a number.
legns = []
for(xs,ys,z) in mydata:
pl = ax.plot(xs,ys,color = (z,0,0))
legns.append("z = %f"%(z))
ax.legends(legns)
plt.show()
But I have too many graphs and the legends will cover the graph. I'd rather have a colorbar indicating the value of z corresponding to the color. I can't find anything like that in the galery and all my attempts do deal with the colorbar failed. Apparently I must create a collection of plots before trying to add a colorbar.
Is there an easy way to do this? Thanks.
EDIT (clarification):
I wanted to do something like this:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
fig = plt.figure()
ax = fig.add_subplot(111)
mycmap = cm.hot
# suppose mydata is a list of tuples containing (xs, ys, z)
# where xs and ys are lists of x's and y's and z is a number between 0 and 1
plots = []
for(xs,ys,z) in mydata:
pl = ax.plot(xs,ys,color = mycmap(z))
plots.append(pl)
fig.colorbar(plots)
plt.show()
But this won't work according to the Matplotlib reference because a list of plots is not a "mappable", whatever this means.
I've created an alternative plot function using LineCollection:
def myplot(ax,xs,ys,zs, cmap):
plot = lc([zip(x,y) for (x,y) in zip(xs,ys)], cmap = cmap)
plot.set_array(array(zs))
x0,x1 = amin(xs),amax(xs)
y0,y1 = amin(ys),amax(ys)
ax.add_collection(plot)
ax.set_xlim(x0,x1)
ax.set_ylim(y0,y1)
return plot
xs and ys are lists of lists of x and y coordinates and zs is a list of the different conditions to colorize each line. It feels a bit like a cludge though... I thought that there would be a more neat way to do this. I like the flexibility of the plt.plot() function.
(I know this is an old question but...) Colorbars require a matplotlib.cm.ScalarMappable, plt.plot produces lines which are not scalar mappable, therefore, in order to make a colorbar, we are going to need to make a scalar mappable.
Ok. So the constructor of a ScalarMappable takes a cmap and a norm instance. (norms scale data to the range 0-1, cmaps you have already worked with and take a number between 0-1 and returns a color). So in your case:
import matplotlib.pyplot as plt
sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=plt.normalize(min=0, max=1))
plt.colorbar(sm)
Because your data is in the range 0-1 already, you can simplify the sm creation to:
sm = plt.cm.ScalarMappable(cmap=my_cmap)
EDIT: For matplotlib v1.2 or greater the code becomes:
import matplotlib.pyplot as plt
sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=plt.normalize(vmin=0, vmax=1))
# fake up the array of the scalar mappable. Urgh...
sm._A = []
plt.colorbar(sm)
EDIT: For matplotlib v1.3 or greater the code becomes:
import matplotlib.pyplot as plt
sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=plt.Normalize(vmin=0, vmax=1))
# fake up the array of the scalar mappable. Urgh...
sm._A = []
plt.colorbar(sm)
EDIT: For matplotlib v3.1 or greater simplifies to:
import matplotlib.pyplot as plt
sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=plt.Normalize(vmin=0, vmax=1))
plt.colorbar(sm)
Here's one way to do it while still using plt.plot(). Basically, you make a throw-away plot and get the colorbar from there.
import matplotlib as mpl
import matplotlib.pyplot as plt
min, max = (-40, 30)
step = 10
# Setting up a colormap that's a simple transtion
mymap = mpl.colors.LinearSegmentedColormap.from_list('mycolors',['blue','red'])
# Using contourf to provide my colorbar info, then clearing the figure
Z = [[0,0],[0,0]]
levels = range(min,max+step,step)
CS3 = plt.contourf(Z, levels, cmap=mymap)
plt.clf()
# Plotting what I actually want
X=[[1,2],[1,2],[1,2],[1,2]]
Y=[[1,2],[1,3],[1,4],[1,5]]
Z=[-40,-20,0,30]
for x,y,z in zip(X,Y,Z):
# setting rgb color based on z normalized to my range
r = (float(z)-min)/(max-min)
g = 0
b = 1-r
plt.plot(x,y,color=(r,g,b))
plt.colorbar(CS3) # using the colorbar info I got from contourf
plt.show()
It's a little wasteful, but convenient. It's also not very wasteful if you make multiple plots as you can call plt.colorbar() without regenerating the info for it.
Here is a slightly simplied example inspired by the top answer given by Boris and Hooked (Thanks for the great idea!):
1. Discrete colorbar
Discrete colorbar is more involved, because colormap generated by mpl.cm.get_cmap() is not a mappable image needed as a colorbar() argument. A dummie mappable needs to generated as shown below:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
n_lines = 5
x = np.linspace(0, 10, 100)
y = np.sin(x[:, None] + np.pi * np.linspace(0, 1, n_lines))
c = np.arange(1, n_lines + 1)
cmap = mpl.cm.get_cmap('jet', n_lines)
fig, ax = plt.subplots(dpi=100)
# Make dummie mappable
dummie_cax = ax.scatter(c, c, c=c, cmap=cmap)
# Clear axis
ax.cla()
for i, yi in enumerate(y.T):
ax.plot(x, yi, c=cmap(i))
fig.colorbar(dummie_cax, ticks=c)
plt.show();
This will produce a plot with a discrete colorbar:
2. Continuous colorbar
Continuous colorbar is less involved, as mpl.cm.ScalarMappable() allows us to obtain an "image" for colorbar().
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
n_lines = 5
x = np.linspace(0, 10, 100)
y = np.sin(x[:, None] + np.pi * np.linspace(0, 1, n_lines))
c = np.arange(1, n_lines + 1)
norm = mpl.colors.Normalize(vmin=c.min(), vmax=c.max())
cmap = mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.jet)
cmap.set_array([])
fig, ax = plt.subplots(dpi=100)
for i, yi in enumerate(y.T):
ax.plot(x, yi, c=cmap.to_rgba(i + 1))
fig.colorbar(cmap, ticks=c)
plt.show();
This will produce a plot with a continuous colorbar:
[Side note] In this example, I personally don't know why cmap.set_array([]) is necessary (otherwise we'd get error messages). If someone understand the principles under the hood, please comment :)
As other answers here do try to use dummy plots, which is not really good style, here is a generic code for a
Discrete colorbar
A discrete colorbar is produced in the same way a continuous colorbar is created, just with a different Normalization. In this case a BoundaryNorm should be used.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
n_lines = 5
x = np.linspace(0, 10, 100)
y = np.sin(x[:, None] + np.pi * np.linspace(0, 1, n_lines))
c = np.arange(1., n_lines + 1)
cmap = plt.get_cmap("jet", len(c))
norm = matplotlib.colors.BoundaryNorm(np.arange(len(c)+1)+0.5,len(c))
sm = plt.cm.ScalarMappable(norm=norm, cmap=cmap)
sm.set_array([]) # this line may be ommitted for matplotlib >= 3.1
fig, ax = plt.subplots(dpi=100)
for i, yi in enumerate(y.T):
ax.plot(x, yi, c=cmap(i))
fig.colorbar(sm, ticks=c)
plt.show()

Categories