matplotlib를 사용하여 테이블을 X축에 정렬하려면 다음 단계를 수행할 수 있습니다.
단계
-
Figure 크기를 설정하고 서브플롯 사이와 주변의 패딩을 조정합니다.
-
데이터 프레임에 대한 데이터 목록을 만듭니다.
-
데이터 프레임의 열에 대한 튜플 만들기 .
-
행 목록 만들기 및 가치 .
-
변수 value_increment 초기화 .
-
데이터 포인트로 막대 그림을 만듭니다.
-
데이터를 테이블에 넣습니다.
-
y-label, yticks, xticks, 설정 및 제목 줄거리.
-
그림을 표시하려면 Show()를 사용하세요. 방법.
예
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
data = [
[66386, 174296, 75131, 577908, 32015],
[58230, 381139, 78045, 99308, 160454],
[89135, 80552, 152558, 497981, 603535],
[78415, 81858, 150656, 193263, 69638],
[139361, 331509, 343164, 781380, 52269]
]
columns = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail')
rows = ['%d year' % x for x in (100, 50, 20, 10, 5)]
values = np.arange(0, 2500, 500)
value_increment = 1000
colors = plt.cm.BuPu(np.linspace(0, 0.5, len(rows)))
n_rows = len(data)
index = np.arange(len(columns)) + 0.3
bar_width = 0.4
y_offset = np.zeros(len(columns))
cell_text = []
for row in range(n_rows):
plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])
y_offset = y_offset + data[row]
cell_text.append(['%1.1f' % (x / 1000.0) for x in y_offset])
colors = colors[::-1]
cell_text.reverse()
the_table = plt.table(cellText=cell_text,
rowLabels=rows,
rowColours=colors,
colLabels=columns,
loc='bottom')
plt.subplots_adjust(left=0.2, bottom=0.2)
plt.ylabel("Loss in ${0}'s".format(value_increment))
plt.yticks(values * value_increment, ['%d' % val for val in values])
plt.xticks([])
plt.title('Loss by Disaster')
plt.show() 출력
다음 출력을 생성합니다 -
