Matplotlib 기본 사용법

2024. 7. 30. 13:24프로그래밍 (확장)/Python-Matplotlib

1. Matplotlib 시작

import matplotlib.pyplot as plt

https://matplotlib.org/stable/users/getting_started/

 

Getting started — Matplotlib 3.9.1 documentation

Check out Plot types to get an overview of the types of plots you can create with Matplotlib.

matplotlib.org

 

2. 기본적인 그래프 그리기

# 데이터 준비
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

# 그래프 그리기
plt.plot(x, y)

# 그래프 표시
plt.show()

https://matplotlib.org/stable/tutorials/introductory/pyplot.html

 

https://matplotlib.org/stable/tutorials/introductory/pyplot.html

 

matplotlib.org

 

3. 그래프 제목 및 라벨 추가

plt.plot(x, y)
plt.title('그래프 제목')
plt.xlabel('x축 라벨')
plt.ylabel('y축 라벨')
plt.show()

https://matplotlib.org/stable/tutorials/text/text_intro.html

 

https://matplotlib.org/stable/tutorials/text/text_intro.html

 

matplotlib.org

 

4. 여러 개의 그래프 그리기

x1 = [1, 2, 3, 4]
y1 = [10, 20, 25, 30]
x2 = [1, 2, 3, 4]
y2 = [30, 25, 20, 10]

plt.plot(x1, y1, label='첫 번째 그래프')
plt.plot(x2, y2, label='두 번째 그래프')
plt.legend()
plt.show()

https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html

 

https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html

 

matplotlib.org

 

5. 스타일 지정

plt.plot(x, y, 'ro-')  # 빨간색 원형 마커와 실선
plt.show()

https://matplotlib.org/stable/gallery/lines_bars_and_markers/line_styles_reference.html

 

6. 막대 그래프

plt.bar(x, y)
plt.show()

https://matplotlib.org/stable/gallery/lines_bars_and_markers/barh.html

 

Horizontal bar chart — Matplotlib 3.9.1 documentation

Horizontal bar chart This example showcases a simple horizontal bar chart. Gallery generated by Sphinx-Gallery

matplotlib.org

 

7. 히스토그램

data = [1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 5, 5, 6, 7, 8, 9]
plt.hist(data, bins=5)
plt.show()

https://matplotlib.org/stable/gallery/statistics/hist.html

 

Histograms — Matplotlib 3.9.1 documentation

Histograms How to plot histograms with Matplotlib. import matplotlib.pyplot as plt import numpy as np from matplotlib import colors from matplotlib.ticker import PercentFormatter # Create a random number generator with a fixed seed for reproducibility rng

matplotlib.org

 

8. 산점도

plt.scatter(x, y)
plt.show()

https://matplotlib.org/stable/gallery/shapes_and_collections/scatter.html

 

Scatter plot — Matplotlib 3.9.1 documentation

Scatter plot This example showcases a simple scatter plot. Gallery generated by Sphinx-Gallery

matplotlib.org

 

9. 서브플롯 (Subplot)

plt.subplot(2, 1, 1)  # 첫 번째 서브플롯 (2행 1열 중 첫 번째)
plt.plot(x, y)
plt.title('첫 번째 서브플롯')

plt.subplot(2, 1, 2)  # 두 번째 서브플롯 (2행 1열 중 두 번째)
plt.plot(y, x)
plt.title('두 번째 서브플롯')

plt.tight_layout()
plt.show()

https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html

 

Creating multiple subplots using plt.subplots — Matplotlib 3.9.1 documentation

Creating multiple subplots using plt.subplots pyplot.subplots creates a figure and a grid of subplots with a single call, while providing reasonable control over how the individual plots are created. For more advanced use cases you can use GridSpec for a m

matplotlib.org

 

10. 저장하기

plt.plot(x, y)
plt.savefig('그래프.png')
plt.show()

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html

 

matplotlib.pyplot.savefig — Matplotlib 3.9.1 documentation

One of 'letter', 'legal', 'executive', 'ledger', 'a0' through 'a10', 'b0' through 'b10'. Only supported for postscript output.

matplotlib.org