dunovank / jupyter-themes

Custom Jupyter Notebook Themes
MIT License
9.76k stars 1.06k forks source link

`jtplot.style` overwrite my font family configured in matplotlibrc #335

Open eromoe opened 5 years ago

eromoe commented 5 years ago

Detail in https://github.com/matplotlib/matplotlib/issues/13904 .

  1. style was set

    from jupyterthemes import jtplot
    jtplot.style()

I used to set font by :

  1. Find matplotlib config path :python -c 'import matplotlib; print(matplotlib.matplotlib_fname())'

  2. add font to that file:

    font.family : YaHei Consolas Hybrid
    font.serif : YaHei Consolas Hybrid
    font.sans-serif : YaHei Consolas Hybrid

    I also tried:

    font.family : "YaHei Consolas Hybrid"
    font.serif : "YaHei Consolas Hybrid"
    font.sans-serif : "YaHei Consolas Hybrid"
  3. check by :

    import matplotlib.pyplot as plt   
    plt.figure()
    plt.title("哈哈")
    plt.show()

Error title showed as image

Correct one:

import matplotlib
import matplotlib.pyplot as plt

matplotlib.rcParams['font.family'] = 'YaHei Consolas Hybrid'

plt.figure()
plt.title("哈哈")
plt.show()

image

set_context overwrite font :

def set_context(context='paper', fscale=1., figsize=(8., 7.)):
    """
    Most of this code has been copied/modified from seaborn.rcmod.plotting_context()
    ::Arguments::
        context (str): 'paper', 'notebook', 'talk', or 'poster'
        fscale (float): font-size scalar applied to axes ticks, legend, labels, etc.
    """
    # scale all the parameters by the same factor depending on the context
    scaling = dict(paper=.8, notebook=1, talk=1.3, poster=1.6)[context]
    context_dict = {k: v * scaling for k, v in base_context.items()}

    # scale default figsize
    figX, figY = figsize
    context_dict["figure.figsize"] = (figX*scaling, figY*scaling)

    # independently scale the fonts
    font_dict = {k: v * fscale for k, v in base_font.items()}
    font_dict["font.family"] = ["sans-serif"]
    font_dict["font.sans-serif"] = ["Helvetica", "Helvetica Neue", "Arial",
                                "DejaVu Sans", "Liberation Sans", "sans-serif"]
    context_dict.update(font_dict)
    return context_dict

I don't want to set font every time and aslo want to use jtplot.style . I think jtplot.style should inherit matplotlibrc then apply the style.