plt.show() strong discrepancy to plt.savefig() PNG, PDF & SVG (which are all ‘identical’)

Bug report

Bug summary

The saved figures of a scatter plot are rendered/interpreted completely different from showing them. This is consistent and might be related to #18871 and/or #8847. Is there a way to circumvent this and to get closer to the desired plot from ‘show()’ whilst saving to a file?

Code for reproduction

import matplotlib.pyplot as plt         # for my tests version 3.3.2
import numpy as np

# Creating dataset
np.random.seed(42)
x = np.random.random_sample(size=10000)
y = np.random.random_sample(size=10000)
z = np.random.random_sample(size=10000)

# Creating figure
fig = plt.figure(figsize=(16, 9))
ax = plt.axes(projection="3d")

# Add x, y gridlines
ax.grid(b=True, color='grey', linestyle='-.', linewidth=0.3, alpha=0.2)

# Creating color map
# https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html
my_cmap = plt.get_cmap('brg')

# Creating plot
sctt = ax.scatter3D(x, y, z, alpha=0.8, c=z, cmap=my_cmap, marker='o')

plt.title("3D-Scatter")
ax.set_xlabel('x label', fontweight='bold')
ax.set_ylabel('y label', fontweight='bold')
ax.set_zlabel('z label', fontweight='bold')
fig.colorbar(sctt, ax=ax, shrink=0.5, aspect=5)

# show plot
plt.show()
# plt.savefig('fig_savefig.png')
plt.close()

Actual outcome
fig_savefig

Expected outcome
fig_show

Matplotlib version

  • Operating system: Win10
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.3.2
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]
  • Jupyter version (if applicable):
  • Other libraries:

1 possible answer(s) on “plt.show() strong discrepancy to plt.savefig() PNG, PDF & SVG (which are all ‘identical’)