简单地假设我有2个列表:
A - >有名单列表['A','B','C','D','E','F','G','H']
B - >有值列表[5,7,3,8,2,9,1,3]
A将是X轴标签的名称,B中的相应值将是图形的高度(即Y轴)。
%matplotlib inline
import pandas as pd
from matplotlib import rcParams
import matplotlib.pyplot as plt
from operator import itemgetter
rcParams.update({'figure.autolayout': True})
plt.figure(figsize=(14,9), dpi=600)
reso_names = ['A','B','C','D','E','F','G','H']
reso_values = [5,7,3,8,2,9,1,3]
plt.bar(range(len(reso_values)), reso_values, align='center')
plt.xticks(range(len(reso_names)), list(reso_names), rotation='vertical')
plt.margins(0.075)
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.title('Graph', {'family' : 'Arial Black',
'weight' : 'bold',
'size' : 22})
plt.show()
但是我希望它能够为每2个值生成子图。在这种情况下,应该有4个子图:
这种分裂应该动态完成(不是4个不同的循环,它应该将图形分成2个单位,取决于输入的大小,如果列表A有10个值,那么它应该给出5个子图)。
我想出了如何将图形分成两半,每个都有一半,但我需要使用每个图形的N步长来实现它(本例中的N为2)。
我将图形分成两个相等的子图的代码是:
%matplotlib inline
import pandas as pd
from matplotlib import rcParams
import matplotlib.pyplot as plt
from operator import itemgetter
rcParams.update({'figure.autolayout': True})
plt.figure(figsize=(14,9), dpi=600)
reso_names = ['A','B','C','D','E','F','G','H']
reso_values = [5,7,3,8,2,9,1,3]
fig, axs = plt.subplots(nrows=2, sharey=True, figsize=(14,18), dpi=50)
size = int(len(reso_values))
half = int( size/2 )
fig.suptitle('Graph',
**{'family': 'Arial Black', 'size': 22, 'weight': 'bold'})
for ax, start, end in zip(axs, (0, half), (half, size)):
names, values = list(reso_names[start:end]), reso_values[start:end]
ax.bar(range(len(values)), values, align='center')
ax.set_xlabel('X-Axis')
ax.set_ylabel('Y-Axis')
ax.set_xticks(range(len(names)))
ax.set_xticklabels(names, rotation='vertical')
ax.set_xlim(0, len(names))
fig.subplots_adjust(bottom=0.05, top=0.95)
plt.show()
我只是希望程序根据分割数N动态地将图形分割成子图。
答案 0 :(得分:2)
您可以使用以下代码直接将带有String(value)
元素的values
/ names
列表分割为size
个size//N + 1
元素列表:
N
请注意,如果N不分割大小,则最后一个子列表将包含更少的元素。
然后你只需执行一个zip并在不同的图表中绘制每个子列表:
N=3
sublists_names = [reso_names[x:x+N] for x in range(0, len(reso_names), N)]
sublists_values = [reso_values[x:x+N] for x in range(0, len(reso_values), N)]
如果列表不能被N分割,您可以取消注释最后一个注释行,以便栏在最后一个子图上保持注释:(import pandas as pd
from matplotlib import rcParams
import matplotlib.pyplot as plt
from operator import itemgetter
rcParams.update({'figure.autolayout': True})
plt.figure(figsize=(14,9), dpi=600)
reso_names = ['A','B','C','D','E','F','G','H']
reso_values = [5,7,3,8,2,9,1,3]
N=3
sublists_names = [reso_names[x:x+N] for x in range(0, len(reso_names), N)]
sublists_values = [reso_values[x:x+N] for x in range(0, len(reso_values), N)]
size = int(len(reso_values))
fig, axs = plt.subplots(nrows=size//N+1, sharey=True, figsize=(14,18), dpi=50)
fig.suptitle('Graph',
**{'family': 'Arial Black', 'size': 22, 'weight': 'bold'})
for ax, names, values in zip(axs, sublists_names, sublists_values):
ax.bar(range(len(values)), values, align='center')
ax.set_xlabel('X-Axis')
ax.set_ylabel('Y-Axis')
ax.set_xticks(range(len(names)))
ax.set_xticklabels(names, rotation='vertical')
ax.set_xlim(0, len(names))
#ax.set_xlim(0, N)
fig.subplots_adjust(bottom=0.05, top=0.95)
plt.show()
):