yellowbrick module instalation affects matplotlib fonts avaliable - matplotlib

After instalation and import of yellowbrick module matplotlib can't find some of the fonts for its labels and titles like Arial, Liberation Sans, Bitstream Vera Sans, sans-serif
the following warning appears:
´´´
findfont: Generic family 'sans-serif' not found because none of the following families were found: Arial, Liberation Sans, Bitstream Vera Sans, sans-serif
´´´
even graph vizualization is affected, looking something like seaborn wolud produce
the first image its normal and represents what it should be the output
Next one is with the module import

I faced the same issue last week. The following steps worked for me.
Uninstalled yellowbrick via pip
Restarted my notebook
Imported font-manager from matplotlib.
import matplotlib.font-manager
I no longer encounter the font not found issue. You can try without step 3 to see if that would work. If not, try again after importing font-manager.

Related

Matplotlib does not find basic fonts

I am trying to use Computer Modern (i.e. the standard LaTeX font) in my pyplot figures, however, I get the error
findfont: Font family ['serif'] not found. Falling back to DejaVu Sans.
All I do is:
from matplotlib import rc
rc('text', usetex=True)
rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
I have also found this thread which dealt with the same issue, however, I cannot install the packages mentioned in the answer there because I am not an admin on the machine I am using. Deleting the matplotlib cache did not help, either. I have located the font, though:
~/.local/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/
which has the subfolders afm and ttf (and another one), and within both of them, I find files like
cmr10.afm
or
cmr10.ttf
so the fonts are actually installed on the computer. The fontlist-v330.json file links to
"fname": "fonts/afm/cmr10.afm"
for Computer Modern. I have tried manually changing this to the ttf file, and I have also tried giving it the full path, but to no avail. At this point, I am at a loss for ideas. How can I solve this issue?
All you need to do should be using the actual name of the font file:
from matplotlib import rc
rc('text', usetex=True)
rc('font', **{'family': 'serif', 'serif': ['cmr10']})

Using matplotlib with latex mode with non-default fonts

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.

Anaconda matplotlib theme / styling / colorcyle

Does anyone know what a default Anaconda install does to change Matplotlib styling?
For example:
import matplotlib.pyplot as pl
pl.figure()
pl.xlabel('X label')
pl.ylabel('Y label')
pl.plot([1,2,3], [4,5,6])
pl.plot([1,2,3], [6,5,4])
pl.show()
The above code creates a blue and a green line on default matplotlib, but blue and orange when run in Anaconda. The axis labels have also changed from a serif (default) to a sans-serif font (Anaconda).
Is this a matplotlib theme? Custom or available from (free) sources? Is it anaconda itself or some package included in it?
Any hints/answers are much appreciated
-- Update --
I found two matplotlibrc files in the anaconda directory, both simply set a backend and had all other lines commented out.
This has nothing to do with anaconda. You most likely have two different versions of matplotlib installed and the one that anaconda uses is the newer one of them.
The default style of matplotlib changed between versions 1.5 and 2.0. See Changes to the default style.
To revert to the classic style (the one used in version 1.5), you may use
plt.style.use("classic")
For an overview of all available styles, see the style_sheets_reference for 2.0.0.
You may create your own stylesheets as detailed in the Customizing article.

Matplotlib seeing new font but not applying it, xkcd mode

I'm trying to get Matplotlib's xkcd style to use the font designed for it, Humor Sans.
I'm getting Comic Sans instead, see pic
Now, the point is about the correct font not getting recognised here, I have removed the cache as per here and as suggested in the same question. Basically what is suggested in this other question.
I have even "forced" Matplotlib to regenerate the font cache as
f = matplotlib.font_manager.FontManager()
and looks like the desired font is there:
f.findfont("Humor Sans")
'/Users/myname/Library/Fonts/Humor-Sans.ttf'
So I don't get why the plot still shows Comic Sans? It has been obtained with code
plt.xkcd()
plt.plot([i for i in range(10)], np.sin([i for i in range(10)]))
plt.title('A sine wave')
plt.show()
I even tried changing backend as per this Q&A, to no avail.
The solution was to also clear the cached font_manager instance, as
rm ~/.matplotlib/fontList.py3k.cache

Transparent inline matplotlibs in IPython

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.