Matplotlib 如何为所有子图设置单个主标题

Matplotlib 如何为所有子图设置单个主标题

多个子图由一系列不同的绘图图表定义。Matplotlib是一种在Python中提供动画和交互式可视化的模块。在Python中,我们有一些内置函数,名为suptitle(),可用于为Matplotlib中的所有子图设置单个主标题。

语法

示例中使用以下语法:

array()

Python的数组方法是通过返回具有特定值的元素的数量来定义的。

suptitle()

这是Python中的内置方法,可用于设置所有子图的主标题。

add_subplot()

这个方法遵循了包装模块即matplotlib.pyplot,并在处理图像窗口api时提供了额外的行为。

imshow()

这是Python中的内置函数,可以用来在窗口中显示图像。

set_ylabel()
set_xlabel()

这是两个内置函数,它们跟随matplotlib模块一起使用,用于设置垂直和水平两侧的标签或文本。

show()

这是Python中的内置方法,用于在程序结束时以图形形式获取结果。

figure()

figure是Python中的一个内建方法,用于创建一个新的图形。

示例1

在下面的示例中,我们将通过导入名为numpy和matplotlib.pyplot的模块来开始程序。然后使用内建方法array()设置数组列表并将其存储在变量x中。然后初始化fig、ax(表示为名为fig的类的对象)并通过使用名为matplotlib.pyplot的包装模块进行子图设置。接下来,使用内建方法subplot()设置4个不同的子图。然后使用内建方法suptitle()设置主标题,主标题用于所有绘图图表。最后,使用show()方法获取所需的结果。

import numpy as np
import matplotlib.pyplot as plt
# create data
x=np.array([1, 2, 3, 4, 5])

# making subplots
fig, ax = plt.subplots(2, 2)

# Set data with subplots and plot
ax[0, 0].plot(x, x)
ax[0, 1].plot(x, x*9)
ax[1, 0].plot(x, x*x*x)
ax[1, 1].plot(x, x*4)
plt.suptitle('Various ways to plotting the graph')
plt.show()

输出

Matplotlib 如何为所有子图设置单个主标题

示例2

在以下示例中,我们将展示所有不同绘图图形的主标题。然后开始导入名为matplotlib.pyplot的包装器模块。然后使用名为figure()的方法创建新的图形。接下来,使用numpy模块(即np)的arange()函数 [定义包含在数组中的间隔值] 和reshape()函数。然后开始迭代for循环,根据图像处理使用内置方法add_subplot()和imshow()设置6个图形。继续使用内置方法suptitle()设置所有绘图图形的主标题,并使用show()方法得到结果。

import matplotlib.pyplot as pl
import numpy as np
fig=pl.figure()
ax_range=np.arange(100).reshape((10,10))
for i in range(1,7):
   ax=fig.add_subplot(3,3,i)        
   ax.imshow(ax_range)
# Set the main title of all the subplots
fig.suptitle('The image processing of various graph ') 
pl.show()

输出

Matplotlib 如何为所有子图设置单个主标题

示例3

在下面的示例中,我们将开始实现必要的库pandas,numpy和matplotlib.pyplot,并将对象引用分别设置为pd,np和plt。然后创建遵循pandas模块的DataFrame,默认参数为np.random.rand()[创建指定形状的数组并填充值]和columns[以列表形式设置列的总数]。接下来,将x轴和y轴的数据设置为变量x和y。然后使用subplot()将子图划分为两行两列,宽度和高度相同,即figsize(8,8)。现在,使用内置的plot()方法绘制坐标轴,并使用内置的grid()方法在图表上设置网格线。为了在图表上显示一些独特性,将在第一行和第一列的轴上使用标签。继续设置主任务,即设置所有绘图柱状图的主标题。最后,通过show()方法打印结果。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# create the data
df=pd.DataFrame(np.random.rand(5, 5), columns=['a', 'b', 'c', 'd','e'])

# create the data
x=[1,2,3,4,5]
y=[5,10,15,20,25]

fig, axes = plt.subplots(figsize=(8,8),nrows=2, ncols=2)
ax1=plt.subplot(2,2,1)
plt.plot(x,y)

# plot the axes
df["b"].plot(ax=axes[1,0], kind='bar', grid=True)
df["c"].plot(ax=axes[1,1], kind='bar', grid=True)
df["d"].plot(ax=axes[0,1], kind='bar', grid=True)

# Set the gridlines on all the plotting graph
ax1.grid(True)

# Set the horizontal and vertical text
ax1.set_ylabel('Test')
ax1.set_xlabel('Test2')
fig.suptitle('BAR GRAPH')
plt.show()

输出

Matplotlib 如何为所有子图设置单个主标题

示例4

在下面的示例中,我们首先使用Seaborn模块创建一个散点图来可视化在两个不同部分(A和B)中学生数量和他们的分数之间的关系。然后,我们使用replot()方法设置一些参数(数据、x、y和col)来创建散点图。接下来,使用rel.fig.suptitle()内置方法设置主标题,并得到最终结果。

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

#create the fake data
df = pd.DataFrame({'students': [15, 12, 15, 24, 19, 23, 25, 29],
   'score': [4, 8, 9, 10, 7, 5, 8, 3],
   'section': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B']})

#create relplot
rel = sns.relplot(data=df, x='students', y='score', col='section')

#add the main title
rel.fig.suptitle('Status of student performance')

输出

Matplotlib 如何为所有子图设置单个主标题

结论

我们讨论了在一个单一框架内绘制不同图形的不同方法。suptitle()是Python中Matplotlib中设置所有子图的单个主标题的重要方法。我们看到了一个新概念,即在图形上进行图像处理,而其他示例则显示了条形图、散点图和线图等图形。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程