I have plotted a graph with 14 subplots in matplotlib. In the window the plot looks like this-
I save this plot using following command-
import matplotlib.pyplot as plt
plt.savefig('img.png')
But the image that is saved looked like this-
Notice that the x axis labels get overlapped because the image is shrinked. The savefig() function has optional argument dpi, but it changes the resolution/quality of saved plot.
I also tried this, but it is used to improve image resolution.
I want the axis labels to be nicely spaced as in the window. Thanks
Ok, So I found the solution myself and posting it here for anyone who might face similar problem. I changed the figure size before saving and following code does the trick-
import matplotlib.pyplot as plt
fig =plt.gcf()
fig.set_size_inches(20, 11,dpi=100)
plt.savefig('img.png')
Related
I plot figures a lot during my python (through Spyder env.) usage. However, when I try to use plt.savefig('figure.png'), the saved figure has a different size from the inline figure plotted on Spyder.
For ex., when I use this command:
plt.savefig('fig1.png')
The saved figure looks like this:
Note that there's something weird with the saved figure, e.g.: the title is cropped, the size is not proportional.
However, the following is the inline figure:
I tried to modify the size through matplotlib.pyplot documentation but couldn't find such setting. Does anyone know how to save the figure with the exact setting as the inline plot?
The inline figure size plotted in Spyder (or any other IDE or editor) depends on how the editor handles showing figures.
If you want to have an exact size as output of your code, use figsize before plotting code. (It uses inches)
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
# Code to produce figure
You can also determine DPI when creating figure or saving.
plt.figure(figsize=(10, 10), dpi=300)
# or
plt.savefig(file_path, dpi=300)
I've a huge list of Coordinates(x,y) of people walking in the streets and I like to design a heatmap using those Coordinates(x,y), i.e., it should look something like this. I want much hotter spot for multiple coordinates in a single spot, hotter means red colored spot. I've tried this but ended up getting a heatmap like this not the one I expected nor the site mentioned in the beginning(the wikipedia heatmap). This is the code I tried,
import cv2
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
x,y = [230,150,200],[200,100,150]
plt.imshow(img)
ax = sns.kdeplot(x,y, cmap = "Blues", shade= True, shade_lowest=False)
ax.set_frame_on(False)
plt.axis('off')
plt.savefig('heatmap_pic.png')
This code also made the result image size smaller the actual image was much bigger. I'm new to OpenCV and Matplotlib someone please help me with this, to plot a Heatmap(like this) in a image using a bunch of Coordinates.
You need to take image, create the heatmap, and superimpose it.
Check this or this.
I am trying to plot a two dimensional numpy matrix (say, Kappa) using pcolor .
The skeleton of the code is this:
from pylab import pcolor, show, colorbar, xticks, yticks
import numpy as np
plt.figure(1)
pcolor(np.transpose(Kappa))
plt.colorbar()
plt.tight_layout()
plt.show()
At the top of the colorbar, I see a weird way in which the highest scale of the colorbar is written. I have attached a picture of the the said output (and I have highlighted the troublesome part by putting it inside a red box.)
Following suggestions, I have uploaded the matrix to my Google Drive. You can use this link to access the matrix.
I am wondering if anyone has faced similar issues with colorbars in pylab? I will appreciate any help.
Is there a possibility to scale the plot size of matplotlib plots in jupyter notebooks? You could increase the plot size by changing the default values of figure.figsize, but this does not affect parameters like fontsize, linewidth, markersize etc. What I need is a plot where all the parameters are scaled accordingly.
P.S.: To display plots in jupyter notebooks I use %matplotlib inline, see screenshot below.
Edit
For completeness, here is a code snippet doing exactly what I needed:
def scale_plot_size(factor=1.5):
import matplotlib as mpl
default_dpi = mpl.rcParamsDefault['figure.dpi']
mpl.rcParams['figure.dpi'] = default_dpi*factor
You don't want to change the figure size. You want to change the dpi (dots per inch).
Also see Relationship between dpi and figure size.
import matplotlib.pyplot as plt
%matplotlib inline
def plot(dpi):
fig, ax=plt.subplots(dpi=dpi)
ax.plot([2,4,1,5], label="Label")
ax.legend()
for i in range(1,4):
plot(i*72)
To keep it short: Is there a way to export plots created with methods like
pcolorfast which basically draw pixels as "real" vector graphics?
I tried to do just that using savefig and saving to a PDF but what would happen is that the plot was actually a vector graphic but the parts drawn by pcolorfast(so basically, what is inside the axes) something like a bitmap. - I checked this using Inkscape.
This resulted in really low resolution plots even though the arrays drawn with pcolorfast where about 3000x4000. I achieved higher resolution by increasing the dpi when exporting, but I'd really appreciate a conversion to a real vector graphic.
Edit: I updated my original code by the piece of code below that should serve to illustrate what exactly I am doing. I tried to involucrate the rasterized tip, but it has had no effect. I still end up with a supersmall PDF-file where the plots are acually raster images (png). I am going to provide you with the data I used and the resulting PDF.
http://www.megafileupload.com/k5ku/test_array1.txt
http://www.megafileupload.com/k5kv/test_array2.txt
http://www.megafileupload.com/k5kw/test.pdf
import numpy as np
import matplotlib.pyplot as plt
arr1= np.loadtxt("test_array1.txt")
arr2= np.loadtxt("test_array2.txt")
fig, (ax1, ax2)=plt.subplots(1, 2)
ax1.set_rasterized(False)
ax1.pcolorfast(arr1)
ax2.pcolorfast(arr2, rasterized=False)
plt.show()
fig.set_rasterized(False)
fig.savefig("test.pdf")