📜  如何更改 Matplotlib 图形中标题的字体大小?

📅  最后修改于: 2022-05-13 01:54:57.720000             🧑  作者: Mango

如何更改 Matplotlib 图形中标题的字体大小?

在本文中,我们将讨论如何使用matplotlib更改图形中标题的字体大小 模块。

当我们使用matplotlib.pyplot.title() 方法为标题分配绘图,因此为了更改字体大小,我们将使用pyplot.title()fontsize参数 matplotlib模块中的方法。

下面是一些描述如何更改matplotlib图形中标题字体大小的程序:

示例 1:

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("ReLU Function",
          fontsize = 40)


Python3
# importing modules
from matplotlib import pyplot as plt
  
# assigning x and y coordinates
foodPreferance = ['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 = foodPreferance,
       autopct='%1.2f%%')
  
# displaying the title
plt.title("Society Food Preferance",
          fontsize = 10)


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",
             fontsize = 30)


输出:

示例 2:

蟒蛇3

# importing modules
from matplotlib import pyplot as plt
  
# assigning x and y coordinates
foodPreferance = ['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 = foodPreferance,
       autopct='%1.2f%%')
  
# displaying the title
plt.title("Society Food Preferance",
          fontsize = 10)

输出:

示例 3:

蟒蛇3

# 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",
             fontsize = 30)

输出: