Python
Matplotlib Scatter
grace21110
2023. 11. 8. 19:34
반응형
- Creating Scatter Plots
You can use the scatter() function to draw a scatter plot.
Example
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 4, 6, 2])
y = np.array([4, 5, 2, 1])
plt.scatter(x, y)
plt.show()
Result
- Compare plots
Example
import matplotlib.pyplot as plt
import numpy as np
#day one, the age and speed of 13 cars:
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y)
#day two, the age and speed of 15 cars:
x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y)
plt.show()
Result
- Adjusting the color
You can use the c argument to adjust the color.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([2,8,9,1])
y = np.array([5,5,5,5])
colors = np.array(["red","green","blue","magenta"])
plt.scatter(x, y, c=colors)
plt.show()
Result
- Size
Use s argument to adjust the size of the dots.
Example
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]
# Define the size of dots
dot_size = [100, 200, 300, 400, 500]
# Create the scatter plot
plt.scatter(x, y, s=dot_size, label='Dots', c='blue')
# Add labels and a legend
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
# Show the plot
plt.show()
Result
- Alpha
You can adjust the transparency of the dots with the alpha argument
Example
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]
# Define the size of dots
dot_size = [100, 200, 300, 400, 500]
# Create the scatter plot
plt.scatter(x, y, s=dot_size, label='Dots', c='blue', alpha =0.3)
# Add labels and a legend
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
# Show the plot
plt.show()
Result