-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
savefig() as PNG get a different result than the image shown in ipython notebook(SVG probably) #5859
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
Comments
To clairfy, if you edit |
@tacaswell Double checked, Yes. Change the two lines as said in Then I can get the same image both by |
Sorry, I misclosed the issue by clicking "Close and comment", I mean “Close the Preview and comment" |
@tacaswell @pelson %matplotlib inline
%config InlineBackend.figure_format = 'svg'
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#prepare the data
data = pd.DataFrame(np.array([[123.5, 39.2, 0, 1, 2, 3, 4],
[123.5, 39.3, 5, 6, 0, 1, 2],
[121.5, 40.7, 3, 4, 5, 6, 0],
[121.5, 40.3, 1, 2, 3, 4, 5]]),
index=['D01', 'D02', 'D03', 'D04'],
columns=['x', 'y', 'g_3', 'g_4', 'g_5', 'g_6', 'g_7'])
labels = ['0', '0-0.5', '0.5-1', '1-2', '2-3', '3-4', '4-5']
groups = [data.groupby('g_'+str(i)) for i in [3, 5, 6, 4, 7]]
x0, x1, y0, y1 = extent = 121, 124, 38.8, 41.1
width = 5
height = width * (y1-y0)/(x1-x0)
# creating 3*2 maps. Actually I only need 5, but I don't how to do that while x&y
# being both shared. So let's put a legend on the 6th one.
fig, axs = plt.subplots(3, 2, sharex=True, sharey=True,
figsize=(width*2, height*3),
dpi=300, subplot_kw={'projection': ccrs.PlateCarree()})
# for everymap
for i, ax in enumerate(axs.flatten()):
#some settings of everymap
ax.set_extent(extent)
ax.set_xticks([122, 123], crs=ccrs.PlateCarree())
ax.set_yticks([39, 40, 41], crs=ccrs.PlateCarree())
ax.xaxis.set_major_formatter(LongitudeFormatter())
ax.yaxis.set_major_formatter(LatitudeFormatter())
[tick.label.set_fontsize(11) for tick in ax.yaxis.get_major_ticks()]
[tick.label.set_fontsize(11) for tick in ax.xaxis.get_major_ticks()]
#plot the point for the first 5th map
if i < 5:
for n, g in groups[i]:
ax.scatter(g.x, g.y, s=10*n**2, c='k', transform=ccrs.PlateCarree())
# I need a legend for all 5 maps, let's put it on the 6th map
# I don't need to plot on the 6th map, so, I put it outside the extent
for n in range(len(labels)):
ax.scatter(120, 38, s=10*n**2, c='k', transform=ccrs.PlateCarree(),
label=labels[n])
ax.legend(loc='upper right', scatterpoints=1)
fig.tight_layout()
fig.savefig('a.png', dpi=300) By running the code in notebook, I got an image shown as And the Difference:
Besides those, what's the right way to do if I only need 3 or 5 (any odd number other than 1) axes? Because by my way, I have to erase the 6th axis in Photoshop and move the 6th's Please tell me if any more information is needede. |
@gepcel Can you open a new issue for misplaced subplots? |
Have you tried deleting your font cache in ~/.matplotlib or ~/.config/matplotlib? matplotlib may not be picking up the font. |
@tacaswell yes, I can open a new issue for misplaced subplots tomorrow. I'll check if I can still produce this in my current environment. Thanks for replying @mdboom . Tomorrow, I'll check that. It has been a long time. |
@tacaswell I have opened a new issue about the misplaced subplots as #6457. |
For me, "deleting the ~/.matplotlib" can solve my problem, I'm fine if anyone close this issue, or anyone still want to do something. |
@gepcel Hi, gepcel, I have the same puzzle with you, I am user Mac, the plt.show() display the image fine, but when I savefig() it went wrong whatever png or svg. I delete the cache file mentioned above but it still didn't work, could you show me more detial about how you solve this, many thks if you help me. |
This section of the docs http://matplotlib.org/devdocs/users/text_props.html#text-with-non-latin-glyphs maybe helpful here. |
Thank you very much, I have figure this problem out. For anyone who maybe count this, here is my suggestion: no matter Windows or Mac OS, just find the path that matplotlib use for font store, and replace the default font "Vera" with your right font. |
@Elgins Replacing the 'vera' files with your preferred font is an extremely bad idea. |
@tacaswell Yeah, I am not professional, this should be the last method to try:) |
Reporter says this is working. |
I'm using matplotlib to plot some images in ipython notebook. But after saving fig as PNG, I get a different image than the one shown in the notebook. I've come across this issue multiple times. Mostly
savefig()
lose the font info, but there maybe others. Can anyone take a look?The code is as following:
I got the fig as following in notebook:

And the 'a.png' from 'savefig()' is as following:

I'm doing some quick plot and formatting, using both 'pyplot' and 'pylab' sometimes, sorry for the dirty code.
Some backgroud:
Some test:
The text was updated successfully, but these errors were encountered: