Bạn có thể tạo ra một âm mưu phụ lớn bao gồm hai ô phụ và sau đó thiết lập các nhãn phổ biến.
import random
import matplotlib.pyplot as plt
x = range(1, 101)
y1 = [random.randint(1, 100) for _ in xrange(len(x))]
y2 = [random.randint(1, 100) for _ in xrange(len(x))]
fig = plt.figure()
ax = fig.add_subplot(111) # The big subplot
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
# Turn off axis lines and ticks of the big subplot
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_color('none')
ax.spines['left'].set_color('none')
ax.spines['right'].set_color('none')
ax.tick_params(labelcolor='w', top='off', bottom='off', left='off', right='off')
ax1.loglog(x, y1)
ax2.loglog(x, y2)
# Set common labels
ax.set_xlabel('common xlabel')
ax.set_ylabel('common ylabel')
ax1.set_title('ax1 title')
ax2.set_title('ax2 title')
plt.savefig('common_labels.png', dpi=300)
Một cách khác là sử dụng fig.text() để thiết lập vị trí của các nhãn phổ biến trực tiếp.
import random
import matplotlib.pyplot as plt
x = range(1, 101)
y1 = [random.randint(1, 100) for _ in xrange(len(x))]
y2 = [random.randint(1, 100) for _ in xrange(len(x))]
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax1.loglog(x, y1)
ax2.loglog(x, y2)
# Set common labels
fig.text(0.5, 0.04, 'common xlabel', ha='center', va='center')
fig.text(0.06, 0.5, 'common ylabel', ha='center', va='center', rotation='vertical')
ax1.set_title('ax1 title')
ax2.set_title('ax2 title')
plt.savefig('common_labels_text.png', dpi=300)
Nguồn
2011-08-08 10:52:03
Chức năng suptitle sử dụng phiên bản fig.text(). Vì vậy, đây có thể là cách "chính thức" để làm điều đó? – PhML
Cần nhấn mạnh rằng 'ax' phải được tạo trước' ax1' và 'ax2', nếu không ô lớn sẽ che phủ các ô nhỏ. –
ax.grid (False) hoặc plt.grid (False) cũng cần thiết nếu các thông số âm mưu toàn cục bao gồm lưới (có thể nhìn thấy). –