Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Figure title with several colors in matplotlib

Tags:

Is it possible to have multiple font colors in matplotlib figure titles? Something like this three colors in the title

like image 809
Boris Gorelik Avatar asked Feb 19 '12 14:02

Boris Gorelik


People also ask

Is PLT show () blocking?

Answer #1: show() (not with block=False ) and, most importantly, plt. pause(. 001) (or whatever time you want). The pause is needed because the GUI events happen while the main code is sleeping, including drawing.


2 Answers

The following snippet seems to work.

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 5, 0.1);
y = np.sin(x)
fig1 = plt.figure(1)
fig1.text(0.45, 0.95, "Case A", ha="center", va="bottom", size="medium",color="red")
fig1.text(0.5, 0.95, "&", ha="center", va="bottom", size="medium")
fig1.text(0.55,0.95,"Case B", ha="center", va="bottom", size="medium",color="blue")
plt.plot(x, y)
plt.show()

As far as I can see the title generated by matplotlib title function only contains one text object and hence can only have one font color. This is the reason for making multiple text elements on the figure.

like image 128
Appleman1234 Avatar answered Oct 02 '22 20:10

Appleman1234


One can also use figtext() command of matplotlib, like below,

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 5, 0.1)
for i in range(4):
    plt.subplot(2,2,i+1)
    plt.plot(x, np.sin((i+1)*x),'r')
    plt.plot(x, np.cos(4*x/(i+1)),'b')
    plt.title('(i+1)='+str(i+1))

plt.figtext(0.47, 0.96, "Case A", fontsize='large', color='r', ha ='right')
plt.figtext(0.53, 0.96, "Case B", fontsize='large', color='b', ha ='left')
plt.figtext(0.50, 0.96, ' vs ', fontsize='large', color='k', ha ='center')
plt.show()
like image 35
Deepak Gupta Avatar answered Oct 02 '22 18:10

Deepak Gupta