Skip to content

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

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

Open
CatNofishing opened this issue Jul 25, 2021 · 5 comments

Comments

@CatNofishing
Copy link

CatNofishing commented Jul 25, 2021

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

@tacaswell
Copy link
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?

@CatNofishing CatNofishing 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 Jul 26, 2021
@CatNofishing
Copy link
Author

CatNofishing commented Jul 26, 2021

@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

@CatNofishing CatNofishing 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 Jul 26, 2021
@jklymak
Copy link
Member

jklymak commented Jul 26, 2021

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.

@CatNofishing
Copy link
Author

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

@jklymak
Copy link
Member

jklymak commented Jul 27, 2021

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.

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