Matplotblib|固定マルチサブグラフ位置 [サブロットを使用しない]

論文を書く際には実験データを描く必要があり、複数のグラフを比較する必要があり、そのために複数の部分グラフを描く必要があります。

最も一般的な方法はサブロットですが、これを行うとサブプロットのサイズが適応的に変化してしまい、絵が変形してしまうため、これを防ぐためにmatlotlibのAxesを使って描画します。

例として1行4列の写真を見てみましょう

まず、rect 属性を定義します。ここで、

Rect(int x, int y, int width, int height);
x = 左上角 x 坐标  
y = 左上角 y 坐标
width = 矩形的宽
height = 矩形的高

次に、対応するサブグラフに属性を割り当てます。

ax1 = plt.axes(rect1)

視覚化の結果を図に示します。


完全なコード:

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

rect1 = [0.05,0.35,0.15,0.47]
rect2 = [0.25,0.35,0.15,0.47]
rect3 = [0.45,0.35,0.15,0.47]
rect4 = [0.65,0.35,0.15,0.47]

x_labels = ['A', 'B', 'C', 'D', 'E', ]

data1 = [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
data2 = [[3, 5, 7, 9, 12], [3, 5, 7, 9, 12], [3, 5, 7, 9, 12], [3, 5, 7, 9, 12]]
data3 = [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
data4 = [[3, 5, 7, 9, 12], [3, 5, 7, 9, 12], [3, 5, 7, 9, 12], [3, 5, 7, 9, 12]]

y_1 = [data1[0], data2[0], data3[0], data4[0]]
y_2 = [data1[1], data2[1], data3[1], data4[1]]
y_3 = [data1[2], data2[2], data3[2], data4[2]]
y_4 = [data1[3], data2[3], data3[3], data4[3]]
legend_label = ['1', '2', '3', '4']

fig = plt.figure(figsize=(20, 4))

ax1 = plt.axes(rect1)
#ax1 = fig.add_subplot(facecolor='white')
# 红橙黄绿青蓝紫
color_list = ['#7A8C6F','#945555', '#FBDC8F',  '#0000FF', 'black']
x_loc = np.arange(5)
# x轴上每个刻度上能容纳的柱子的总的宽度设为0.8
total_width = 0.8

total_num = 4
# 每个柱子的宽度用each_width表示
each_width = total_width / total_num
if total_num % 2 == 0:
    x1 = x_loc - (total_num / 2 - 1) * each_width - each_width / 2
else:
    x1 = x_loc - ((total_num - 1) / 2) * each_width
x_list = [x1 + each_width * i for i in range(total_num)]
print(x_list)
# 这里颜色设置成 橙色:"#FF8C00"
for i in range(0, len(y_1)):
    ax1.bar(x_list[i], y_1[i], color=color_list[i], width=each_width, label=legend_label[i])
ax1.set_xticks(x_loc)
ax1.set_xticklabels(x_labels)
ax1.grid(True, ls=':', color='b', alpha=0.3)
ax1.set_xlabel('Y', fontweight='bold', fontsize=15)
ax1.set_ylabel('X', fontweight='bold', fontsize=15)

# ——————————————————————————————————————
ax2 = plt.axes(rect2)
# 红橙黄绿青蓝紫
color_list = ['#7A8C6F','#945555', '#FBDC8F',  '#0000FF', 'black']
x_loc_2 = np.arange(5)
# x轴上每个刻度上能容纳的柱子的总的宽度设为0.8
total_width = 0.8

total_num = 4
# 每个柱子的宽度用each_width表示
each_width = total_width / total_num
if total_num % 2 == 0:
    x1 = x_loc_2 - (total_num / 2 - 1) * each_width - each_width / 2
else:
    x1 = x_loc_2 - ((total_num - 1) / 2) * each_width
x_list = [x1 + each_width * i for i in range(total_num)]
print(x_list)
# 这里颜色设置成 橙色:"#FF8C00"
for i in range(0, len(y_2)):
    ax2.bar(x_list[i], y_2[i], color=color_list[i], width=each_width, label=legend_label[i])
ax2.set_xticks(x_loc)
ax2.set_xticklabels(x_labels)
ax2.grid(True, ls=':', color='b', alpha=0.3)
ax2.set_xlabel('Y', fontweight='bold', fontsize=15)
ax2.set_ylabel('X', fontweight='bold', fontsize=15)

# ——————————————————————————————————————
ax3 = plt.axes(rect3)
# 红橙黄绿青蓝紫
color_list = ['#7A8C6F','#945555', '#FBDC8F',  '#0000FF', 'black']
x_loc_3 = np.arange(5)
# x轴上每个刻度上能容纳的柱子的总的宽度设为0.8
total_width = 0.8

total_num = 4
# 每个柱子的宽度用each_width表示
each_width = total_width / total_num
if total_num % 2 == 0:
    x1 = x_loc_3 - (total_num / 2 - 1) * each_width - each_width / 2
else:
    x1 = x_loc_3 - ((total_num - 1) / 2) * each_width
x_list = [x1 + each_width * i for i in range(total_num)]
print(x_list)
# 这里颜色设置成 橙色:"#FF8C00"
for i in range(0, len(y_3)):
    ax3.bar(x_list[i], y_3[i], color=color_list[i], width=each_width, label=legend_label[i])
ax3.set_xticks(x_loc)
ax3.set_xticklabels(x_labels)
ax3.grid(True, ls=':', color='b', alpha=0.3)
ax3.set_xlabel('Y', fontweight='bold', fontsize=15)
ax3.set_ylabel('X', fontweight='bold', fontsize=15)

# ——————————————————————————————————————
ax4 = plt.axes(rect4)
# 红橙黄绿青蓝紫
color_list = ['#7A8C6F','#945555', '#FBDC8F',  '#0000FF', 'black']
x_loc_4 = np.arange(5)
# x轴上每个刻度上能容纳的柱子的总的宽度设为0.8
total_width = 0.8

total_num = 4
# 每个柱子的宽度用each_width表示
each_width = total_width / total_num
if total_num % 2 == 0:
    x1 = x_loc_4 - (total_num / 2 - 1) * each_width - each_width / 2
else:
    x1 = x_loc_4 - ((total_num - 1) / 2) * each_width
x_list = [x1 + each_width * i for i in range(total_num)]
print(x_list)
# 这里颜色设置成 橙色:"#FF8C00"
for i in range(0, len(y_4)):
    ax4.bar(x_list[i], y_4[i], color=color_list[i], width=each_width, label=legend_label[i])
ax4.set_xticks(x_loc)
ax4.set_xticklabels(x_labels)
ax4.grid(True, ls=':', color='b', alpha=0.3)
ax4.set_xlabel('Y', fontweight='bold', fontsize=15)
ax4.set_ylabel('X', fontweight='bold', fontsize=15)

ax1.legend(loc='upper center', bbox_to_anchor=(2.5, 1.3), frameon=False, ncol=4, handlelength=0.9, handleheight=0.9, fontsize='small')
plt.show()

おすすめ

転載: blog.csdn.net/qq_43604183/article/details/130893338
おすすめ