Gödel
Gödel

Reputation: 592

How can I change the text direction of tick labels in Matplotlib?

I have to use Matplotlib library to visualize some data with Chinese tick labels on XAxis. Here is an example:

A plot with Chinese tick labels

I want to change the text direction of the tick labels to get a plot like this:

enter image description here

Note, it is not a 'text rotation'. It's about the text direction.

How can I achieve this in Matplotlib?

P.S. The above plots are created in Excel for demonstration. Here is the code of Matplotlib, which can produce a plot just like the first one. Well, I can't produce the second one in Matplotlib, That's why I'm here...

import matplotlib.pyplot as plt
import matplotlib.font_manager as mfont
myfont = mfont.FontProperties('SimHei')

data = [1,2,3,4,5]
labels = ['中文1', '中文2', '中文3', '中文4', '中文5']

fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(data, data, tick_label=labels)

for label in ax.xaxis.get_majorticklabels():
    label.set(fontproperties=myfont)
plt.show()

I inserted new lines in the label texts, and I got a plot like this: enter image description here

Something has occluded the text.

The code for the above plot:

import matplotlib.pyplot as plt
import matplotlib.font_manager as mfont
# myfont = mfont.FontProperties('SimHei', size=30)

data = [1,2,3,4,5]
labels = ['中\n文\n1', '中\n文\n2 ', '中\n文\n3', '中\n文\n4', '中\n文\n5']

fig = plt.figure()
fig.set_facecolor('white')
ax = fig.add_subplot(111)
ax.bar(data, data, tick_label=labels)

for label in ax.xaxis.get_majorticklabels():
    label.set(family='SimHei', size=15, backgroundcolor='red', linespacing=3)
plt.show()

Upvotes: 4

Views: 2420

Answers (3)

Orient Zdf
Orient Zdf

Reputation: 86

visit:

https://matplotlib.org/gallery/ticks_and_spines/ticklabels_rotation.html

example:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 9, 6]
labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']

plt.plot(x, y, 'ro')

You can specify a rotation for the tick labels in degrees or with keywords.

plt.xticks(x, labels, rotation='vertical')

Pad margins so that markers don't get clipped by the axes

plt.margins(0.2)

Tweak spacing to prevent clipping of tick-labels

plt.subplots_adjust(bottom=0.15)
plt.show()

Upvotes: 4

Venkatachalam
Venkatachalam

Reputation: 16966

A workaround to get each word in a new line while printing the ticks.

labels = ['\n'.join(list(tick)) for tick in labels ]

Upvotes: 1

Ernest S Kirubakaran
Ernest S Kirubakaran

Reputation: 1564

I don't think there are any option to print text vertically in matplotlib. However, you can do a simple hack by inserting new line character in the text which you want to print.

import matplotlib.pyplot as plt
import matplotlib.font_manager as mfont
myfont = mfont.FontProperties('SimHei')

data = [1,2,3,4,5]
labels = ['中\n文\n1', '中\n文\n2', '中\n文\n3', '中\n文\n4', '中\n文\n5']

fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(data, data, tick_label=labels)

for label in ax.xaxis.get_majorticklabels():
    label.set(fontproperties=myfont)
plt.show()

Upvotes: 2

Related Questions