Skip to content

the font set {"font.sans-serif":"SimHei"} is ignored by plt.rc_context in jupyter #20738

Closed as duplicate

Description

@CatNofishing

Bug report

plt.rc_context {"font.sans-serif":"SimHei"} does not work , but it work well in plt.rcParams.update(rc).

image

Code for reproduction

rc={
    "font.sans-serif":"SimHei",
    "axes.unicode_minus":False,
    }
with plt.rc_context(rc):
    plt.xlabel("上海市")

Expected outcome

image

Activity

  1. tacaswell commented on Jul 26, 2021

    @tacaswell
    Member

    @CatNofishing Can you determine if the issue is the rcparam is being not being set, that the rcparam is being set, but the font is not found, or if it is being set and ignored for some other reason?

    How did you generate the corect output?

  2. changed the title [-]plt.rc_context {"font.sans-serif":"SimHei"} does not work [/-] [+]the font set {"font.sans-serif":"SimHei"} is ignored by plt.rc_context[/+] on Jul 26, 2021
  3. CatNofishing commented on Jul 26, 2021

    @CatNofishing
    Author

    @tacaswell the font set {"font.sans-serif":"SimHei"} is ignored by plt.rc_context in jupyter notebook.

    the incorect output code:

    import matplotlib.pyplot as plt
    rc={
        "font.sans-serif":"SimHei"
    }
    
    with plt.rc_context(rc):
        plt.xlabel("上海市")

    image

    the correct output code:

    import matplotlib.pyplot as plt
    rc={
        "font.sans-serif":"SimHei"
    }
    plt.rcParams.update(rc)
    plt.xlabel("上海室")

    image

    another intersting thing is that if you add plt.show() in rc_context() end ,it works well in jupyter.

    import matplotlib.pyplot as plt
    rc={
        "font.sans-serif":"SimHei"
    }
    with plt.rc_context(rc):
        plt.xlabel("上海市")
        plt.show()

    It seems that xlabel is not rendered correctly in jupyter when you use plt.rc_context

  4. changed the title [-]the font set {"font.sans-serif":"SimHei"} is ignored by plt.rc_context[/-] [+]the font set {"font.sans-serif":"SimHei"} is ignored by plt.rc_context in jupyter[/+] on Jul 26, 2021
  5. jklymak commented on Jul 26, 2021

    @jklymak
    Member

    I think this is working as expected. If you don't render the label until after the rccontext then it renders in the global context. This is simply a case of it being confusing about what Matplotlib applies at instantiation of the label versus when it is rendered.

  6. CatNofishing commented on Jul 27, 2021

    @CatNofishing
    Author

    @jklymak Thanks your reply, I still can't understand what you mean,so it is not a bug?😂

  7. jklymak commented on Jul 27, 2021

    @jklymak
    Member

    The font can either be applied to the string when you call xlabel, or it can be applied to the string when the figure is rendered (shown or printed). You expect the first, but unfortunately Matplotlib does the second.

  8. github-actions commented on Oct 29, 2025

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  9. QuLogic commented on Oct 30, 2025

    @QuLogic
    Member

    This isn't a bug per se, but has to do with the laziness of rcParam evaluation. I'm not sure if this one in particular is inconsistent with others, but there are other issues that track that as an overall problem, so I'll this close one as a duplicate.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions