Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set multiple default fonts with matplotlib? #3010

Closed
mwaskom opened this issue Apr 26, 2014 · 13 comments
Closed

How to set multiple default fonts with matplotlib? #3010

mwaskom opened this issue Apr 26, 2014 · 13 comments

Comments

@mwaskom
Copy link

mwaskom commented Apr 26, 2014

I asked this as a SO question that didn't seem to get any responses.

I don't think this is a bug, but it feels like a bug because I don't understand it :). Feel free to close this and answer over there if you think it is just a misunderstanding.

I want to set the default matplotlib fonts through the rc parameters in a way that is portable to computers that might not have the same set of fonts available.

Here is what I thought would work:

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams["font.sans-serif"] = ["Arial", "Liberation Sans", "Bitstream Vera Sans"]
mpl.rcParams["font.family"] = "sans-serif"

ax = plt.subplot()
ax.plot([0, 1, 2], [0, 2, 1])
ax.set_xlabel("the x label")

Because I have Arial installed on my system, I thought that the xlabel would be in Arial. But this approach should mean the default for someone who doesn't have Arial but does have Liberation Sans (i.e. a default Ubuntu system) will be Liberation Sans. And then it should fall back to Bitstream Vera Sans as the default when neither of the two alternate choices are available.

However, this is not what happens. If I do print ax.xaxis.label.get_fontname() it reports Bitstream Vera Sans. Again, I have Arial installed. In other words, I can then do

ax.set_ylabel("the y label", fontdict={"name": "Arial"})
print ax.yaxis.label.get_fontname()

and get Arial.

It feels like I am missing something fundamental about the relationship between the font.family rc parameter and the font.sans-serif parameter when family is "sans serif".

@mdboom
Copy link
Member

mdboom commented Apr 28, 2014

I think the problem you are running into is solved by #2771 -- which has stalled out because we're having trouble getting the test suite to pass on Travis with those changes. We'll need to devote a few cycles to getting that resolved -- but you could try using that patch locally if you want it today.

@mwaskom
Copy link
Author

mwaskom commented May 8, 2014

So I'm a little confused, is this is a bug just on 1.3.1, or on anything <= 1.3.1? I'm trying to figure out the best way to work around this in seaborn.

@mdboom
Copy link
Member

mdboom commented May 8, 2014

I think it was introduced in 1.3.0 and will be fixed in the next release.

@mwaskom
Copy link
Author

mwaskom commented May 8, 2014

Is there an ETA on 1.4? And will there be a 1.3.x bugfix release?

@tacaswell
Copy link
Member

'soon' for 1.4 and probably not on 1.3.2.

@tacaswell
Copy link
Member

@mwaskom Can you confirm that this is fixed for you an master?

@mwaskom
Copy link
Author

mwaskom commented May 17, 2014

I am unable to build matplotlib on my laptop :(

[matplotlib]{master}$ pip install .
Unpacking /Users/mwaskom/Code/matplotlib
  Running setup.py egg_info for package from file:///Users/mwaskom/Code/matplotlib
    ============================================================================
    Edit setup.cfg to change the build options

    BUILDING MATPLOTLIB
                matplotlib: yes [1.4.x]
                    python: yes [2.7.6 |Anaconda 1.8.0 (x86_64)| (default, Jan
                            10 2014, 11:23:15)  [GCC 4.0.1 (Apple Inc. build
                            5493)]]
                  platform: yes [darwin]

    REQUIRED DEPENDENCIES AND EXTENSIONS
                     numpy: yes [version 1.8.1]
                       six: yes [using six version 1.6.1]
                  dateutil: yes [using dateutil version 2.2]
                   tornado: yes [using tornado version 3.2]
                 pyparsing: yes [using pyparsing version 2.0.1]
                     pycxx: yes [Couldn't import.  Using local copy.]
                    libagg: yes [pkg-config information for 'libagg' could not
                            be found. Using local copy.]
                  freetype: no  [The C/C++ header for freetype2 (ft2build.h)
                            could not be found.  You may need to install the
                            development package.]
                       png: yes [pkg-config information for 'libpng' could not
                            be found. Using unknown version.]
                     qhull: yes [pkg-config information for 'qhull' could not be
                            found. Using local copy.]

    OPTIONAL SUBPACKAGES
               sample_data: yes [installing]
                  toolkits: yes [installing]
                     tests: yes [using nose version 1.3.0 / mock is required to
                            run the matplotlib test suite.  pip/easy_install may
                            attempt to install it after matplotlib.]

    OPTIONAL BACKEND EXTENSIONS
                    macosx: yes [installing, darwin]
                    qt4agg: no  [PyQt4 not found]
                   gtk3agg: no  [Requires pygobject to be installed.]
                 gtk3cairo: no  [Requires cairocffi or pycairo to be installed.]
                    gtkagg: no  [Requires pygtk]
                     tkagg: yes [installing, version 81008]
                     wxagg: no  [requires wxPython]
                       gtk: no  [Requires pygtk]
                       agg: yes [installing]
                     cairo: no  [cairocffi or pycairo not found]
                 windowing: no  [Microsoft Windows only]

    OPTIONAL LATEX DEPENDENCIES
                    dvipng: yes [version 1.14]
               ghostscript: no
                     latex: yes [version 3.1415926]
                   pdftops: no

    ============================================================================
                            * The following required packages can not be built:
                            * freetype
    Complete output from command python setup.py egg_info:
    ============================================================================

Edit setup.cfg to change the build options



BUILDING MATPLOTLIB

            matplotlib: yes [1.4.x]

                python: yes [2.7.6 |Anaconda 1.8.0 (x86_64)| (default, Jan

                        10 2014, 11:23:15)  [GCC 4.0.1 (Apple Inc. build

                        5493)]]

              platform: yes [darwin]



REQUIRED DEPENDENCIES AND EXTENSIONS

                 numpy: yes [version 1.8.1]

                   six: yes [using six version 1.6.1]

              dateutil: yes [using dateutil version 2.2]

               tornado: yes [using tornado version 3.2]

             pyparsing: yes [using pyparsing version 2.0.1]

                 pycxx: yes [Couldn't import.  Using local copy.]

                libagg: yes [pkg-config information for 'libagg' could not

                        be found. Using local copy.]

              freetype: no  [The C/C++ header for freetype2 (ft2build.h)

                        could not be found.  You may need to install the

                        development package.]

                   png: yes [pkg-config information for 'libpng' could not

                        be found. Using unknown version.]

                 qhull: yes [pkg-config information for 'qhull' could not be

                        found. Using local copy.]



OPTIONAL SUBPACKAGES

           sample_data: yes [installing]

              toolkits: yes [installing]

                 tests: yes [using nose version 1.3.0 / mock is required to

                        run the matplotlib test suite.  pip/easy_install may

                        attempt to install it after matplotlib.]



OPTIONAL BACKEND EXTENSIONS

                macosx: yes [installing, darwin]

                qt4agg: no  [PyQt4 not found]

               gtk3agg: no  [Requires pygobject to be installed.]

             gtk3cairo: no  [Requires cairocffi or pycairo to be installed.]

                gtkagg: no  [Requires pygtk]

                 tkagg: yes [installing, version 81008]

                 wxagg: no  [requires wxPython]

                   gtk: no  [Requires pygtk]

                   agg: yes [installing]

                 cairo: no  [cairocffi or pycairo not found]

             windowing: no  [Microsoft Windows only]



OPTIONAL LATEX DEPENDENCIES

                dvipng: yes [version 1.14]

           ghostscript: no

                 latex: yes [version 3.1415926]

               pdftops: no



============================================================================

                        * The following required packages can not be built:

                        * freetype

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /var/folders/rr/svlz5yhs0tsdqnsp8g4qg65m0000gn/T/pip-beHCX1-build

Apparently I have freetype 2.4.10 installed, according to conda.

@tacaswell
Copy link
Member

Does this PR help? #3032

@mwaskom
Copy link
Author

mwaskom commented May 17, 2014

Well, with that I can install matplotlib but not import it

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-eff513f636fd> in <module>()
----> 1 import matplotlib.pyplot as plt

/Users/mwaskom/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py in <module>()
     25
     26 import matplotlib
---> 27 import matplotlib.colorbar
     28 from matplotlib import style
     29 from matplotlib import _pylab_helpers, interactive

/Users/mwaskom/anaconda/lib/python2.7/site-packages/matplotlib/colorbar.py in <module>()
     32 import matplotlib.artist as martist
     33 import matplotlib.cbook as cbook
---> 34 import matplotlib.collections as collections
     35 import matplotlib.colors as colors
     36 import matplotlib.contour as contour

/Users/mwaskom/anaconda/lib/python2.7/site-packages/matplotlib/collections.py in <module>()
     25 import matplotlib.artist as artist
     26 from matplotlib.artist import allow_rasterization
---> 27 import matplotlib.backend_bases as backend_bases
     28 import matplotlib.path as mpath
     29 from matplotlib import _path

/Users/mwaskom/anaconda/lib/python2.7/site-packages/matplotlib/backend_bases.py in <module>()
     54
     55 import matplotlib.tight_bbox as tight_bbox
---> 56 import matplotlib.textpath as textpath
     57 from matplotlib.path import Path
     58 from matplotlib.cbook import mplDeprecation

/Users/mwaskom/anaconda/lib/python2.7/site-packages/matplotlib/textpath.py in <module>()
     17 from matplotlib.path import Path
     18 from matplotlib import rcParams
---> 19 import matplotlib.font_manager as font_manager
     20 from matplotlib.ft2font import FT2Font, KERNING_DEFAULT, LOAD_NO_HINTING
     21 from matplotlib.ft2font import LOAD_TARGET_LIGHT

/Users/mwaskom/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py in <module>()
     55 import matplotlib
     56 from matplotlib import afm
---> 57 from matplotlib import ft2font
     58 from matplotlib import rcParams, get_cachedir
     59 from matplotlib.cbook import is_string_like

ImportError: dlopen(/Users/mwaskom/anaconda/lib/python2.7/site-packages/matplotlib/ft2font.so, 2): Library not loaded: libfreetype.6.dylib
  Referenced from: /Users/mwaskom/anaconda/lib/python2.7/site-packages/matplotlib/ft2font.so
  Reason: image not found

@tacaswell
Copy link
Member

I guess that is progress? @maciejkula can you take a look at this? iirc @efiring is a mac user. Do we have a local anaconda expert?

@efiring
Copy link
Member

efiring commented May 17, 2014

Offhand, it seems like the problem is related overwriting the Anaconda matplotlib with the one you are building with pip. I think I would try to restore a clean Anaconda, and then build and install in some other location. I suppose a virtualenv could be used for this, or maybe the --user option to pip install. That's just a rough outline; I don't know what the best method would be in detail.

@mwaskom
Copy link
Author

mwaskom commented May 17, 2014

The example in the first post should be sufficient to test for anyone who has Arial (or Liberation Sans) installed and is using master.

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams["font.sans-serif"] = ["Arial", "Liberation Sans", "Bitstream Vera Sans"]
mpl.rcParams["font.family"] = "sans-serif"

ax = plt.subplot()
ax.plot([0, 1, 2], [0, 2, 1])
ax.set_xlabel("the x label")

assert ax.xaxis.label.get_fontname() == "Arial"

@tacaswell
Copy link
Member

Closing this as that example works correctly for me on current master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants