Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version
  1. Import the necessary packages:
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_scatter_density import ScatterDensity
  1. Create data points for x, y, and density values:
x = [1, 2, 3, 4, 5]
y = [1, 1, 2, 2, 2]
density = [5, 10, 20, 30, 40]
  1. Set up the scatter plot with mpl-scatter-density:
fig, ax = plt.subplots()
sc = ScatterDensity(ax, x, y, density)
  1. Create a colormap with the desired dot size:
cmap = cm.get_cmap('jet', 30) #create a color map using the 'jet' color scheme with 30 different colors
sizes = [20, 40, 60, 80, 100] #a list of five different dot sizes to correspond with the five different density values
  1. Update the scatter plot with the new colormap and dot sizes:
sc.scatter(s=100, marker='.', cmap=cmap, sizes=sizes, lw=0)

The sizes argument in sc.scatter() specifies the different dot sizes to be used for the different density levels. The s=100 argument sets the default size for the dots in the plot. Adjust the sizes argument and the s argument as needed to achieve the desired dot size.