3 回答

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
繪制多個(gè)圖形的基本形式是下面的方法 作為先決條件,您只需要決定列數(shù)。cols=3 剩下的循環(huán)過程由詞典的數(shù)量來完成。
import matplotlib.pyplot as plt
import pandas as pd
import math
z = {'A': [0.3618426, 0.36146951], 'B': [1.8908799, 1.904695], 'C': [2.1813462e+08, 2.1833622e+08], 'D': [0.89925492, 0.89953589], 'E': [2.6356747, 2.6317911], 'F': [2.2250445e+08, 2.2501808e+08], 'G': [2.0806053e+08, 2.0691238e+08], 'H': [0.37242803, 0.37611806]}
k = [1,2]
cols = 3
rows = math.ceil(len(z) / cols)
fig, axes = plt.subplots(rows, cols, figsize=(16,12))
dict_keys = [k for k in z.keys()]
l = 0
for i in range(rows):
for j in range(cols):
if len(z) == l:
break
else:
key = dict_keys[i+j]
axes[i][j].plot(k, [z[key][0],z[key][1]], 'ro-')
l += 1
plt.show()

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊
如果沒有重復(fù)的子圖,@r-beginners 的答案是完美的。我做了兩個(gè)小修改:
import matplotlib.pyplot as plt
import math
z = {'A': [0.3618426, 0.36146951], 'B': [1.8908799, 1.904695], 'C': [2.1813462e+08, 2.1833622e+08], 'D': [0.89925492, 0.89953589],
'E': [2.6356747, 2.6317911], 'F': [2.2250445e+08, 2.2501808e+08], 'G': [2.0806053e+08, 2.0691238e+08], 'H': [0.37242803, 0.37611806]}
k = [1, 2]
cols = 3
rows = math.ceil(len(z) / cols)
fig, axes = plt.subplots(rows, cols, figsize=(16, 12))
dict_keys = [m for m in z.keys()]
l = 0
for i in range(rows):
for j in range(cols):
if len(z) == l:
break
else:
key = dict_keys[l] # modified
axes[i][j].plot(k, [z[key][0], z[key][1]], 'ro-')
l += 1 # modified
plt.show()

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊
這是一個(gè)可能對您有幫助的代碼:
from math import ceil
# 9 elements in my dict (4x4 + 1)
z = {'A': [0.3618426, 0.36146951], 'B': [1.8908799, 1.904695], 'C': [2.1813462e+08, 2.1833622e+08], 'D': [0.89925492, 0.89953589], 'E': [2.6356747, 2.6317911], 'F': [2.2250445e+08, 2.2501808e+08], 'G': [2.0806053e+08, 2.0691238e+08], 'H': [0.37242803, 0.37611806], 'X': [0.37242803, 0.37611806]}
k = [1,2]
plt.subplots(figsize=(16,8)) # optional
# fixed number of columns
cols = 4
# number of rows, based on cols
rows = ceil(len(z) / cols)
# iterate through indices and keys
for index, key in enumerate(z):
# new subplot with (i + 1)-th index laying on a grid
plt.subplot(rows, cols, index + 1)
# drawing the plot
plt.plot(k, [z[key][0], z[key][1]], 'ro-')
# render everything
plt.show()
添加回答
舉報(bào)