Matplotlib transparent line plots

I am plotting two similar trajectories in matplotlib and I'd like to plot each of the lines with partial transparency so that the red (plotted second) doesn't obscure the blue.

alt text

EDIT: Here's the image with transparent lines.

alt text


Asked by: Elise293 | Posted: 30-11-2021






Answer 1

Plain and simple:

plt.plot(x, y, 'r-', alpha=0.7)

(I know I add nothing new, but the straightforward answer should be visible).

Answered by: Freddie799 | Posted: 01-01-2022



Answer 2

After I plotted all the lines, I was able to set the transparency of all of them as follows:

for l in fig_field.gca().lines:
    l.set_alpha(.7)

EDIT: please see Joe's answer in the comments.

Answered by: Chester627 | Posted: 01-01-2022



Answer 3

It really depends on what functions you're using to plot the lines, but try see if the on you're using takes an alpha value and set it to something like 0.5. If that doesn't work, try get the line objects and set their alpha values directly.

Answered by: Brianna199 | Posted: 01-01-2022



Similar questions

python - Matplotlib / PyQT4: transparent figure

I am using Python with PyQt4 and I want to embed a matplotlib figure in the GUI. What's important to me, is that the background of the Figure matches the background color of the GUI. (no grey background around the actual plot) My partial solution would be to make the Figure transparent with the following code: ... fig.patch.set_alpha(0.5) ... This works fi...


python - How to fade color to transparent using hexbin and matplotlib?

I am creating a "heatmap" using hexbin, and want this heatmap to be placed on top of an image. However, I would like to have the coloring of the plot fade to transparent as the frequency does (i.e. as the color fades to white, it disappears). I have tried changing the alpha value but that does not produce the desired effect. My code is: n = 100000 x = np.random.standard_normal(n) img = imread("socc...


python - How to make axes transparent in matplotlib?

So I have the following example that plots a figure and an inset: import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax1 = fig.add_subplot(1, 1, 1) x = np.arange(100).reshape((10, 10)) ax1.imshow(x) ax2 = fig.add_axes([0.5, 0.5, 0.3, 0.3]) t = np.arange(0, 1, 0.01) s = np.sin(t) ax2.plot(t, s, linewidth=2) This produces the following figure:


python - Matplotlib Make Center Circle Transparent

I am plotting a pie chart making background in the png image looks transparent. How can I make the center circle also looks transparent instead of the white color? import matplotlib.pyplot as plt # Pie chart, where the slices will be ordered and plotted count...


python - Matplotlib / Imshow / transparent layer everywhere but certain points

I try to plot a layer on an imshow. This layer is based on a matrix where every values are equals to 0 but some specific pixels are equals to 1. If I use the alpha parameter, I'm loosing mycolor as well... How can I have 0% transparency where I have a "1" value in my layer and 100% transparency where it is equals to 0 ? Here is an example: import matplotlib.pyplot as plt import numpy as np ...


python - How to define a No Data / transparent value in a Matplotlib TIFF plot?

I have a numpy array as a result from an image transformation. In the method I wrote for the image transformation the No Data value is 0 (I can change this if needed). I want to display this result with Matplotlib but offcourse the color scale stretches to the 0. Is there a way to ignore the 0 value in the visualization, make it transparent, define it as a No Data value etc. so the colorscale is stretched to the relevant v...


python - How to make title and legend area in a matplotlib figure not transparent?

Edit: The issue I have with my example further below I also can recreate with this: import matplotlib.patches as mpatches import matplotlib.pyplot as plt fig, ax = plt.subplots() plt.ylabel('Y-label'); plt.figtext(0.4, 1.1,'Title:',fontsize=40, color='black',ha ='left', backgroundcolor='white') patch_example=mpatches.Patch(color='green', label='label') plt.legend(fontsize=16, loc='upper center',bbox_to_anch...


python - White to Transparent Layer in matplotlib figure (SVG)

I would like to add a layer with a gradient (from white to transparent) on top of my matplotlib image and be able to save the result as an SVG eventually (I have some more shapes on my actual image than sketched below). It is unclear to me, if this is possible with matplotlib + svg. Does anybody know or has a solution for this? Here is some example code: plt.figure(figsize=[6, 6]) x = np.arange...


python - matplotlib bar chart just appear transparent white

I have the following code and it was working fine a month ago and suddenly i get a strange looking white transparent bar chart.. fig, ax1 = plt.subplots(figsize=(16,6)) ax1.bar(loanapp.date, loanapp.total.rolling(14).mean(), width = 4.4, color = 'tab:blue', label="Rejected applicants")


Python matplotlib -> 3D bar plot -> adjusting tick label position, transparent bars

I am trying to create a 3D bar histogram in Python using bar3d() in Matplotlib. I have got to the point where I can display my histogram on the screen after passing it some data, but I am stuck on the following: Displaying axes labels correctly (currently misses out final (or initial?) tick labels) Either making the ticks on each axis (e.g. that for 'Mon') either point to it's corresponding b...


python - 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 ...


python - Matplotlib / PyQT4: transparent figure

I am using Python with PyQt4 and I want to embed a matplotlib figure in the GUI. What's important to me, is that the background of the Figure matches the background color of the GUI. (no grey background around the actual plot) My partial solution would be to make the Figure transparent with the following code: ... fig.patch.set_alpha(0.5) ... This works fi...


python - Overlay transparent paths in matplotlib?

from pylab import * plot(randn(1000), randn(1000), alpha=0.1) [<matplotlib.lines.Line2D at 0x7f756e65a450>] savefig('test.png') gives this: Where the paths are combined, and then the transparency is applied after. I want something like this:


python - How to fade color to transparent using hexbin and matplotlib?

I am creating a "heatmap" using hexbin, and want this heatmap to be placed on top of an image. However, I would like to have the coloring of the plot fade to transparent as the frequency does (i.e. as the color fades to white, it disappears). I have tried changing the alpha value but that does not produce the desired effect. My code is: n = 100000 x = np.random.standard_normal(n) img = imread("socc...


python - Plot semi transparent contour plot over image file using matplotlib

I'd like to plot a transparent contour plot over an image file in matplotlib/pyplot. Here's what I got so far... I have a 600x600 pixel square image file test.png that looks like so: I would like to plot a contour plot over this im...


python - How to make axes transparent in matplotlib?

So I have the following example that plots a figure and an inset: import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax1 = fig.add_subplot(1, 1, 1) x = np.arange(100).reshape((10, 10)) ax1.imshow(x) ax2 = fig.add_axes([0.5, 0.5, 0.3, 0.3]) t = np.arange(0, 1, 0.01) s = np.sin(t) ax2.plot(t, s, linewidth=2) This produces the following figure:


python - matplotlib, copy and restore transparent region

I'd like to copy-paste 2 transparent figures into a third. Figure 1 (transparent): Figure 2 (transparent):


python - Matplotlib Make Center Circle Transparent

I am plotting a pie chart making background in the png image looks transparent. How can I make the center circle also looks transparent instead of the white color? import matplotlib.pyplot as plt # Pie chart, where the slices will be ordered and plotted count...


python - How to save the plot with the labels and ticks area transparent but not the main plot area in matplotlib?

I'm trying to save a plot with the labels area (the area where you print the xlabel, ylabel and title) transparent and the main plot (the area where you plot the data) not transparent. I'm using the transparent command in savefig: plt.sav...


python - How to make a surface with a transparent background in pygame

Can someone give me some example code that creates a surface with a transparent background in pygame?


python - How to use PIL to make all white pixels transparent?

I'm trying to make all white pixels transparent using the Python Image Library. (I'm a C hacker trying to learn python so be gentle) I've got the conversion working (at least the pixel values look correct) but I can't figure out how to convert the list into a buffer to re-create the image. Here's the code img = Image.open('img.png') imga = img.convert("RGBA") datas = imga.getdata() newData = list() for i...


pygame - Updating part of a surface in python, or transparent surfaces

I have an application written in python that's basically an etch-a-sketch, you move pixels around with WASD and arrow keys and it leaves a trail. However, I want to add a counter for the amount of pixels on the screen. How do I have the counter update without updating the entire surface and pwning the pixel drawings? Alternatively, can I make a surface that's completely transparent except for the text so you can se...


django - python PIL - background displayed opaque instead of transparent

I want to generate 32x32 sized thumbnails from uploaded images (actually avatars). To prevent a thumbnail from being smaller than that size, I want to create a transparent 32x32 background and paste the thumbnail on it. The code below tries to do so. However, the avatar is displayed on a black and opaque background; I lose transparency information somewhere through the process. Where am I doing wrong?...


python - Transparent FrameBuffer background in OpenGL

I want to use glClear and glClearColor to fill a frame buffer with a colour including alpha transparency. However the framebuffer always renders as opaque when binded to a texture which is rendered to the screen. I want everything which is rendered to the framebuffer to kept their transparency. I just want to change the background. See the following code: def create_texture(surface): surface...


How do I create a Status Icon / System Tray Icon with custom text and transparent background using Python and GTK?

Here is the code that I have so far to define the icon: icon_bg = gtk.gdk.pixbuf_new_from_file('gmail.png') w, h = icon_bg.get_width(), icon_bg.get_height() cmap = gtk.gdk.Colormap(gtk.gdk.visual_get_system(), False) drawable = gtk.gdk.Pixmap(None, w, h, 24) drawable.set_colormap = cmap gc = drawable.new_gc() drawable.draw_pixbuf(gc, icon_bg, 0, 0, 0, 0, w, h) drawn_icon = gtk.gdk.Pixbuf(gtk.gdk.COLORSPAC...


python - How to make cStringIO transparent to another function that expects a real local file

I came up with the following problem: CODE A works right now.. I am saving a png file called chart.png locally, and then I am loading it into the proprietary function (which I do not have access). However, in CODE B, am trying to use cStringIO.StringIO() so that I do not have to write the file "chart.png" to the disk. But I cannot find a way to pass it to the pproprietaryfunction because it is expecting a real fil...


python - PIL: How to make area transparent in PNG?

I've been using PIL to crop Images, now I also want to make certain rectangular areas transparent, say from PIL import Image im = Image.open("sample.png") transparent_area = (50,80,100,200) ...


python - Transparent 3D bar graphs

I would like to generate 3D bar graphs with transparent surfaces so that I can see what is going on behind tall bars. The mplot3d API docs say that keywords are allowed for the bar3d function. I pass all the required parameters but can only output graphs with solid surfaces.


python - Partially transparent scatter plot, but with a solid color bar

In Python, with Matplotlib, how to simply do a scatter plot with transparency (alpha < 1), but with a color bar that represents their color value, but has alpha = 1? Here is what one gets, with from pylab import *; scatter(range(10), arange(0, 100, 10), c=range(10), alpha=0.2); color_bar = colorbar(): How can the color ba...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top