I am using matplotlib to make some graphs and unfortunately I cannot export them without the white background.
In other words, when I export a plot like this and position it on top of another image, the white background hides what is behind it rather than allowing it to show through. How can I export plots with a transparent background instead?
Use the matplotlib savefig function with the keyword argument transparent=True to save the image as a png file.
In [30]: x = np.linspace(0,6,31)
In [31]: y = np.exp(-0.5*x) * np.sin(x)
In [32]: plot(x, y, 'bo-')
Out[32]: [<matplotlib.lines.Line2D at 0x3f29750>]
In [33]: savefig('demo.png', transparent=True)
Result:
Of course, that plot doesn't demonstrate the transparency. Here's a screenshot of the PNG file displayed using the ImageMagick display command. The checkerboard pattern is the background that is visible through the transparent parts of the PNG file.
Png files can handle transparency.
So you could use this question Save plot to image file instead of displaying it using Matplotlib so as to save you graph as a png file.
And if you want to turn all white pixel transparent, there's this other question : Using PIL to make all white pixels transparent?
If you want to turn an entire area to transparent, then there's this question: And then use the PIL library like in this question Python PIL: how to make area transparent in PNG? so as to make your graph transparent.
As a reminder, the plt.savefig() should be written before the plt.show(), otherwise, a transparent image will be created (without the actual plot).
For high-quality images:
plt.savefig('filename.png', format='png', dpi='600', transparent=True)
plt.show()
Related
The following code gives the figure like the image below.
plt.subplot(1,1,1)
ax = sns.barplot(x=contr, y=X.columns)
ax.bar_label(ax.containers[0])
plt.title('Contribution')
plt.savefig('result_image.png')
plt.show()
What I can see in the jupyter notebook
However, the saved image ('result_image.png') has no titles or axis, but literally just figure box itself like the picture below
the real image file is like this
What I wanted is the plt image with title and axis.
=====
EDIT
The real problem was not the crop of the figure,
but the figure background being transparent.
(I didn't notice because the background of my photo application was dark)
I solved the problem with the code below.
plt.savefig('result_image.png', facecolor='white')
The real problem was not the crop of the figure, but the figure background being transparent.
(I didn't notice because the background of my photo application was dark)
I solved the problem with the codes below.
plt.savefig('result_image.png', facecolor='white')
or
plt.savefig('result_image.jpg')
When making a subplot we use facecolor argument to set the color of the background. For example
import matplotlib.pyplot as plt
fig, ax = plt.subplots(facecolor="black")
ax.set_facecolor("black")
This produces the following output:
Now the above output is a plain black color. What I want is to add an effect to this plain black color so that it will look like this:
If you observe closely there is a bit of white color scattered over the black colored background. How to produce this effect in matplotlib?
I believe that only solid colours can be set. You could create the desired effect in a different program and save it as an image. Then, set the background of the plot to that image as in this SO answer.
I'm trying to create a pdf using Python's reportlab module.
I generated a png with matplotlib and saved it in the pdf file using report labs canvas.drawImage method.
My problem is that the generated png file is very fuzzy. I specified the size in inches with plt.figure(figsize=(20,10)) and saved the picture with the plt.savefig method.
This works out perfectly (except the fuzzy quality of the picture).
But when I increase the dpi within the savefig method the size of the picture increases.
Is there any way to improve the dpi without changing the picture size.
Or is there a way to resize it to the predefined values?
Thanks!
f = df.plot()
fig = f.get_figure()
fig.set_size_inches((2,2))
fig.savefig('C:/temp/foo.png', bbox_inches='tight', dpi=1500)
The left image was drawn using MATLAB and color changing is smooth, beautiful! The right image was drawn using Python matplotlib hist2D and it is sharp.
Is there any method to make it more beautiful like MATLAB's?
I would like to make the background of my imshow plots transparent. I know I can do this using the color to alpha option in GIMP. However, using GIMP in this case is not an option as I still need to perform actions after the background of imshow has been made transparent.
Is there a way to remove the background of imshow plots similar to GIMP's color to alpha in python without saving the figure first?