Python中的 Matplotlib.pyplot.title()
Matplotlib是Python中用于数组二维图的惊人可视化库。 Matplotlib 是一个基于 NumPy 数组构建的多平台数据可视化库,旨在与更广泛的 SciPy 堆栈配合使用。
Matplotlib.pyplot.title()
matplotlib 模块中的title()
方法用于指定所描绘的可视化的标题,并使用各种属性显示标题。
Syntax: matplotlib.pyplot.title(label, fontdict=None, loc=’center’, pad=None, **kwargs)
Parameters:
label
(str): This argument refers to the actual title text string of the visualization depicted.fontdict
(dict) : This argument controls the appearance of the text such as text size, text alignment etc. using a dictionary. Below is the default fontdict:fontdict = {‘fontsize’: rcParams[‘axes.titlesize’],
‘fontweight’ : rcParams[‘axes.titleweight’],
‘verticalalignment’: ‘baseline’,
‘horizontalalignment’: loc}loc
(str): This argument refers to the location of the title, takes string values like'center'
,'left'
and'right'
.pad
(float): This argument refers to the offset of the title from the top of the axes, in points. Its default values in None.- **kwargs: This argument refers to the use of other keyword arguments as text properties such as
color
,fonstyle
,linespacing
,backgroundcolor
,rotation
etc.
Return Type: The title()
method returns a string that represents the title text itself.
下面是一些示例来说明title()
方法的使用:
示例 1:使用matplotlib.pyplot
描绘线性图并使用matplotlib.pyplot.title()
显示其标题。
Python3
# importing module
import matplotlib.pyplot as plt
# assigning x and y coordinates
y = [0,1,2,3,4,5]
x= [0,5,10,15,20,25]
# depicting the visualization
plt.plot(x, y, color='green')
plt.xlabel('x')
plt.ylabel('y')
# displaying the title
plt.title("Linear graph")
plt.show()
Python3
# importing module
import matplotlib.pyplot as plt
# assigning x and y coordinates
x = [-5,-4,-3,-2,-1,0,1,2, 3, 4, 5]
y = []
for i in range(len(x)):
y.append(max(0,x[i]))
# depicting the visualization
plt.plot(x, y, color='green')
plt.xlabel('x')
plt.ylabel('y')
# displaying the title
plt.title(label="ReLU function graph",
fontsize=40,
color="green")
Python3
# importing modules
import matplotlib.pyplot as plt
import numpy as np
# assigning x and y coordinates
language = ['C','C++','Java','Python']
users = [80,60,130,150]
# depicting the visualization
index = np.arange(len(language))
plt.bar(index, users, color='green')
plt.xlabel('Users')
plt.ylabel('Language')
plt.xticks(index, language)
# displaying the title
plt.title(label='Number of Users of a particular Language',
fontweight=10,
pad='2.0')
Python3
# importing modules
from matplotlib import pyplot as plt
# assigning x and y coordinates
foodPreference = ['Vegetarian', 'Non Vegetarian',
'Vegan', 'Eggitarian']
consumers = [30,100,10,60]
# depicting the visualization
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.axis('equal')
ax.pie(consumers, labels = foodPreference,
autopct='%1.2f%%')
# displaying the title
plt.title(label="Society Food Preference",
loc="left",
fontstyle='italic')
Python3
# importing modules
from matplotlib import pyplot
import numpy
# assigning time values of the signal
# initial time period, final time period
# and phase angle
signalTime = numpy.arange(0, 100, 0.5);
# getting the amplitude of the signal
signalAmplitude = numpy.sin(signalTime)
# depicting the visualization
pyplot.plot(signalTime, signalAmplitude, color ='green')
pyplot.xlabel('Time')
pyplot.ylabel('Amplitude')
# displaying the title
pyplot.title("Signal",
loc='right',
rotation=45)
Python3
# importing modules
from PIL import ImageTk, Image
from matplotlib import pyplot as plt
# depicting the visualization
testImage = Image.open('g4g.png')
# displaying the title
plt.title("Geeks 4 Geeks",
fontsize='20',
backgroundcolor='green',
color='white')
plt.imshow(testImage)
输出:
在上面的示例中,只有label
参数在title()
方法中被分配为“线性图”,而其他参数被分配为其默认值。 label
参数的分配是显示可视化标题的最低要求。
示例 2:使用matplotlib.pyplot
描绘 ReLU函数图并使用matplotlib.pyplot.title()
显示其标题。
Python3
# importing module
import matplotlib.pyplot as plt
# assigning x and y coordinates
x = [-5,-4,-3,-2,-1,0,1,2, 3, 4, 5]
y = []
for i in range(len(x)):
y.append(max(0,x[i]))
# depicting the visualization
plt.plot(x, y, color='green')
plt.xlabel('x')
plt.ylabel('y')
# displaying the title
plt.title(label="ReLU function graph",
fontsize=40,
color="green")
输出:
上面的程序说明了label
参数的使用, fontdict
参数的fontsize
键和color
参数,这是一个额外的参数(由于**kwargs
),它改变了文本的颜色。
示例 3:使用matplotlib.pyplot
描绘条形图并使用matplotlib.pyplot.title()
显示其标题。
Python3
# importing modules
import matplotlib.pyplot as plt
import numpy as np
# assigning x and y coordinates
language = ['C','C++','Java','Python']
users = [80,60,130,150]
# depicting the visualization
index = np.arange(len(language))
plt.bar(index, users, color='green')
plt.xlabel('Users')
plt.ylabel('Language')
plt.xticks(index, language)
# displaying the title
plt.title(label='Number of Users of a particular Language',
fontweight=10,
pad='2.0')
输出:
这里,在title()
方法中使用了fontdict
参数和pad
参数的fontweight
键以及label
参数。
示例 4:使用matplotlib.pyplot
描绘饼图并使用matplotlib.pyplot.title()
显示其标题。
Python3
# importing modules
from matplotlib import pyplot as plt
# assigning x and y coordinates
foodPreference = ['Vegetarian', 'Non Vegetarian',
'Vegan', 'Eggitarian']
consumers = [30,100,10,60]
# depicting the visualization
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.axis('equal')
ax.pie(consumers, labels = foodPreference,
autopct='%1.2f%%')
# displaying the title
plt.title(label="Society Food Preference",
loc="left",
fontstyle='italic')
输出:
在上面的饼图数据可视化中, label
、 fontweight
在title()
方法中使用来自fontdict
和fontstyle
( **kwargs
) 参数的关键字(采用字符串值,例如'italic'
、 'bold'
和'oblique'
)来显示饼图的标题。
示例 5:使用matplotlib.pyplot
在图表中可视化信号并使用matplotlib.pyplot.title()
显示其标题。
Python3
# importing modules
from matplotlib import pyplot
import numpy
# assigning time values of the signal
# initial time period, final time period
# and phase angle
signalTime = numpy.arange(0, 100, 0.5);
# getting the amplitude of the signal
signalAmplitude = numpy.sin(signalTime)
# depicting the visualization
pyplot.plot(signalTime, signalAmplitude, color ='green')
pyplot.xlabel('Time')
pyplot.ylabel('Amplitude')
# displaying the title
pyplot.title("Signal",
loc='right',
rotation=45)
输出:
在这里, label
参数被分配给'signal'
, loc
参数被分配给'right'
并且以度为单位的角度值的rotation
参数( **kwargs
)被分配给 45 度。
示例 6:使用matplotlib.pyplot
显示图像并使用matplotlib.pyplot.title()
显示其标题。
Python3
# importing modules
from PIL import ImageTk, Image
from matplotlib import pyplot as plt
# depicting the visualization
testImage = Image.open('g4g.png')
# displaying the title
plt.title("Geeks 4 Geeks",
fontsize='20',
backgroundcolor='green',
color='white')
plt.imshow(testImage)
输出:
在上面的示例中,使用title()
方法显示图像的标题,其参数label
为"Geeks 4 Geeks"
,来自fontdict
的fontsize
键为'20'
, backgroundcolor
和color
是具有字符串值'green'
和分别为'white'
。