Thanks to visit codestin.com
Credit goes to github.com

Skip to content

cm.set_bad() not working for specific values of grayscale and dpi when saving as pdf #20575

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

Closed
bugalo opened this issue Jul 5, 2021 · 1 comment · Fixed by #25824
Closed

Comments

@bugalo
Copy link

bugalo commented Jul 5, 2021

Bug report

Bug summary

When modifying a colormap with cm.set_bad( color='0.9' ) and saving the image as pdf, the bad colors appear as green instead of gray. This happens for some data sets (I cannot reproduce it with synthetic data sets, it happens with some of my datasets), with some dpi values (for the attached example, it happens with dpi of 1200, but not with dpi of 1000), with some grayscale values (for the attached example, it doesn't happen with color values of '0.79' or darker), and when saving as pdf (it does not happen when displaying the figure with plt.show()).

This is the dataset used for the example code: Data.csv

Code for reproduction

# Paste your code here
#
#

import numpy as np
import pandas as pd

from copy import copy
from matplotlib import cm

from matplotlib import pyplot as plt

fig_size_px = (1280, 960)
fig_dpi = 200
fig_size_inch = tuple( fi/fig_dpi for fi in fig_size_px )

n = 1730
m = 1731

data = pd.read_csv( 'Data.csv', index_col=0, engine='c',  dtype=np.float64 ).values

Mask = np.uint8( np.reshape( data[ :, 0 ], ( m, n ) ) )
data = np.reshape( data[ :, 1 ], ( m, n ) )

cmap_jet = copy( cm.jet )
cmap_jet.set_bad( color='0.9' )
# cmap_jet.set_bad( color='0.78' )
# cmap_jet.set_bad( color=(0.9, 0.9, 0.9) )
# cmap_jet.set_bad( color='#d8dcd6' )

fig4 = plt.figure( 4, figsize=fig_size_inch, dpi=fig_dpi, facecolor='w', clear=True, tight_layout=True )
fig4.clf()
ax4 = fig4.add_subplot(111, aspect='equal')
ax4.cla()

data_masked = np.ma.MaskedArray( data, Mask==0 )

h4 = ax4.imshow( data_masked, origin='lower', cmap=cmap_jet, interpolation='Antialiased' )

ax4.get_xaxis().set_visible( False )
ax4.get_yaxis().set_visible( False )

fig4.savefig( 'Plot_Bad.pdf', dpi=fig_dpi*6, format='pdf', transparent=True )
fig4.savefig( 'Plot_Good.pdf', dpi=fig_dpi*5, format='pdf', transparent=True )
plt.show()

Actual outcome

Plot_Bad.pdf

# If applicable, paste the console output here
#
#

Expected outcome

Plot_Good.pdf

Matplotlib version

  • Operating system: Gentoo Linux
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.4.2
  • Matplotlib backend (print(matplotlib.get_backend())): GTK3Agg
  • Python version: 3.9.5
  • Jupyter version (if applicable):
  • Other libraries:

From the distribution repositories

@QuLogic
Copy link
Member

QuLogic commented May 5, 2023

This appears to be the same issue as #25806.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants