Matplotlib로 테이블을 만드는 방법


Matplotlib를 사용하여 Python에서 테이블을 생성하려면 다음 두 가지 방법 중 하나를 사용할 수 있습니다.

방법 1: Pandas DataFrame에서 테이블 만들기

 #create pandas DataFrame
df = pd.DataFrame(np. random . randn (20, 2), columns=[' First ', ' Second '])

#create table
table = ax. table (cellText=df. values , colLabels=df. columns , loc=' center ')

방법 2: 사용자 정의 값으로 배열 만들기

 #create values for table
table_data=[
    ["Player 1", 30],
    ["Player 2", 20],
    ["Player 3", 33],
    ["Player 4", 25],
    ["Player 5", 12]
]

#create table
table = ax. table (cellText=table_data, loc=' center ')

이 튜토리얼에서는 이러한 방법을 실제로 사용하는 방법에 대한 예를 제공합니다.

예시 1: Pandas DataFrame에서 테이블 만들기

다음 코드는 Pandas DataFrame의 값을 포함하는 Matplotlib에서 테이블을 생성하는 방법을 보여줍니다.

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

#make this example reproducible
n.p. random . seeds (0)

#define figure and axes
fig, ax = plt. subplots ()

#hide the axes
fig.patch. set_visible (False)
ax.axis(' off ')
ax.axis(' tight ')

#createdata
df = pd.DataFrame(np. random . randn (20, 2), columns=[' First ', ' Second '])

#create table
table = ax. table (cellText=df.values, colLabels=df.columns, loc=' center ')

#display table
fig. tight_layout ()
plt. show () 

Matplotlib 테이블

예 2: 사용자 정의 값으로 테이블 생성

다음 코드는 사용자 정의 값을 포함하는 Matplotlib에서 테이블을 생성하는 방법을 보여줍니다.

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

#define figure and axes
fig, ax = plt. subplots ()

#create values for table
table_data=[
    ["Player 1", 30],
    ["Player 2", 20],
    ["Player 3", 33],
    ["Player 4", 25],
    ["Player 5", 12]
]

#create table
table = ax. table (cellText=table_data, loc=' center ')

#modify table
table. set_fontsize (14)
table. scale (1.4)
ax. axis (' off ')

#displaytable
plt. show () 

사용자 정의 값이 있는 matplotlib 테이블

table.scale(width, length)는 테이블의 너비와 길이를 수정합니다. 예를 들어, 길이를 변경하여 테이블을 더 길게 만들 수 있습니다.

 table. scale (1.10)

matplotlib의 테이블

추가 리소스

Matplotlib 플롯에 텍스트를 추가하는 방법
Matplotlib에서 종횡비를 설정하는 방법
Matplotlib에서 범례 글꼴 크기를 변경하는 방법

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다