pythonmatplotlibspyder

PyPlot plots are bigger with high DPI, but still blurry


I am following a tutorial to generate a scatter plot of points, coloured by cluster and also colour-saturated based each point's strength of membership in its respective clusters. I mention the colouring details in case they affect the resolution, but I suspect they don't.

What I find is that if I increase the DPI of a PyPlot figure, the figure increases in size, but is still very blurry. Below is my test code, which generates a small DPI figure and a large DPI figure. The latter still seems extraordinarily blurry. What is causing this and how can I get sharp plots? I am using Spyder.

# Adapted from:
# https://hdbscan.readthedocs.io/en/latest/advanced_hdbscan.html

# Generate plot data
#-------------------
import numpy as np
data = np.load('clusterable_data.npy') # https://github.com/lmcinnes/hdbscan/blob/master/notebooks/clusterable_data.npy
import hdbscan
clusterer = hdbscan.HDBSCAN(min_cluster_size=15).fit(data)
import seaborn as sns
color_palette = sns.color_palette( 'deep',
   len( np.unique( clusterer.labels_ ) ) )
cluster_colors = [color_palette[x] if x >= 0
                  else (0.5, 0.5, 0.5)
                  for x in clusterer.labels_]
cluster_member_colors = [sns.desaturate(x, p) for x, p in
   zip(cluster_colors, clusterer.probabilities_)]

# Plot the scatter graph
#-----------------------
%matplotlib tk
   # To get separate window in Spyder
import matplotlib.pyplot as plt
plt.close('all')

# Low resolution
plt.rcParams['figure.dpi']=50
plt.scatter(*data.T, s=50, linewidth=0, c=cluster_member_colors, alpha=0.25)

# High resolution
plt.rcParams['figure.dpi']=150
plt.figure()
plt.scatter(*data.T, s=50, linewidth=0, c=cluster_member_colors, alpha=0.25)

enter image description here


Solution

  • They're blurry because you've set alpha=0.25, so the points are all nearly transparent. If you want transparency, then blurry is a side effect. If you don't want blurry, then play with the alpha value.