title in italic font in matplotlib isn't working

import matplotlib.pyplot as plt
from textwrap import wrap

x_list = [11, 3, 6, 5]
label_list = ["red colour", "blue colour", "green colour", "back colour"]

title = "Some really really long long long title I really really need - and just can't - just can't - make it any - simply any - shorter - at all?"

newtitle = '\n'.join(wrap(title, width=50))  # wrap the long title so that it won't get cropped.

# here if I print `newtitle` first then "text below the main title" gets convert into italics
newtitle = "%s\n$%s$"%(newtitle, 'text\ below\ the\ main\ title') 
# but If I print `newtitle` after the secondary text it doesn't convert into italics
newtitle = "%s\n$%s$"%('text\ below\ the\ main\ title', newtitle)  # comment this line for 1st effect

plt.figure(figsize=(7,6), dpi=100)
plt.axis("equal")
plt.subplots_adjust(left=0.1, right=0.8, top=0.7)
plt.rc("font", size=10)
explode = [0.03, 0, 0, 0]

plt.pie(x_list, labels=label_list, explode=explode, autopct="%1.1f%%", startangle=90)
plt.title(newtitle, size=12)

plt.savefig('test.png')

So my main problem is -

secondary text come first in normal font and then main title(newtitle) come after this in italics/light font (looks pretty).

newtitle = "%s\n$%s$"%('text\ below\ the\ main\ title', newtitle)

Can we do it without using $ sign?

Answers


Is this the intended result?

It can be done in a quick and dirty way using mathtext, but you have to have $ flaking each lines, such as:

newtitle = '\n'.join(['$%s$'%item for item in wrap(title, width=50)]).replace(' ', '\ ')
newtitle = "%s\n%s"%('text\ below\ the\ main\ title', newtitle)

But I think a better looking way is to have these two titles separated. You can use suptitle for the upper one, or maybe just use text, either way you can then control their properties independently. e.g.:

plt.pie(x_list, labels=label_list, explode=explode, autopct="%1.1f%%", startangle=90)
plt.title('\n'.join(wrap(title, width=50)), size=12, style='italic')
plt.suptitle('some title', y=0.85, x=0.45) #y and x needed as you have adjusted the subplot size already.


Need Your Help

parsing CDATA in XElement

c# xml xml-parsing xmlreader xelement

I am trying to parse an XML with CDATA elements...my below code blows up if content in CDATA is xml encoded...is there any way i can decode all the xml before i parse..

Is there a way of placing a video in my Java game main menu

java video slick2d

I have made a simple Java game, when I load the game a start-up menu pops up with a simple play button and a close button, if I press the start button Java changes to another class which takes the ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.