When I create a matplotlib chart in my jupyter notebook in vscode, it gives the whole cell a white background. I've tried putting
"import matplotlib.pyplot as plt",
"plt.style.use('dark_background')"
in my settings file to run as startup commands and that makes it gives the plot a black background, but it still doesn't match my vscode theme. Does anyone know how to make them match seamlessly? transparent chart background mabye?
The white background is actually on purpose so that it matches what Jupyter would show. It's from reading the 'needs_background' element of the matplotlib output.
The background unfortunately will always be either white or black based on the 'needs_background' element. (As you found with your style of 'dark_background')
The code for that is here:
https://github.com/microsoft/vscode-notebook-renderers/blob/91210f63be0fcca92d7ea9125b7e9445a423b0a4/src/client/render.tsx#L70
You can enter a request for the jupyter extension to change that color here:
https://github.com/microsoft/vscode-jupyter/issues
You can plot your chart in a PyQt5 window.
I am not sure if this solves your problem, but it's a way to make the chart transparent. I hope it's helpful:
You need to import PyQt5 (pip3 install PyQt5).
from PyQt5 import QtCore
The code to create the transparent window is inserted after defining your figure:
window = plt.gcf().canvas.manager.window # used to get your figure
window.setAttribute(QtCore.Qt.WA_NoSystemBackground, True)
window.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
window.setStyleSheet("background:transparent")
Finally show the window with plot.show():
plt.show()
Related
For my plots made using matplotlib I'm trying to add zoom and save options in my GUI. I got to know about Jupyter notebook but I don't know if I can make a GUI in that. Please suggest a way to add these options for my plots in the GUI.
If I use a dark theme in Visual Studio Code and in Matplotlib, the figure can be configured to have dark background, but the background of the widget / cell is still white.
%matplotlib widget
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('dark_background')
figure, axis = plt.subplots()
axis.plot(np.arange(100))
This also happens if I use the option of the Jupyter extension to use the Visual Studio Code theme in the notebook (actually, then it's even worse because the axis labels are not visible).
Note, that I need to use %matplotlib widget as a renderer, and cannot use %matplotlib inline. For inline, I found a configuration that works.
In the browser, it works if I select the Jupyter dark theme, so I guess, I would kind of select a different "theme" in the Jupyter renderer extension, if something like this is possible (?)
Is there a way to configure the Jupyter extension / Visual Studio Code / ... to have a real "dark mode for Matplotlib"?
I have found a workaround!
(Make sure Visual Studio Code is closed.)
Open file explorer, go to:
C:\Users\<your_username_here>\.vscode\extensions\ms-toolsai.jupyter-<(THE VERSION NUMBER MAY VARY)2022.4.1021342353>\out\webviews\webview-side\ipywidgetsRenderer
Open the ipywidgetsRenderer.js file (using Notepad++ or Notepad or even Visual Studio Code) before letting any Jupyter stuff run
Wherever you see cell-output-ipywidget-background followed by background: white, replace white with black.
Now the problem is solved. Enjoy :)
Plot on VS Code jupyter notebook takes all the width on cell output. Before June update this wasn't a problem. Problem is on dark theme it creates too much unnecessary white space. Does anyone know the reason?
you can "fix" it for your current notebook session by resetting matplotlib option back to default .
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
If you check on the vs code notebook docs, you can see the same white space appears in their screenshots.
I am using matplotlib version 3.0.3 in Eclipse Oxygen 3 on Windows 10.
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=4, ncols=4) # Figure is shown!?
plt.show() # expected to see figure only here
It seems that whenever a plt method gets called matplotlib already opens the UI.
That is useful while debugging but I usually want to just savefig and not stop to look at the generated image ...
This used to work fine for me on my previous Window 7 laptop (not sure what matplotlib version).
Has something changed in the default behavior which I cannot find documented?
Stepping into the matplotlib code I've discovered that interactive mode was set -- probably by default as I don't have a matplotlibrc file anywhere.
I added matplotlib.interactive(False) at the beginning of main() to solve this.
Apparently I still get the interactive mode when debugging even with the above line, so this is good enough for me (for now).
Any help with the issue is appreciated. As the following photo shows, I am trying to get an horizontal bar chart and always get instead. I don't know what to do with it.
Reposting as an answer:
If you run the %matplotlib magic in IPython before creating plots, the plots will appear automatically, and won't block the prompt.
We're working with matplotlib so that this won't be necessary in matplotlib 2.0.