Skip to content

pcolormesh with shading=gouraud gives error when saved as eps #16217

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
lclanzi opened this issue Jan 14, 2020 · 7 comments
Closed

pcolormesh with shading=gouraud gives error when saved as eps #16217

lclanzi opened this issue Jan 14, 2020 · 7 comments
Milestone

Comments

@lclanzi
Copy link

lclanzi commented Jan 14, 2020

Bug summary
I have a pcolormesh that has shading='gouraud', and I am trying to save it to an .eps. If I save the image as .png everything works fine, but when I save it as .eps it gives error. A similar issue was already open and then closed in the past.

Code for reproduction
import matplotlib.pyplot as plt
import numpy as np
plt.pcolormesh(np.random.randn(10,10), shading='gouraud')
plt.gcf().savefig('test.eps')

Matplotlib version
matplotlib 3.1.1
Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]

@lclanzi
Copy link
Author

lclanzi commented Jan 14, 2020

I provide an example. The first figure (etaGainH.png) is the one I save from python with plt.savefig('etaGainH.png'). The second figure is the one I get when I use plt.savefig('etaGainH.eps').

etaGainH
etaGainH-eps-converted-to.pdf

This is the code I'm using:

fig, ax = plt.subplots(1,1,figsize=(5.3,4.3))
X,Y = np.meshgrid(hstar,invparam,indexing='ij')
cmap = mpl.cm.get_cmap('viridis') ;\
plt.pcolormesh(X,Y,etaGain*100,cmap=cmap,shading='gouraud');\
cbar = plt.colorbar()
cbar.set_label(r'$\mathcal{G}_{\eta}\;[\%]$',fontsize = 14)
plt.ylim([0.5,1.5])
plt.xlim([0.16,0.4])
plt.savefig('etaGainH.eps',format='eps',bbox_inches='tight',dpi=300)

Note that if I use shading='flat' instead of shading='gouraud', this is what I get

etaGainHFlat
etaGainHFlat-eps-converted-to.pdf

Code

fig, ax = plt.subplots(1,1,figsize=(5.3,4.3))
X,Y = np.meshgrid(hstar,invparam,indexing='ij')
cmap = mpl.cm.get_cmap('viridis') ;\
plt.pcolormesh(X,Y,etaGain*100,cmap=cmap,shading='flat');\
cbar = plt.colorbar()
cbar.set_label(r'$\mathcal{G}_{\eta}\;[\%]$',fontsize = 14)
plt.ylim([0.5,1.5])
plt.xlim([0.16,0.4])
plt.savefig('etaGainH.eps',format='eps',bbox_inches='tight',dpi=300)

The problem seems to be related to this particular shading option.
Hope that the issue is more clear.

@anntzer
Copy link
Contributor

anntzer commented Jan 14, 2020

Locally this works for me, and #1993 suggests that it indeed should.

@lclanzi
Copy link
Author

lclanzi commented Jan 14, 2020

Hi, thank you for your reply.
I did not explain well what the problem is in the first post.
The code

import matplotlib.pyplot as plt
import numpy as np
plt.pcolormesh(np.random.randn(10,10), shading='gouraud')
plt.gcf().savefig('test.eps')

works fine. However, when I save the image with .eps, some pixels change. You can see the difference in my second post. This problem does not exist when I use shading='flat' or when I save the image as .png.

@anntzer
Copy link
Contributor

anntzer commented Jan 14, 2020

Your example is not reproducible: we don't have the hstar, invparam variables.

@lclanzi
Copy link
Author

lclanzi commented Jan 14, 2020

I'm sorry, you are right.
Here it is a reproducible example.

For images with shading='gouraud':

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,1,25)
y = np.linspace(0,1,25)
X,Y = np.meshgrid(x,y,indexing='ij')
z = np.sin(X*2*np.pi) + np.cos(Y*2*np.pi)
cmap = plt.get_cmap('viridis')
plt.subplots(1,1,figsize=(5.3,4.3))
plt.pcolormesh(x, y, z, cmap=cmap, shading='gouraud')
cbar = plt.colorbar()
plt.title('Gouraud')
plt.savefig('TestGou.eps',bbox_inches='tight',dpi=300)

Output:
TestGou
TestGou-eps-converted-to.pdf

For images with shading='flat':

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,1,25)
y = np.linspace(0,1,25)
X,Y = np.meshgrid(x,y,indexing='ij')
z = np.sin(X*2*np.pi) + np.cos(Y*2*np.pi)
cmap = plt.get_cmap('viridis')
plt.subplots(1,1,figsize=(5.3,4.3))
plt.pcolormesh(x, y, z, cmap=cmap, shading='flat')
cbar = plt.colorbar()
plt.title('Flat')
plt.savefig('TestFlat.eps',bbox_inches='tight',dpi=300)

Output:
TestFlat
TestFlat-eps-converted-to.pdf

If you compare the image TestGou.png and TestGou-eps-converted-to.pdf, they are different. On the other hand, TestFlat-eps-converted-to.pdf and TestFlat.png are identical.
I would like to know where I'm doing something wrong. Thank you in advance for your help.

@anntzer
Copy link
Contributor

anntzer commented Jan 14, 2020

Yes, I can confirm the difference now.

@QuLogic
Copy link
Member

QuLogic commented Apr 21, 2023

I'm not sure of the exact portion of the change that did it, but this was fixed by #23294.

@QuLogic QuLogic closed this as completed Apr 21, 2023
@QuLogic QuLogic added this to the v3.6.0 milestone Apr 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants