-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make base color white #22
Comments
@cbelth - I think you should be able to pass |
That makes the background white, but there is no discernible gradient from light red to dark red so all densities look the same. |
@cbelth - could you show your example here? (code and plot). Normally there should be a gradient, but I wonder if somehow the max/min values for the density aren't being chosen correctly. |
I used the LinearSegmentedColormap to create viridis-like colormap with white base color ("from 0 to 1e-20"). I don't know if there are better options. from matplotlib.colors import LinearSegmentedColormap
white_viridis = LinearSegmentedColormap.from_list('white_viridis', [
(0, '#ffffff'),
(1e-20, '#440053'),
(0.2, '#404388'),
(0.4, '#2a788e'),
(0.6, '#21a784'),
(0.8, '#78d151'),
(1, '#fde624'),
], N=256)
ax = fig.add_subplot(1, 1, 1, projection='scatter_density')
density = ax.scatter_density(x, y, cmap=white_viridis)
fig.colorbar(density, label='Number of points per pixel') Example: |
@np-8 That does the job for me! Thanks a lot! |
There is a function So you can use import numpy as np
import matplotlib.pyplot as plt
import mpl_scatter_density
from matplotlib import cm
import copy
cmap = copy.copy(cm.get_cmap("viridis"))
cmap.set_under(alpha=0)
data = np.random.multivariate_normal([0, 0], [[1, 2.85], [2.85, 9]], 1000000)
x, y = data[:, 0], data[:, 1]
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='scatter_density')
density = ax.scatter_density(x, y, cmap=cmap,
vmin=0.5, vmax=np.nanmax,
dpi=150, downres_factor=2)
cb = fig.colorbar(density, label='Number of points per pixel') In my case, the goal was to imitate plots typically used in Flow Cytometry. The requirements that the background be white where density is zero and that individual data points (outliers) be clearly visible are met with @np-8's trick or with the solution I propose above. Beyond that, I found that the "jet" colormap with a from matplotlib.colors import LogNorm
cmap = copy.copy(cm.get_cmap("jet"))
cmap.set_under(alpha=0)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='scatter_density')
density = ax.scatter_density(x, y, cmap=cmap,
norm=LogNorm(vmin=0.5, vmax=x.size),
dpi=36, downres_factor=2)
cb = fig.colorbar(density, label='Number of points per pixel') |
Is there any way to make the color corresponding with 0 density white and have the color approach red as the density increases?
The text was updated successfully, but these errors were encountered: