Data Science Explorer

Matplotlib: Matplotlib Plotting 본문

Python

Matplotlib: Matplotlib Plotting

grace21110 2023. 11. 4. 11:01
반응형

The plot() function is used to draw points (markers) in a diagram. 

Parameter 1 is an array containing the points on the x-axis. 

Parameter 2 is an array containing the points on the y-axis. 

 

  • Plotting Without Line 

You can use shortcut string notation parameter 'o', which means rings. 

Example 

Draw two points in the diagram, one at position (1,1) and one in position (6,10).

# Draw two points in the diagram, one at position (1,1) and one in position (6,10).

import matplotlib.pyplot as plt 
import numpy as np 

xpoints = np.array ([1,1])
ypoints = np.array([6,10])

plt.plot (xpoints, ypoints, 'o')
plt.show()

 

Result

 

 

  • Multiple Points 

Example 

Draw a line in a diagram from position (1, 1) to (2, 8) then to (8, 2) and finally to position (9, 10).

#Draw a line in a diagram from position (1, 1) to (2, 8) then to (8, 2) and finally to position (9, 10).
import matplotlib.pyplot as plt 
import numpy as np 

xpoints = np.array([1, 2, 8, 9])
ypoints = np.array ([1, 8, 2, 10])

plt.plot(xpoints, ypoints) 

plt.show()

 

Result 

 

  • Default X points 

If you don't specify the xpoint, it will default the xpoint depending on the length of the y points. 

Example 

Plotting without x-points:

import matplotlib.pyplot as plt 
import numpy as np 

ypoints = np.array ([2, 4, 6, 8]) 

plt.plot(ypoints)
plt.show()

 

Result

 

 

Exercise

Draw a line in a diagram from position (2, 3) to (5, 5) and then to (7, 1) and finally to position (9, 9).

import matplotlib.pyplot as plt
import numpy as np 

xpoints = ([2, 5, 7, 9]) 
ypoints = ([3, 5, 1, 9])

plt.plot(xpoints, ypoints)
plt.show()

 

Result

'Python' 카테고리의 다른 글

Matplotlib: Matplotlib Labels and Title  (0) 2023.11.05
Matplotlib: Matplotlib Markers  (2) 2023.11.04
Matplotlib: Matplotlib Pyplot  (0) 2023.11.04
Matplotlib  (0) 2023.11.04
Pandas: Removing Duplicates  (0) 2023.10.30