hello dear denzjos
many many thanks for the quick reply - very glad to hear from you
i updated the google-colab-settings - now you have access and can run the plotly-code
https://colab.research.google.com/drive/...sp=sharing
and it should output it with a transparent background
greetings
btw.
the code is taken form here:
https://plotly.com/python/line-and-scatter/
btw: does this help
siome changes were made:
and some additonal Changes were made:
paper_bgcolor='rgba(0,0,0,0)': Makes the entire paper background (the area surrounding the plot) transparent. rgba(0,0,0,0) stands for red, green, and blue with an alpha value (transparency) of 0.
plot_bgcolor='rgba(0,0,0,0)': Makes the background of the coordinate system itself transparent.
template='plotly_white': This sets a light base style, though it is overridden by the transparency settings. It ensures that axis labels and legends remain clearly legible.
Adjusting grid lines: To ensure data points remain visible against a transparent background, the grid lines are kept a subtle gray ('lightgrey'). You can set showgrid=False if you wish to remove them entirely.
you should be able to run it here: https://colab.research.google.com/drive/...sp=sharing
some findings: Even if the screenshot turns black when clicked. Didn't know exporting a png could be so complicated and messy.
Guess that for Matplotlib, export directly with alpha from the figure, not by screenshotting or copying the preview.
what about the usage of this pattern:
look forward to hear from you
many many thanks for the quick reply - very glad to hear from you
i updated the google-colab-settings - now you have access and can run the plotly-code
https://colab.research.google.com/drive/...sp=sharing
and it should output it with a transparent background
greetings
btw.
the code is taken form here:
https://plotly.com/python/line-and-scatter/
btw: does this help
Code:
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color='petal_length')
fig.show()siome changes were made:
Code:
# Plotly-Bibliothek installieren (falls nicht vorhanden)
!pip install plotly
import plotly.express as px
import plotly.graph_objects as go # Für erweiterte Layout-Optionen
# Daten laden
df = px.data.iris()
# Figur erstellen
fig = px.scatter(df, x="sepal_width", y="sepal_length", color='petal_length')
# --- Hier wird der Hintergrund transparent gemacht ---
fig.update_layout(
template='plotly_white', # Helle Basis, aber wir überschreiben alles
paper_bgcolor='rgba(0,0,0,0)', # Transparenter Papier-Hintergrund
plot_bgcolor='rgba(0,0,0,0)', # Transparenter Plot-Bereich
margin=dict(l=40, r=40, t=40, b=40) # Etwas Rand für die Achsenbeschriftung
)
# Achsenlinien und Gitterlinien optional anpassen (für bessere Sichtbarkeit auf transparentem Grund)
fig.update_xaxes(
showgrid=True, # Gitterlinien anzeigen (optional)
gridcolor='lightgrey', # Farbe der Gitterlinien
zeroline=True,
zerolinecolor='lightgrey'
)
fig.update_yaxes(
showgrid=True,
gridcolor='lightgrey',
zeroline=True,
zerolinecolor='lightgrey'
)
# Figur an
zeigen
fig.show()and some additonal Changes were made:
paper_bgcolor='rgba(0,0,0,0)': Makes the entire paper background (the area surrounding the plot) transparent. rgba(0,0,0,0) stands for red, green, and blue with an alpha value (transparency) of 0.
plot_bgcolor='rgba(0,0,0,0)': Makes the background of the coordinate system itself transparent.
template='plotly_white': This sets a light base style, though it is overridden by the transparency settings. It ensures that axis labels and legends remain clearly legible.
Adjusting grid lines: To ensure data points remain visible against a transparent background, the grid lines are kept a subtle gray ('lightgrey'). You can set showgrid=False if you wish to remove them entirely.
you should be able to run it here: https://colab.research.google.com/drive/...sp=sharing
some findings: Even if the screenshot turns black when clicked. Didn't know exporting a png could be so complicated and messy.
Guess that for Matplotlib, export directly with alpha from the figure, not by screenshotting or copying the preview.
what about the usage of this pattern:
Code:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10, 5), dpi=150)
# Plot your data
sc = ax.scatter(x, y, c=petal_length, cmap="plasma", s=20)
# Make BOTH figure canvas and axes rectangle transparent
fig.patch.set_alpha(0)
ax.patch.set_alpha(0)
# Optional: if you have a colorbar
cbar = fig.colorbar(sc, ax=ax)
cbar.ax.patch.set_alpha(0)
# Save directly to PNG with alpha
fig.savefig(
"plot_transparent.png",
format="png",
dpi=300,
transparent=True,
facecolor="none",
edgecolor="none",
bbox_inches="tight",
pad_inches=0.05
)
Important details
Do not do this
plt.savefig("plot.png")
if your figure or axes already have a dark/white facecolor.
Also avoid:
fig.savefig("plot.png", facecolor=fig.get_facecolor())
That can bake the current background into the PNG.
Do this instead
fig.savefig(
"plot.png",
transparent=True,
facecolor="none",
edgecolor="none"
)
If you use a dark plotting style
For example:
plt.style.use("dark_background")
it may set the axis background to dark. Clear it before saving:
fig.patch.set_facecolor("none")
ax.set_facecolor("none")
fig.savefig(
"plot.png",
transparent=True,
facecolor="none",
edgecolor="none",
dpi=300,
bbox_inches="tight"
)
If you want transparent background but visible grid/text
You still need to set the plot element colors manually:
ax.tick_params(colors="white")
ax.xaxis.label.set_color("white")
ax.yaxis.label.set_color("white")
ax.title.set_color("white")
for spine in ax.spines.values():
spine.set_color("white")
ax.grid(color="white", alpha=0.45)
That makes the background transparent, while the grid, axes, labels, and points remain visible.
Verification
After export:
from PIL import Image
import numpy as np
img = Image.open("plot_transparent.png").convert("RGBA")
alpha = np.array(img)[:, :, 3]
print(alpha.min(), alpha.max())
Correct transparent PNG:
0 255
Flattened/non-transparent PNG:
255 255
Main rule: save from Matplotlib with transparent=True, facecolor="none", and transparent fig/ax patches. Do not export through viewer/editor screenshlook forward to hear from you

