I have an Nx2 matrix X and an N-dim vector of labels y. For instance:
from sklearn.datasets.samples_generator import make_blobs
import matplotlib.pyplot as plt
X, y = make_blobs(n_samples=100, centers=2, n_features=2, random_state=2)
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolor='k')
plt.show()
In the background of this plot I want to plot two heatmaps, with a colormap that has the point's colour in the zones of high points density, so that the image looks like having a purple and a yellow cloud, each centered at the purple and yellow blobs.
This has been challenging for me. I tried creating a 2D histogram for each blob as shown in this answer, and also created a custom colormap so that the low density areas of the plot are white, and the high density areas are coloured with the blob's colour:
import numpy as np
import seaborn as sns
from matplotlib.colors import ListedColormap
palette_colors = sns.color_palette("deep")
palette = sns.light_palette(palette_colors[0], input="husl", n_colors=100)
my_cmap = ListedColormap(sns.color_palette(palette).as_hex())
whr1 = np.where(y==0)
whr2 = np.where(y==1)
x1 = X[whr1][:, 0]
y1 = X[whr1][:, 1]
x2 = X[whr2][:, 0]
y2 = X[whr2][:, 1]
heatmap1, xedges1, yedges1 = np.histogram2d(x1, y1, bins=50)
extent1 = [xedges1[0], xedges1[-1], yedges1[0], yedges1[-1]]
heatmap2, xedges2, yedges2 = np.histogram2d(x2, y2, bins=50)
extent2 = [xedges2[0], xedges2[-1], yedges2[0], yedges2[-1]]
But now I don't know how to plot those heatmaps using imshow. I also want to make sure that if the blobs overlap, so will the heatmaps so that one heatmap does not cover the other heatmap, but rather there is a combination of the heatmaps colours and intensities in the overlapping region.
I really appreciate your help!
You could use seaborn's kdeplot
x1,y1 = np.random.normal(loc=0.0, scale=1.0, size=(100,)), np.random.normal(loc=2.0, scale=1.0, size=(100,))
x2,y2 = np.random.normal(loc=2., scale=1.0, size=(100,)), np.random.normal(loc=0.0, scale=1.0, size=(100,))
fig, ax = plt.subplots()
sns.kdeplot(x1,y1, shade=True, shade_lowest=False, alpha=0.5, cbar=False, ax=ax, cmap="Blues")
sns.kdeplot(x2,y2, shade=True, shade_lowest=False, alpha=0.5, cbar=False, ax=ax, cmap="Oranges")
ax.scatter(x1,y1, color="C0")
ax.scatter(x2,y2, color="C1")
Related
I have a heatmap plotted above an image (as shown on image link 1), with gaussian filter and normalize data. The main issue is that there is no value under 92 on the y axis, so the plot doesnt start on (0,0), instead start on (0,92). So, when I put together both pictures (heatmap and background image), there is an abrupt cut on the graph (as shown on 2nd link, where is the heatmap without background).
So, how can I extend the axis on the heatmap so it start on (0,0)?
Below is the code that I'm currently using to plot both images. Thanks!
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from scipy.ndimage.filters import gaussian_filter
import matplotlib.colors as mcolors
from sklearn.preprocessing import normalize
x = df['x_data']
y = df['y_data']
heatmap, xedges, yedges = np.histogram2d(x, y, bins = [800,600])
extent = [0, xedges[-1], yedges[0], yedges[-1]]
heatmap = normalize(heatmap)
heatmap = gaussian_filter(heatmap, 16)
colors = [(1,1-c,0,c) for c in np.linspace(0,1,100)]
cmapred = mcolors.LinearSegmentedColormap.from_list('mycmap', colors, N=5)
map_img = mpimg.imread('dir/to/background/image.png')
fig, ax = plt.subplots(figsize=(16.1, 9.1))
plt.imshow(map_img, extent=[0, 800, 0, 600], cmap = 'Greys_r')
plt.imshow(heatmap.T, extent = extent, origin = 'lower', cmap = cmapred, alpha = 0.7)
plt.ylim([0,600])
plt.xlim([0,800])
plt.show()
Image of heatmap + background: https://imgur.com/2vX6Bw6
Image of only heatmap: https://imgur.com/axMe7K7
You could add rows to your heat map manually. Maybe easier is to try setting the histogram bins explicitly?
bins=[np.arange(0, 800, 1), np.arange(0, 600, 1)]
heatmap, xedges, yedges = np.histogram2d(x, y, bins=bins)
This is my plot:
I would like the coloring to be centered at 0 within the plot. While I managed to have the legend centered at 0, this does not apply to the dots in the plot (i.e. I would expect them to be gray at the zero value).
This is my code which generates the plots:
import matplotlib.colors as mcolors
import matplotlib.cm as cm
import seaborn as sns
def plot_jitter(df):
plot = sns.stripplot(x='category', y='overall_margin', hue='overall_margin', data=df,
palette='coolwarm_r',
jitter=True, edgecolor='none', alpha=.60)
plot.get_legend().set_visible(False)
sns.despine()
plt.axhline(0, 0,1,color='grey').set_linestyle("--")
#Drawing the side color bar
normalize = mcolors.TwoSlopeNorm(vcenter=0, vmin=df['overall_margin'].min(), vmax=df['overall_margin'].max())
colormap = cm.coolwarm_r
[plt.plot(color=colormap(normalize(x))) for x in df['overall_margin']]
scalarmappaple = cm.ScalarMappable(norm=normalize, cmap=colormap)
scalarmappaple.set_array(df['overall_margin'])
plt.colorbar(scalarmappaple)
By using sns.scatterplot instead of sns.stripplot you can use the c, norm and cmap parameters like so.
# Load demo data, scale `total_bill` to be in the range [0, 1]
tips = sns.load_dataset("tips")
tips["total_bill"] = tips["total_bill"].div(100)
Building the plot:
fig, ax = plt.subplots()
# Get/set params for the colour mapping
vcenter = 0.15
vmin, vmax = tips["total_bill"].min(), tips["total_bill"].max()
normalize = mcolors.TwoSlopeNorm(vcenter=vcenter, vmin=vmin, vmax=vmax)
colormap = cm.coolwarm_r
# plot with:
# - `c`: array of floats for colour mapping
# - `cmap`: the colourmap you want
# - `norm`: to scale the data from `c`
sns.scatterplot(
x="day",
y="total_bill",
data=tips,
c=tips["total_bill"],
norm=normalize,
cmap=colormap,
ax=ax,
)
ax.axhline(vcenter, color="grey", ls="--")
# Tweak the points to mimic `sns.stripplot`
pts = ax.collections[0]
pts.set_offsets(pts.get_offsets() + np.c_[np.random.uniform(-.1, .1, len(tips)), np.zeros(len(tips))])
ax.margins(x=0.15)
scalarmappaple = cm.ScalarMappable(norm=normalize, cmap=colormap)
scalarmappaple.set_array(tips["total_bill"])
fig.colorbar(scalarmappaple)
Which produces:
The code to mimic stripplot is from seaborn's github issues
I want to plot some scatter plots over the map of a country (an image). The idea is to depict the data visualization of the area at which the plot is plotted.
So, this is how I plot the image of the map of USA, where the circles I have drawn towards the top left and the middle are where I would like to display my scatter plots:
import numpy as np
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(10,15))
im = plt.imread("usa-states-map.jpg")
implot = plt.imshow(im, extent=[0, 200, 0, 150])
# A circle in the upper left region
theta=np.linspace(0,2*np.pi,50)
faux_radius = 15
z=np.cos(theta)*faux_radius + 45
t=np.sin(theta)*faux_radius + 130
plt.plot(z,t)
# A circle in the middle region
theta=np.linspace(0,3*np.pi,50)
faux_radius = 15
z=np.cos(theta)*faux_radius + 100
t=np.sin(theta)*faux_radius + 80
plt.plot(z,t)
This plots the image like so:
I proceed to plot the scatter plots like so:
import numpy as np
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(10,15))
im = plt.imread("usa-states-map.jpg")
implot = plt.imshow(im, extent=[0, 200, 0, 150])
# A circle in the upper left region
theta=np.linspace(0,2*np.pi,50)
faux_radius = 15
z=np.cos(theta)*faux_radius + 45
t=np.sin(theta)*faux_radius + 130
plt.plot(z,t)
# A circle in the middle region
theta=np.linspace(0,3*np.pi,50)
faux_radius = 15
z=np.cos(theta)*faux_radius + 100
t=np.sin(theta)*faux_radius + 80
plt.plot(z,t)
# Scatter plot 1
ax1 = plt.subplot(2,2,1)
ax1.scatter(x_1_a, y_1_a, marker="s")
ax1.scatter(x_1_b, y_1_b, marker="o")
# Scatter plot 2
ax1 = plt.subplot(2,2,2)
ax1.scatter(x_2_a, y_2_a, marker="s")
ax1.scatter(x_2_a, y_2_b, marker="o")
But the output it produces does not display the background image, and only plots the scatter plots:
[]
I even tried using zorder which is supposed to tell matplotlib which plot should come on top and which on bottom, but to no avail - it produces the same output as above:
implot = plt.imshow(im, extent=[0, 200, 0, 150], zorder=1)
...
...
...
# Scatter plot 1
ax1 = plt.subplot(2,2,1)
ax1.scatter(x_1_a, y_1_a, marker="s", zorder=2)
ax1.scatter(x_1_b, y_1_b, marker="o", zorder=2)
# Scatter plot 2
ax1 = plt.subplot(2,2,2)
ax1.scatter(x_2_a, y_2_a, marker="s", zorder=3)
ax1.scatter(x_2_a, y_2_b, marker="o", zorder=3)
How do I fix this to get the desired result? I don't even need the 2 circles to be present on the map actually - those were just to illustrate where I would like to plot the 2 scatter plots. Thanks.
I was able to solve the problem using the plt.axes suggestion in the comments:
from mpl_toolkits.axes_grid.inset_locator import inset_axes
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(10, 15),facecolor='white')
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
im = plt.imread("usa-states-map.jpg")
implot = plt.imshow(im)
plt.xticks([])
plt.yticks([])
# this is an inset axes over the main axes for the top left region
a = plt.axes([.2, .6, .2, .1], facecolor='w')
plt.scatter(x_1_a, y_1_a, marker="s")
plt.scatter(x_1_b, y_1_b, marker="o")
plt.legend(['%.2f%%' %(100*len(x_1_a)/(len(x_1_a)+len(y_1_a))), '%.2f%%' %(100*len(y_1_a)/(len(x_1_a)+len(y_1_a)))], loc='upper right');
# this is an inset axes over the main axes for the middle region
a = plt.axes([.45, .45, .2, .1], facecolor='w')
plt.scatter(x_2_a, y_2_a, marker="s")
plt.scatter(x_2_b, y_2_b, marker="o")
plt.legend(['%.2f%%' %(100*len(x_2_b)/(len(x_2_b)+len(y_2_b))), '%.2f%%' %(100*len(y_2_b)/(len(x_2_b)+len(y_2_b)))], loc='upper right');
plt.show()
In Jfreechart there is a method called setQuadrantPaint which let's you set the background colour of a given quandrant in a plot.
How would you achieve the equivalent in matplotlib?
E.g.
You can plot a 2x2 array with imshow in the background. Giving it an extent will make the center of it always at 0,0.
import numpy as np
import matplotlib.pyplot as plt
x1, y1 = np.random.randint(-8,8,5), np.random.randint(-8,8,5)
x2, y2 = np.random.randint(-8,8,5), np.random.randint(-8,8,5)
vmax = np.abs(np.concatenate([x1,x2,y1,y2])).max() + 5
extent = [vmax*-1,vmax, vmax*-1,vmax]
arr = np.array([[1,0],[0,1]])
fig, ax = plt.subplots(1,1)
ax.scatter(x1,y1, marker='s', s=30, c='r', edgecolors='red', lw=1)
ax.scatter(x2,y2, marker='s', s=30, c='none', edgecolors='red', lw=1)
ax.autoscale(False)
ax.imshow(arr, extent=extent, cmap=plt.cm.Greys, interpolation='none', alpha=.1)
ax.axhline(0, color='grey')
ax.grid(True)
Setting the autoscale to False after the data points are plotted, but before the image is, makes sure that the axes scales only to the data points.
How can I create a 3D plot with a color gradient for the points? See the example below, which works for a 2D scatter plot.
Edit (thanks to Chris): What I'm expecting to see from the 3D plot is a color gradient of the points ranging from red to green as in the 2D scatter plot.
What I see in the 3D scatter plot are only red points.
Solution: for some reasons (related to the gradient example I copied elsewhere) I set xrange to len-1, which messes everything in the 3D plot.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create Map
cm = plt.get_cmap("RdYlGn")
x = np.random.rand(30)
y = np.random.rand(30)
z = np.random.rand(30)
#col = [cm(float(i)/(29)) for i in xrange(29)] # BAD!!!
col = [cm(float(i)/(30)) for i in xrange(30)]
# 2D Plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x, y, s=10, c=col, marker='o')
# 3D Plot
fig = plt.figure()
ax3D = fig.add_subplot(111, projection='3d')
ax3D.scatter(x, y, z, s=10, c=col, marker='o')
plt.show()
Here is an example for 3d scatter with gradient colors:
import matplotlib.cm as cmx
from mpl_toolkits.mplot3d import Axes3D
def scatter3d(x,y,z, cs, colorsMap='jet'):
cm = plt.get_cmap(colorsMap)
cNorm = matplotlib.colors.Normalize(vmin=min(cs), vmax=max(cs))
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm)
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x, y, z, c=scalarMap.to_rgba(cs))
scalarMap.set_array(cs)
fig.colorbar(scalarMap)
plt.show()
Of course, you can choose the scale to range between different values, like 0 and 1.
Following works: I can't figure out why yours doesn't. You should be able to set color as a sequence of RGBA floats, or just sequence of floats.
# Create Map
cm = plt.get_cmap("RdYlGn")
x = np.random.rand(30)
y = np.random.rand(30)
z = np.random.rand(30)
col = np.arange(30)
# 2D Plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x, y, s=10, c=col, marker='o')
# 3D Plot
fig = plt.figure()
ax3D = fig.add_subplot(111, projection='3d')
p3d = ax3D.scatter(x, y, z, s=30, c=col, marker='o')
plt.show()
However, in help of scatter, I see the following, it may be related.
A :class:`matplotlib.colors.Colormap` instance or registered
name. If *None*, defaults to rc ``image.cmap``. *cmap* is
only used if *c* is an array of floats.