Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Matplotlib를 사용하여 두 개의 점선을 그리고 마커를 설정하는 방법은 무엇입니까?

<시간/>

이 프로그램에서 우리는 matplot 라이브러리를 사용하여 두 개의 라인을 그릴 것입니다. 코딩을 시작하기 전에 먼저 다음 명령을 사용하여 matplotlib 라이브러리를 가져와야 합니다 -

Import matplotlib.pyplot as plt

Pyplot은 matplotlib가 MATLAB처럼 작동하도록 하는 명령 스타일 함수의 모음입니다.

알고리즘

Step 1: Import matplotlib.pyplot
Step 2: Define line1 and line2 points.
Step 3: Plot the lines using the plot() function in pyplot.
Step 4: Define the title, X-axis, Y-axis.
Step 5: Display the plots using the show() function.

예시 코드

import matplotlib.pyplot as plt

line1_x = [10,20,30]
line1_y = [20,40,10]

line2_x = [10,20,30]
line2_y= [40,10,30]

plt.xlabel('X AXIS')
plt.ylabel('Y AXIS')

plt.plot(line1_x ,line1_y , color='blue', linewidth = 3, label = 'line1-dotted',linestyle='dotted')
plt.plot(line2_x ,line2_y, color='red', linewidth = 5, label = 'line2-dashed', linestyle='dotted')

plt.title("PLOTTING DOTTED LINES")
plt.legend()

plt.show()

출력

Matplotlib를 사용하여 두 개의 점선을 그리고 마커를 설정하는 방법은 무엇입니까?

설명

변수 line1_x, line_y 및 line2_x, line2_y는 우리 선의 좌표입니다. plot 함수의 linewidth 매개변수는 기본적으로 우리가 그리는 선의 너비/두께입니다. 프로그램의 plt.legend() 함수는 x축, y축 이름과 같은 범례를 그래프에 배치하는 데 사용됩니다.