Use scatter to draw a scatter plot. Some examples are shown below.
Below is an example of the simplest scatter plot.
import numpy as np
import matplotlib.pyplot as plt
# generate data
x = np.random.rand(100)
y = np.random.rand(100)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x,y)
ax.set_title('first scatter plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
fig.show()
You can change the color by specifying c ='red' in the parameter.
import numpy as np
import matplotlib.pyplot as plt
# generate data
x1 = np.random.rand(100)*0.5
y1 = np.random.rand(100)
x2 = np.random.rand(100)*0.5 + 0.5
y2 = np.random.rand(100)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x1,y1, c='red')
ax.scatter(x2,y2, c='blue')
ax.set_title('second scatter plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
fig.show()
The same result is obtained even if specified in RGB. At this time, specify a value of 0.0 --1.0.
ax.scatter(x1,y1, c=(1.0,0,0))
ax.scatter(x2,y2, c=(0, 0, 1.0))
The legend uses legend. You can change the display position with an argument. If you want to draw a grid line, use grid (True).
position |
---|
upper right |
upper left |
lower left |
lower right |
right |
center left |
center right |
lower center |
upper center |
center |
import numpy as np
import matplotlib.pyplot as plt
# generate data
x1 = np.random.rand(100)*0.5
y1 = np.random.rand(100)*0.5
x2 = np.random.rand(100)*0.5 + 0.5
y2 = np.random.rand(100)*0.5
x3 = np.random.rand(100)*0.5
y3 = np.random.rand(100)*0.5 + 0.5
x4 = np.random.rand(100)*0.5 + 0.5
y4 = np.random.rand(100)*0.5 + 0.5
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x1,y1, c='red', label='group1')
ax.scatter(x2,y2, c='blue', label='group2')
ax.scatter(x3,y3, c='green', label='group3')
ax.scatter(x4,y4, c='yellow', label='group4')
ax.set_title('third scatter plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)
ax.legend(loc='upper left')
fig.show()
Markers are specified as marker ='o'. Four typical markers were used as an example. There are many others. See here.
import numpy as np
import matplotlib.pyplot as plt
# generate data
x1 = np.random.rand(100)*0.5
y1 = np.random.rand(100)*0.5
x2 = np.random.rand(100)*0.5 + 0.5
y2 = np.random.rand(100)*0.5
x3 = np.random.rand(100)*0.5
y3 = np.random.rand(100)*0.5 + 0.5
x4 = np.random.rand(100)*0.5 + 0.5
y4 = np.random.rand(100)*0.5 + 0.5
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x1,y1, c='red', marker='.', label='group1')
ax.scatter(x2,y2, c='blue',marker='o', label='group2')
ax.scatter(x3,y3, c='green',marker='^', label='group3')
ax.scatter(x4,y4, c='yellow',marker='s', label='group4')
ax.set_title('fourth scatter plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)
ax.legend(loc='upper left')
fig.show()
The size of the marker is a parameter such as s = 20. The default size is 20.
import numpy as np
import matplotlib.pyplot as plt
# generate data
x1 = np.random.rand(100)*0.5
y1 = np.random.rand(100)*0.5
x2 = np.random.rand(100)*0.5 + 0.5
y2 = np.random.rand(100)*0.5
x3 = np.random.rand(100)*0.5
y3 = np.random.rand(100)*0.5 + 0.5
x4 = np.random.rand(100)*0.5 + 0.5
y4 = np.random.rand(100)*0.5 + 0.5
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x1,y1, c='red', s=20, marker='o', label='group1')
ax.scatter(x2,y2, c='blue',s=40, marker='o', label='group2')
ax.scatter(x3,y3, c='green',s=80, marker='o', label='group3')
ax.scatter(x4,y4, c='yellow',s=120, marker='o', label='group4')
ax.set_title('fifth scatter plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True)
ax.legend(loc='upper left')
fig.show()
Recommended Posts