Matplotlib fonts in Enthought Canopy

前端 未结 3 1945
盖世英雄少女心
盖世英雄少女心 2020-12-28 08:54

I am using the matplotlib library inside Canopy, and the specific function is xkcd(). This function uses a specific font to plot charts. The font is Comic Sans MS, which if

3条回答
  •  醉话见心
    2020-12-28 09:30

    After a lot of research, and not finding anybody who could help me with my question, I was able to answer my own question. This is what I did:

    First, I found exactly where all the fonts are in within matplotlib in the virtual environment of Enthought Canopy:

    luis@luis-VirtualBox:~$ find -iname '*.ttf'
    

    A long list is generated, with results similar to this:

    ./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/Vera.ttf
    ./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/VeraMoBI.ttf
    ./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/STIXGeneral.ttf
    ./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/STIXNonUniBol.ttf
    ./Canopy/appdata/canopy-1.1.0.1371.rh5-x86_64/lib/python2.7/site-packages/canopy/resources/fonts/Inconsolata.ttf
    

    I could not see the 'Humor-Sans-1.0.ttf' file/font anywhere, so I manually downloaded and copied it to the directory:

    ./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/

    Still, the chart was defaulting to another font:

    Font family ['Humor Sans', 'Comic Sans MS'] not found. Falling back to Bitstream Vera Sans (prop.get_family(), self.defaultFamily[fontext]))
    

    Then I noticed that the font I had downloaded was 'Humor-Sans-1.0.ttf' and the error messages was referring to 'Humor Sans' and 'Comic Sans' (without the 1.0 appendix). So I made two copies of the same file, inside the same directory and called them 'Humor-Sans.ttf' and 'Comic-Sans.ttf' respectively.

    Next, I found where the matplotlib fontCache list resides within my virtual environment:

    luis@luis-VirtualBox:~$ find -iname 'fontList.cache'
    ./.cache/matplotlib/fontList.cache
    

    Then removed the cache:

    luis@luis-VirtualBox:~$ rm ./.cache/matplotlib/fontList.cache
    

    After that, I opened my Canopy Editor, opened an iPython notebook, wrote some code, plotted some graphs, and presto, my fonts were right!

    final output

    Not the most elegant solution, but it worked for me.

提交回复
热议问题