I have numpy array of shape (4000000, 200, 3), where first dimension relates to image height, second - width.
I m confused how to save this image as png (or any other format) with high resolution, because when I set dpi = 5000 then I get mermory error
Here is my code
fig, ax = plt.subplots()
im = ax.imshow(final_image_train)
ax.axis('off')
plt.savefig('final.png', dpi = 5000, bbox_inches = 'tight')
Any suggestions are appreciated.
Are you using the default figsize? This parameter gives a determined amount of space to the elements inside the figure, including ticklabels.
Then, if you know which pixel size is needed, for example (1200, 600), you need to choose the combination of figure size and dpi. An example relation would be:
figsize=(12,6) , dpi=100
figsize=( 8,4) , dpi=150
figsize=( 6,3) , dpi=200
There is more about it on other stack overflow posts like this one. Your dpi seems to be extremely high, maybe you need to calculate the dpi and figsize better...
Now, this answer part is just a recommendation. Is the matplotlib and .png mandatory? If not, have a look at the plotly library, which lets you create interactive plots, which are really good if you need to explore a lot of data (.html format). You have the offline version of the library, if you are interested. Also, here you have subplots examples.
Related
I have a large tiff file (around 2GB) containing a map. I have been able to successfully read the data and even display it using the following python code:
import rasterio
from rasterio.plot import show
with rasterio.open("image.tif") as img:
show(img)
data = img.read()
This works just fine. However, I need to be able to display specific parts of this map without having to load the entire file into memory (as it takes up too much of the RAM and is not doable on many other PCs). I tried using the Window class of rasterio in order to that, but when I tried to display the map the outcome was different from how the full map is displayed (as if it caused data loss):
import rasterio
from rasterio.plot import show
from rasterio.windows import Window
with rasterio.open("image.tif") as img:
data = img.read(window=Window(0, 0, 100000, 100000))
show(data)
So my question is, how can I display a part of the map without having to load into memory the entire file, while also making it look as if it had been cropped from the full map image?
thanks in advance :)
The reason that it displays nicely in the first case, but not in the second, is that in the first case you pass an instance of rasterio.DatasetReader to show (show(img)), but in the second case you pass in a numpy array (show(data)). The DatasetReader contains additional information, in particular an affine transformation and color interpretation, which show uses.
The additional things show does in the first case (for RGB data) can be recreated for the windowed case like so:
import rasterio
from rasterio.enums import ColorInterp
from rasterio.plot import show
from rasterio.windows import Window
with rasterio.open("image.tif") as img:
window = Window(0, 0, 100000, 100000)
# Lookup table for the color space in the source file
source_colorinterp = dict(zip(img.colorinterp, img.indexes))
# Read the image in the proper order so the numpy array will have the colors in the
# order expected by matplotlib (RGB)
rgb_indexes = [
source_colorinterp[ci]
for ci in (ColorInterp.red, ColorInterp.green, ColorInterp.blue)
]
data = img.read(rgb_indexes, window=window)
# Also pass in the affine transform corresponding to the window in order to
# display the correct coordinates and possibly orientation
show(data, transform=img.window_transform(window))
(I figured out what show does by looking at the source code here)
In case of data with a single channel, the underlying matplotlib library used for plotting scales the color range based on the min and max value of the data. To get exactly the same colors as before, you'll need to know the min and max of the whole image, or some values that come reasonably close.
Then you can explicitly tell matplotlib's imshow how to scale:
with rasterio.open("image.tif") as img:
window = Window(0, 0, 100000, 100000)
data = img.read(window=window, masked=True)
# adjust these
value_min = 0
value_max = 255
show(data, transform=img.window_transform(window), vmin=value_min, vmax=value_max)
Additional kwargs (like vmin and vmax here) will be passed on to matplotlib.axes.Axes.imshow, as documented here.
From the matplotlib documenation:
vmin, vmax: float, optional
When using scalar data and no explicit norm, vmin and vmax define the data range that the colormap covers. By default, the colormap covers the complete value range of the supplied data. It is deprecated to use vmin/vmax when norm is given. When using RGB(A) data, parameters vmin/vmax are ignored.
That way you could also change the colormap it uses etc.
I am trying to save a plot into a file using plt.savefig, however I am dissatisfied with the output picture quality. Changing dpi option doesn't help.
plt.savefig('filename.png', dpi=1200, format='png', bbox_inches='tight')
I tried saving to 'svg' and 'eps' - makes no difference. I wonder if the problem is with something else, like version of some library or OS or something alike. It also looks like the problem is not with resolution but the way lines and symbols are drawn - too bold.
plt.show() shows significantly better picture, and I can save it to png with satisfying quality - and surprisingly file size is about 8 times smaller (because of compressing, I suppose, which is fine.)
Part of the picture saved using savefig()
The same part of the picture saved from plot.show()
Figsize option did the trick for me.
The idea is that default parameters for saving to file and for displaying the chart are different for different devices. That's why representation was different in my case.
It's possible to adjust settings manually (as Piotrek suggests), but for me it was enough just to increase figure size - this setting is shared and allows python to auto-adjust visualization.
More details are on the page Piotrek mentioned, answered by doug and Karmel.
I have several subplots, so i used it like that:
fig, ax = plt.subplots(nrows=4, ncols=1, figsize=(20, 10))
For one plot case command is like that:
plt.figure(figsize=(20,10))
P.S. figsize parameters are in inches, not pixels.
Have a look here: Styles and Futurile
In short, you can experiment with the following options to edit the line, ticks etc.
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = 'Ubuntu'
plt.rcParams['font.monospace'] = 'Ubuntu Mono'
plt.rcParams['font.size'] = 10
plt.rcParams['axes.labelsize'] = 10
plt.rcParams['axes.labelweight'] = 'bold'
plt.rcParams['axes.titlesize'] = 10
plt.rcParams['xtick.labelsize'] = 8
plt.rcParams['ytick.labelsize'] = 8
plt.rcParams['legend.fontsize'] = 10
plt.rcParams['figure.titlesize'] = 12
Also have a look at this topic:
matplotlib savefig() plots different from show()
I want to embed a zoomed portion of a FITS image in the same plot with APLpy.
But when loading a FITS file with APLpy, there is only a 'FITSFigure' object returned.
fig = aplpy.FITSFigure('tmp.fits', slices=[0,0])
Is it possible to make it work with zoomed_inset_axes like here , or there are some other solution?
You may specify the figure to which to plot with aplpy. You can then get the axes inside the figure.
fig = plt.figure()
aplpyfig = aplpy.FITSFigure('tmp.fits', figure=fig)
axes = fig.get_axes()
From that point onwards you can work with that axes and use any of the methods that matplotlib offers to obtain insets.
Also see this question: Aplpy multiplot dynamic axis sharing
I am using word cloud with some txt files. How do I change this example if I wanted to 1) increase resolution and 2) remove empty border.
#!/usr/bin/env python2
"""
Minimal Example
===============
Generating a square wordcloud from the US constitution using default arguments.
"""
from os import path
import matplotlib.pyplot as plt
from wordcloud import WordCloud
d = path.dirname(__file__)
# Read the whole text.
text = open(path.join(d, 'constitution.txt')).read()
wordcloud = WordCloud().generate(text)
# Open a plot of the generated image.
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
You can't increase the resolution of the image in plt.show() since that is determined by your screen, but you can increase the size. This allows it to scale, zoom, etc. without blurring. To do this pass dimensions to WordCloud, e.g.
wordcloud = WordCloud(width=800, height=400).generate(text)
However, this just determines the size of the image created by WordCloud. When you display this using matplotlib it is scaled to the size of the plot canvas, which is (by default) around 800x600 and you again lose quality. To fix this you need to specify the size of the figure before you call imshow, e.g.
plt.figure( figsize=(20,10) )
plt.imshow(wordcloud)
By doing this I can successfully create a 2000x1000 high resolution word cloud.
For your second question (removing the border) first we could set the border to black, so it is less apparent, e.g.
plt.figure( figsize=(20,10), facecolor='k' )
You can also shrink the size of the border by using tight_layout, e.g.
plt.tight_layout(pad=0)
The final code:
# Read the whole text.
text = open(path.join(d, 'constitution.txt')).read()
wordcloud = WordCloud(width=1600, height=800).generate(text)
# Open a plot of the generated image.
plt.figure( figsize=(20,10), facecolor='k')
plt.imshow(wordcloud)
plt.axis("off")
plt.tight_layout(pad=0)
plt.show()
By replacing the last two lines with the following you can get the final output shown below:
plt.savefig('wordcloud.png', facecolor='k', bbox_inches='tight')
If you are trying to use an image as a mask, make sure to use a big image to get better image quality.. I spent hours figuring this out.
Heres an example of a code snippet I used
mask = np.array(Image.open('path_to_your_image'))
image_colors = ImageColorGenerator(mask)
wordcloud = WordCloud(width=1600, height=800, background_color="rgba(255, 255, 255, 0)", mask=mask
,color_func = image_colors).generate_from_frequencies(x)
# Display the generated image:
plt.figure( figsize=(20,10) )
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
It is very simple, plt.tight_layout(pad=0) does the job, reduces the space in the background, removing the excess padding.
You can use the method to_svg and get a resolution however high you want.
with open("Output.svg", "w") as text_file:
text_file.write(wc.to_svg())
Try an example by appending these two lines to this file, and the result is gorgeous.
(Other answers have addressed the border problem, and also the example doe not have a border.)
In case you run into the issue of slower application while improving the resolution ie. in a web application, the WordCloud documentation advises that you utilize the scale parameter along with the canvas' width & height params to get a resolution & response time that works for your use case.
Blurry wordclouds - I've been wrestling with this. For my use, I found that too large a differential in the between the most frequent word occurrences and those with few occurrences left the lower-count words unreadable. When I scaled the more frequent counts to reduce the differential, all the lower-frequency words were much more readable.
This question already has answers here:
How do I change the size of figures drawn with Matplotlib?
(14 answers)
Closed 1 year ago.
I want to obtain fig1 exactly of 4 by 3 inch sized, and in tiff format correcting the program below:
import matplotlib.pyplot as plt
list1 = [3,4,5,6,9,12]
list2 = [8,12,14,15,17,20]
plt.plot(list1, list2)
plt.savefig('fig1.png', dpi = 300)
plt.close()
You can set the figure size if you explicitly create the figure with
plt.figure(figsize=(3,4))
You need to set figure size before calling plt.plot()
To change the format of the saved figure just change the extension in the file name. However, I don't know if any of matplotlib backends support tiff
You can change the size of the plot by adding this before you create the figure.
plt.rcParams["figure.figsize"] = [16,9]
The first part (setting the output size explictly) isn't too hard:
import matplotlib.pyplot as plt
list1 = [3,4,5,6,9,12]
list2 = [8,12,14,15,17,20]
fig = plt.figure(figsize=(4,3))
ax = fig.add_subplot(111)
ax.plot(list1, list2)
fig.savefig('fig1.png', dpi = 300)
fig.close()
But after a quick google search on matplotlib + tiff, I'm not convinced that matplotlib can make tiff plots. There is some mention of the GDK backend being able to do it.
One option would be to convert the output with a tool like imagemagick's convert.
(Another option is to wait around here until a real matplotlib expert shows up and proves me wrong ;-)
If you need to change the figure size after you have created it, use the methods
fig = plt.figure()
fig.set_figheight(value_height)
fig.set_figwidth(value_width)
where value_height and value_width are in inches. For me this is the most practical way.