Here, I have a plot work to do with pandas, like this :
most_active_posts.plot(x = 'title',y = 'active_span',kind = 'barh')
most_active_posts
is an object of dataframe with index, I want a simple two-dimentional plot with two columns, one is 'title'
and the other is 'active_span'
.
title
is type of string, which contains Chinese characters, while active_span
is type of integer .
How can I display Chinese characters normally?
My work-around is like this:
Basically, you need to specify a valid font for Chinese characters.
I think you want the characters to be the labels on the plot right?
I just grabbed some random characters:
I don't think there is a way to set
y_ticklabels
fromdf.plot()
, but you can set them from the returnedaxes
object:Here's the figure:
I'm not actually able to save the file and have the characters show up. Not sure why at the moment, but this may get you started.
I find a python library designed for fixing Chinese display in pip. You can download it using the command in your terminal:
And you can write the following code instead(full code):
The result is like this
And it can help you handle matplotlib Chinese font for you! This is the github page:
https://github.com/201528015329004/pyplotz
And there are some handy examples:
https://github.com/201528015329004/pyplotz/blob/master/examples/quick_start.ipynb
Lib\site-packages\matplotlib\mpl-data
, and edit the filematplotlibrc
.font.family
andfont.sans-serif
then uncomment both of them and insetMicrosoft YaHei
afterfont.sans-serif
.