GIMP's color to alpha in python - python

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?

Related

Hide Matplotlib grid color

I'd like to hide the 3d grid background color and just display gridlines and axis labels, as it's currently the default gray color
like this (Not allowed to embed images since this is a new account). Thanks.
For your case, you can set the background color to black.
ax.set_facecolor("black")
https://matplotlib.org/3.5.0/gallery/color/color_demo.html#sphx-glr-gallery-color-color-demo-py

how to use Python hist2D to figure more beautiful?

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?

Why/How to use cmap argument in matplotlib imshow() in isolating color panes?

Whenever I plot this image I get issues with the whites turning yellow. I know it's due to the default color mapping viridis, which matplotlib uses. When I switch to cmap='gray', it ends up showing the right red color pane.
Can anyone explain why this is happening? What color map should be used generally for pictures like this? How is the picture able to show the right colors when I do the default imshow(img)? What changes when I isolate the a single color pane? And when isolating red green or blue color panes in images, what is the preferred cmap and why?
This is the output for the red color pane
The regular image plots correctly according to RGB color mode:
The logic is rather simple, possibly the following chart helps more than any explanation.

Matplotlib color scales that are suitable for printing

Is there an existing color map that can be easily applied to make a line plot visible on a grayscale medium (e.g. a paper printed in black and white) by modifying the way the line is drawn (e.g. black full line, black dashed line, gray full line, etc.)?
The gray color scale is a good start but it doesn't cut it: when I have more than 4 series on the same plot it's hard to distinguish once printed, and also it colors one series as white (making it invisible unless I change the background to something else).
You can vary the linestyles as shown here and use different markers as shown here.
Finally, I would suggest you look at cyclers to loop over different colors, linestyles and markers.

How to export plots from matplotlib with transparent background?

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()

Categories