import matplotlib.pyplot as plt
import numpy as np
# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
# Four axes, returned as a 2-d array
f, axarr = plt.subplots(2, 2)
axarr[0, 0].plot(x, y)
axarr[0, 0].set_title('Title One')
axarr[0, 1].scatter(x, y)
axarr[0, 1].set_title('Title Two')
axarr[1, 0].plot(x, y ** 2)
axarr[1, 0].set_title('Title Three')
axarr[1, 1].scatter(x, y ** 2)
axarr[1, 1].set_title('Title Four')
# Fine-tune figure; hide x ticks for top plots and y ticks for right plots
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)
import matplotlib.pyplot as plt
import numpy as np
# List of subplot's description
description = ['''Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.''','''
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. ''', '''Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur.''',''' Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.''']
# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
# Four axes, returned as a 2-d array
f, axarr = plt.subplots(2, 2)
axarr[0, 0].plot(x, y)
axarr[0, 0].set_title('Title One')
axarr[0, 1].scatter(x, y)
axarr[0, 1].set_title('Title Two')
axarr[1, 0].plot(x, y ** 2)
axarr[1, 0].set_title('Title Three')
axarr[1, 1].scatter(x, y ** 2)
axarr[1, 1].set_title('Title Four')
# Fine-tune figure; hide x ticks for top plots and y ticks for right plots
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)
# Adjusting height between subplots and adding bottom space
plt.subplots_adjust(hspace = .4, bottom = .2)
# Print position and adding description manually
x0, y0 = axarr[0,0].get_position().x0, axarr[0,0].get_position().y0
f.text(x0,y0-.05,description[0])
x0, y0 = axarr[0,1].get_position().x0, axarr[0,1].get_position().y0
f.text(x0,y0-.05,description[1])
x0, y0 = axarr[1,0].get_position().x0, axarr[1,0].get_position().y0
f.text(x0,y0-.05,description[2])
x0, y0 = axarr[1,1].get_position().x0, axarr[1,1].get_position().y0
f.text(x0,y0-.05,description[3])
plt.show()
首先,为每个子批添加标题非常简单,可以在定义轴之后使用
set_title("Title")
来完成:(这个示例取自matplotlib's reference)为了在每个子图下面添加描述,我们必须编写一些复杂的代码,首先,我们需要使用^{} 在每个子图下面添加空间:
要编写文本,^{} ,注意我们需要每个x,y坐标,要得到它们,我能想到的最好办法是通过得到每个
bbox
的x0
和y0
参数来得到它们的坐标,所以,像这样:x0
,y0
只是引用点,请注意,此代码将在主子图本身中绘制文本:然而,即使这不是一个普遍的解决方案,在这种情况下,它似乎抵消了
y0
由.05
给我们一个相当不错的结果:完整代码:
相关问题 更多 >
编程相关推荐