I need to visualize some data. It's basic 2D grid, where each cell have float value. I know how to e.g. assign color to value and paint grid in OpenCV. But the point here is that there are so many values so it's nearly impossible to do that. I am looking for some method, where I could use gradient. For example value -5.0 will be represented by blue, 0 - black, and +5.0 as red. Is there any way to do that in Python?
Here is sample data I am talking about
A B C D
A -1.045 2.0 3.5 -4.890
B -5.678 3.2 2.89 5.78
Matplotlib has the imshow method for plotting arrays:
import matplotlib as mpl
from matplotlib import pyplot
import numpy as np
# make values from -5 to 5, for this example
zvals = np.random.rand(100,100)*10-5
# make a color map of fixed colors
cmap = mpl.colors.ListedColormap(['blue','black','red'])
bounds=[-6,-2,2,6]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
# tell imshow about color map so that only set colors are used
img = pyplot.imshow(zvals,interpolation='nearest',
cmap = cmap,norm=norm)
# make a color bar
pyplot.colorbar(img,cmap=cmap,
norm=norm,boundaries=bounds,ticks=[-5,0,5])
pyplot.show()
This is what it looks like:
The details for the color bar setup were taken from a matplotlib example: colorbar_only.py. It explains that the number of boundaries need to be one larger then then number of colors.
EDIT
You should note, that imshow accepts the origin keyword, which sets the where the first point is assigned. The default is 'upper left', which is why in my posted plot the y axis has 0 in the upper left and 99 (not shown) in the lower left. The alternative is to set origin="lower", so that first point is plotted in the lower left corner.
EDIT 2
If you want a gradient and not a discrete color map, make a color map by linearly interpolating through a series of colors:
fig = pyplot.figure(2)
cmap2 = mpl.colors.LinearSegmentedColormap.from_list('my_colormap',
['blue','black','red'],
256)
img2 = pyplot.imshow(zvals,interpolation='nearest',
cmap = cmap2,
origin='lower')
pyplot.colorbar(img2,cmap=cmap2)
fig.savefig("image2.png")
This produces:
EDIT 3
To add a grid, as shown in this example, use the grid method. Setting the grid color to 'white' works well with the colors used by the colormap (ie the default black does not show up well).
pyplot.grid(True,color='white')
Including this before the savefig call produces this plot (made using 11x11 grid for clarity):
There are many options for grid, which are described in the matplotlib documentation. One you might be interested in is linewidth.
How about using matplotlib?
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FixedLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = Axes3D(fig)
Z = np.array([[-1.045, 2.0, 3.5, -4.890],
[-5.678, 3.2, 2.89, 5.78]])
X = np.zeros_like(Z)
X[1,:] = 1
Y = np.zeros_like(Z)
Y[:,1] = 1
Y[:,2] = 2
Y[:,3] = 3
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet,
linewidth=0, antialiased=False)
ax.set_zlim3d(-10.0, 10.0)
ax.w_zaxis.set_major_locator(LinearLocator(10))
ax.w_zaxis.set_major_formatter(FormatStrFormatter('%.03f'))
m = cm.ScalarMappable(cmap=cm.jet)
m.set_array(Z)
fig.colorbar(m)
plt.show()
This shows:
Related
I am doing PCA, that has 350 points to plot. I have successfully plotted them on a 3D plot. I want to color them to tell which point is which on my 3D plot. Is it possible to color each one? Maybe color points 1 to 50 different shades of green, then 51 to 100 different shades of red, etc? And the lighter the shade of color the smaller the number is?
You mentioned you have been trying to do this with matplotlib and ax.scatter. I think for your example all the functionality you need is already built into ax.scater, with the c input argument. For example, does the following meet your requirements?
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
# Some dummy data
xval = np.random.randn(350)
yval = np.random.randn(350)
zval = np.exp( - (xval**2 + yval**2)/0.5)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
cax = ax.scatter(xval, yval, zval, cmap=plt.cm.viridis, c=zval)
fig.colorbar(cax)
There are two similar questions about this:
Individual alpha values in scatter plot
Add alpha to an existing matplotlib colormap
but neither of these address this issue. I need to produce a scatter plot with individual alphas (as in question 1.) but I need to combine this with a given colormap (as in question 2.)
This is what I came up with:
import numpy as np
import matplotlib.pylab as plt
from matplotlib.colors import ListedColormap
x = np.arange(10)
y = np.arange(10)
# These are the colors for my data
z = np.arange(10)
# These are the alpha values for my data
alphas = np.linspace(0.1, 1, 10)
# Color map I want to use
cm = plt.cm.get_cmap('viridis')
# Get the colormap colors for my data
my_cmap = cm(z)
# Set alpha
my_cmap[:, -1] = alphas
# Create new colormap
my_cmap = ListedColormap(my_cmap)
plt.subplot(121)
plt.scatter(x, y, cmap=cm, c=z)
plt.subplot(122)
plt.scatter(x, y, cmap=my_cmap, c=z)
plt.show()
but the result is not what I expect:
where the image to the left is what the scatter plot looks like using the colormap and no alphas, and the plot to the right is my attempt to add individual alphas to the data points.
Usual colormaps have 256 colors. Here you select only the first 10, which look roughly the same (all dark violet).
I suppose your code will run as expected when replacing my_cmap = cm(z) by
my_cmap = cm(plt.Normalize(z.min(), z.max())(z))
or
my_cmap = cm(np.linspace(0,1,len(z)))
Say I have the following plot:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1)
data = np.sort(np.random.rand(8,12))
plt.figure()
c = plt.pcolor(data, edgecolors='k', linewidths=4, cmap='Blues', vmin=0.0, vmax=1.0)
plt.colorbar(c)
plt.show()
The colorbar has the (almost) white color assigned to the lowest values. How do I make it slightly darker? I want that instead of the colorbar ranging from white to blue, it should range from light blue to dark blue. Like, the color for the value 0 should be something like what it is for the value 0.4 in the plot above.
I found this when searching about it, but the question (and the solutions) is about making all the colors darker, which is not what I am looking for.
Although the suggestion of #user3483203 is very good, you do re-interpolate the colormap. You could avoid this by first getting the colormap as a matrix of colors (based on the original interpolation) and then select a part of this matrix as your new colormap:
import matplotlib as mpl
cmap = mpl.cm.Blues(np.linspace(0,1,20))
cmap = mpl.colors.ListedColormap(cmap[10:,:-1])
Your example then becomes
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
cmap = mpl.cm.Blues(np.linspace(0,1,20))
cmap = mpl.colors.ListedColormap(cmap[10:,:-1])
np.random.seed(1)
data = np.sort(np.random.rand(8,12))
plt.figure()
c = plt.pcolor(data, edgecolors='k', linewidths=4, cmap=cmap, vmin=0.0, vmax=1.0)
plt.colorbar(c)
plt.show()
which gives
which is in this case probably equivalent to re-interpolated colormap, as Blues itself comes from some interpolation.
For other colormaps the results may be quite different. For example, for jet:
No new interpolation, but just a subset of the original colormap (i.e. current solution):
Using re-interpolation (i.e. #user3483203's solution):
Simply define your own custom colormap:
from matplotlib.colors import LinearSegmentedColormap
colors = [(0.6, 0.76, 0.98), (0, 0.21, 0.46)] # Experiment with this
cm = LinearSegmentedColormap.from_list('test', colors, N=10)
Then just plug it in for the cmap parameter:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1)
data = np.sort(np.random.rand(8,12))
plt.figure()
c = plt.pcolor(data, edgecolors='k', linewidths=4, cmap=cm, vmin=0.0, vmax=1.0)
plt.colorbar(c)
plt.show()
And the result:
Using set_clim is a simple way to get your colors adjusted the way you probably want:
c.set_clim(-0.5, 1.0)
This sets the color limit (first value is vmin and second is vmax).
↳ https://matplotlib.org/api/_as_gen/matplotlib.pyplot.clim.html
I have 2 variables (x,y) that change with time (t). I want to plot x vs. t and color the ticks based on the value of y. e.g. for highest values of y the tick color is dark green, for lowest value is dark red, and for intermediate values the color will be scaled in between green and red.
Can this be done with matplotlib in python?
This is what matplotlib.pyplot.scatter is for.
If no colormap is specified, scatter will use whatever the default colormap is set to. To specify which colormap scatter should use, use the cmap kwarg (e.g. cmap="jet").
As a quick example:
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np
# Generate data...
t = np.linspace(0, 2 * np.pi, 20)
x = np.sin(t)
y = np.cos(t)
plt.scatter(t, x, c=y, ec='k')
plt.show()
One may specify a custom color map and norm
cmap, norm = mcolors.from_levels_and_colors([0, 2, 5, 6], ['red', 'green', 'blue'])
plt.scatter(x, y, c=t, cmap=cmap, norm=norm)
If you want to plot lines instead of points, see this example, modified here to plot good/bad points representing a function as a black/red as appropriate:
def plot(xx, yy, good):
"""Plot data
Good parts are plotted as black, bad parts as red.
Parameters
----------
xx, yy : 1D arrays
Data to plot.
good : `numpy.ndarray`, boolean
Boolean array indicating if point is good.
"""
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
from matplotlib.colors import from_levels_and_colors
from matplotlib.collections import LineCollection
cmap, norm = from_levels_and_colors([0.0, 0.5, 1.5], ['red', 'black'])
points = np.array([xx, yy]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lines = LineCollection(segments, cmap=cmap, norm=norm)
lines.set_array(good.astype(int))
ax.add_collection(lines)
plt.show()
I am using scatter plot in matplotlib to plot some points. I have two 1D arrays each storing the x and y coordinate of the samples. Also there is another 1D array that stores the label(to decide in which colour the point should be plotted). I programmed thus far:
import matplotlib.pyplot as plt
X = [1,2,3,4,5,6,7]
Y = [1,2,3,4,5,6,7]
label = [0,1,4,2,3,1,1]
plt.scatter(X, Y, c= label, s=50)
plt.show()
Now I want to be able to see which color corresponds to which label?
I looked up the implementation of legends in matplotlib like the one here:
how to add legend for scatter()?
However they are suggesting to create a plot for each label of sample. However all my labels are in the same 1D array(label). How can I achieve this?
You could do it with a colormap. Some examples of how to do it are here.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as colors
X = [1,2,3,4,5,6,7]
Y = [1,2,3,4,5,6,7]
label = [0,1,4,2,3,1,1]
# Define a colormap with the right number of colors
cmap = plt.cm.get_cmap('jet',max(label)-min(label)+1)
bounds = range(min(label),max(label)+2)
norm = colors.BoundaryNorm(bounds, cmap.N)
plt.scatter(X, Y, c= label, s=50, cmap=cmap, norm=norm)
# Add a colorbar. Move the ticks up by 0.5, so they are centred on the colour.
cb=plt.colorbar(ticks=np.array(label)+0.5)
cb.set_ticklabels(label)
plt.show()
You might need to play around to get the tick labels centred on their colours, but you get the idea.