Last active
June 22, 2025 04:57
-
-
Save partrita/951579c2e7330958a27bf4d2acc86293 to your computer and use it in GitHub Desktop.
line_plot_errorbar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import matplotlib.pyplot as plt | |
| def plot_data_with_errorbar(ax, x_data, y_data, y_error, fmt, label): | |
| """ | |
| 주어진 데이터를 사용하여 errorbar 그래프를 그립니다. | |
| 매개변수: | |
| ax (matplotlib.axes.Axes): 그림을 그릴 Axes 객체. | |
| x_data (list): X축 데이터. | |
| y_data (list): Y축 데이터. | |
| y_error (list): Y축 에러 바 데이터. | |
| fmt (str): 선 스타일 및 마커 포맷 문자열 (예: "ro-"). | |
| label (str): 범례에 표시될 레이블. | |
| """ | |
| ax.errorbar( | |
| x_data, | |
| y_data, | |
| yerr=y_error, | |
| fmt=fmt, | |
| linewidth=2, | |
| elinewidth=0.5, | |
| ecolor='k', | |
| capsize=3, | |
| capthick=0.5, | |
| label=label # 범례를 위해 label 추가 | |
| ) | |
| def main(): | |
| """ | |
| Matplotlib 그래프를 생성하고 표시하는 메인 함수입니다. | |
| """ | |
| # --- 데이터 정의 --- | |
| # 각 라인에 대한 데이터를 딕셔너리 리스트로 구조화 | |
| plot_series = [ | |
| { | |
| 'x': [0.3, 1, 2, 3], | |
| 'y': [1000, 400, 90, 16], | |
| 'yerr': [54, 12, 41, 4], | |
| 'fmt': "ro-", | |
| 'label': "Series 1 (Red)" | |
| }, | |
| { | |
| 'x': [0.3, 1, 2, 3], | |
| 'y': [1120, 340, 49, 46], | |
| 'yerr': [134, 124, 21, 9], | |
| 'fmt': "bo-", | |
| 'label': "Series 2 (Blue)" | |
| }, | |
| { | |
| 'x': [0.3, 1, 2, 3], | |
| 'y': [619, 674, 359, 126], | |
| 'yerr': [44, 34, 21, 13], | |
| 'fmt': "ko-", | |
| 'label': "Series 3 (Black)" | |
| } | |
| ] | |
| # --- 그래프 설정 및 그리기 --- | |
| fig, ax = plt.subplots(figsize=(8, 6)) # Figure와 Axes 객체 생성 | |
| for series in plot_series: | |
| plot_data_with_errorbar( | |
| ax, | |
| series['x'], | |
| series['y'], | |
| series['yerr'], | |
| series['fmt'], | |
| series['label'] | |
| ) | |
| # --- 그래프 추가 설정 --- | |
| ax.set_ylabel('Concentration (ng/ml)') | |
| ax.set_xlabel('Hours') | |
| ax.set_title('Python Plot: Concentration vs. Time') | |
| ax.set_yscale('log') # Y축 스케일을 로그로 설정 | |
| ax.legend() # 범례 표시 | |
| # 주석 처리된 설정 (필요에 따라 주석 해제하여 사용) | |
| # ax.set_xlim((0.2, 3.5)) | |
| # ax.set_ylim((10, 1200)) | |
| # ax.set_xticks([0.5, 1, 1.5, 2, 2.5, 3]) | |
| # ax.set_yticks([10, 100, 1000]) | |
| # --- 그래프 표시 --- | |
| plt.grid(True, which="both", ls="--", c='0.7') # 그리드 추가 | |
| plt.tight_layout() # 레이아웃 자동 조정 | |
| plt.show() | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment