I'd like the background of my matplotlib plots to be transparent in my IPython notebook. This may sound silly because the notebook itself defaults to a white background but:
1) I use a solarized background and
2) more importantly, I want them to be transparent for when I embed the notebook directly into my blog via nbconvert.
It's easy enough to use something like savefig('file', transparent=True) , but I'm not saving the figures, I am displaying them inline (by calling IPython with ipython notebook --matplotlib inline.
I've been playing around with the IPython notebook configuration file, especially with c.InlineBackend.rc. For example, I upgraded to the dev version of matplotlib to get access to its new savefig.transparent rcParam, and tried configuring that with c.InlineBackend.rc = {'savefig.transparent': True}, but as expected it only affects plots saved with savefig.
Note that I am using the recent IPython 2.0 release. This must be possible somehow, right? Any light that you can shed would be appreciated.
Just to follow up, the issue opened on Github by tillsten has been patched so something like this:
rcParams['figure.facecolor'] = (0,0,0,0)
should work now after you update IPython. Three cheers for open source.
The inline plots are html objects (<img>) with class ui-resizable. So you can change their default behavior by customizing the CSS for your notebooks:
locate your settings for notebooks: in a terminal, type
ipython locate
in the indicated directory, go to subdir profile_default\static\custom (or any profile you want to use instead)
edit or create a CSS file named custom.css
put this in it:
img.ui-resizable
{
opacity:0.4;
}
Close your notebooks, kill IPython and restart it (so that it recreates the served files).
It should work with exported notebooks, as long as you export them as html and you change the css there too.
It's not exactly what you want, but it does the job.
Related
I'm using tensorflow to open some .png images and every image it opens, an annoying message is printed.
def open_img(path):
img = tf.io.read_file(path)
img = tf.io.decode_png(img)
return tf.image.resize(img, [IMG_HEIGHT, IMG_WIDTH])
Every time i try to open an image it says "Cleanup called...", even while training:
(This code is running on Kaggle)
tensorflow version: 2.6.3
How can i solve this annoying thing please?
Updating my TensorFlow installation to version 2.8 fixed the issue for me.
If you're running the code on Kaggle, upgrading Tensorflow to 2.8 will break CuDNN dependencies for GPU. I found that downgrading Tensorflow to 2.4.1 one will remove the debugging message while being able to work with GPU.
I believe, these "Cleanup called..." lines might be generated by some C++ code running under the hood of TF, as I was not able to redirect them to a file using redirect_stdout(). Technically, it would be best to intercept those lines and just not output them or dump them to smth like /dev/null. E.g., here, Eli Bendersky showcases how to redirect C-level output, but in my case it didn't work out with Python 3.7. Perhaps, Eli's code needs adjustment for a Python version newer than 3.4.
Concerning the given answers with upgrading/downgrading, this likely won't work with committed Kaggle notebooks as, I believe, there is no way to restart the kernel once the notebook is committed.
For committed Kaggle notebooks, there is the following workaround w/o upgrading/downgrading anything: if you need to view the output of a cell that produces "Cleanup called…" later, log it to a file or both to a file and the console (here's a code snippet for doing so), and invoke the following code at the end of the cell:
from IPython.display import clear_output
clear_output()
This will clear the output of that cell so that, once you open the "Notebook" tab of the committed notebook after it's been finished, it won't be littered with "Cleanup called…" lines and, as such, will open swiftly. Simple logging to a file (like in the above linked snipped) will not capture "Cleanup called…" lines, so one will be able to view the entire log of that cell in that log file in the "Data" tab of the committed notebook. The "Logs" tab, sadly, will still be cluttered with "Cleanup called…" lines.
I am using Windows 10 with Anaconda and Spyder 4. When using matplotlib, I would like to use the font Proxima Nova and render with LaTeX.
If in my matplotlibrc file I specify
font.family : Proxima Nova
then the figure renders with the font Proxima Nova. This means that the font is installed on my system (as it is) and matplotlib can use it. However, if in the matplotlibrc file I also specify
text.usetex: True
then, even though I have specified Proxima Nova as the font, the figure renders in the default LaTeX font, which I guess is Computer Modern.
I have tried
matplotlib.font_manager._rebuild()
In the source code file and also have tried specifying the fonts in the source code file and not in the matplotlibrc file. However I always get the same result. I have also followed all the advice on this help page, including making sure that latex, dvipng and ghostscript are all the PATH variable. However nothing seems to work.
I would like to note that I can use Proxima Nova separately when compiling Latex documents, so that should not be an issue either.
How can I get matplotlib to be able to use a non-default font and render with LateX at the same time?
After some further investigation, I was able to get to use Proxima Nova with Latex, although there are still some outstanding issues.
The main issue is that if the font Proxima Nova is used with Latex, one needs to use Lualatex and not plain Latex. Here is the Matplotlib instruction on using matplotlib with Lualatex.
The key to getting things to work was this post.
At the very beginning of my .py file, I have the following code:
import matplotlib as mpl
mpl.use("pgf")
mpl.rcParams.update({
'font.family': 'sans-serif',
'text.usetex': True,
'pgf.rcfonts': False,
'pgf.texsystem': 'lualatex',
'pgf.preamble': r'\usepackage{fontspec} \setmainfont{Proxima Nova}',
})
The code above should be placed at the very top of the code, above any other imports.
The problem, however is that this solution works only after performing the following steps:
Delete the .matplotlib/tex.cache folder and restart spyder
Replace 'font.family': 'sans-serif' and \setmainfont{Proxima Nova} with 'font.family': 'serif' and \setmainfont{Times New Roman} respectively. Run python once.
Revert back to 'font.family': 'sans-serif' and
\setmainfont{Proxima Nova} and run python again.
The output with the correct font is produced.
Unless the above 4 steps are performed, the output is compiled with the default DejaVu Sans font and not with Proxima Nova. I am not sure why...
After getting help on the matplotlib github forum, I was pointed to the following solution:
mpl.rcParams.update({
'font.family': 'sans-serif',
'text.usetex': True,
'pgf.rcfonts': False,
'pgf.texsystem': 'lualatex',
'pgf.preamble': r'\usepackage{fontspec} \setsansfont{Proxima Nova}',
})
In other words you need to use setsansfont in stead of setmainfont. You can see the matplotlib forum page here.
I got a super graph that I would like to export as an html file to be showed on an website but I don't know how to figure that. Have you got an idea ?
You can use mpld3. This is a great tutorial. The mpld3 library's main functionality is to take an existing matplotlib visualization and transform it into some HTML code that you can embed on your website.
Use fig_to_html file, which accepts a matplotlib figure object as its sole argument and returns HTML.
To use the fig_to_html method , simply add the following code to the end of our Python script:
html_str = mpld3.fig_to_html(fig)
Html_file= open("index.html","w")
Html_file.write(html_str)
Html_file.close()
Links where I found solutions but don't understand:
no display name and no $DISPLAY environment variable in Jupyter Notebook
ipython notebook - $DISPLAY variable error
ipython notebook on linux VM running matplotlib interactive with nbagg
1. Summarize the problem
Want to study python with my son, we have book called Hello World Computer Programming for kids and other beginners. In chapter 6 we move toward create message boxes, button boxes, and choice boxes which will need to build our private app together.
2. Describe what you’ve tried
I looked at previous questions and learned that in order to import the module, I would have to first move the easygui.py file into my google drive. Then I learn to use [!pip install easygui] to install the module, which also worked. I was able to import the module without any problem but once the system reads the line of code to create the msg box it crashes and returns the message.
TclError: no display name and no $DISPLAY environment variable
I am completely clueless. I saw two similar situations but I'm new and I'm not sure how they solved this problem, they only listed that they used:
Your problem might be solved if you run before:
%matplotlib inline
My question is how and where did they do this because when i place this code before my only two lines a code i still get an error.
3. When appropriate, show some code
!pip install easygui
import easygui
easygui.msgbox("Hello There!")
and also with
%matplotlib inline
import easygui
easygui.msgbox("Hello There!")
easygui runs on local machine. So it's working when you write your lines in an editor, for example Python IDE or Sublime Text etc.
google-colab is an online platform, which is different from local (machine) environment. That's why on colab, it doesn't perform the pop-up.
I have searched this for long time, similar questions have been asked but none solved my problem.
Usually I run my py files by ctrl+shift+r, and what I expect is to show the graphs in side the session, like the way it works for Jupyter notebook. But instead it either omits the graph, or show it separately and if I have several graphs they will all pop once and lock the session.
Something like %matplotlib inline but for default python files.
So far I have tried to add the env var for Pycharm, and tried to play around with plt.isinterative, nothing worked :(
Thank you.