I think that the matplot legend should be created automatically in most cases, but since irregularities have occurred, make a note of how to set it manually.
Perhaps this is sufficient in most cases, and you should organize your data so that you can do this.
import numpy as np
from matplotlib import pyplot as plt
red_x, red_y = np.random.randn(10), np.random.randn(10)
blue_x, blue_y = np.random.randn(10), np.random.randn(10)
green_x, green_y = np.random.randn(10), np.random.randn(10)
plt.scatter(red_x, red_y, c="r", alpha=0.5, label="red")
plt.scatter(blue_x, blue_y, c="b", alpha=0.5, label="blue")
plt.scatter(green_x, green_y, c="g", alpha=0.5, label="green")
plt.legend()
plt.show()
If you are dealing with special situations or special data and cannot make the above code, or if the code becomes dirty, you can set it manually as follows.
import numpy as np
from matplotlib import pyplot as plt
red_x, red_y = np.random.randn(10), np.random.randn(10)
blue_x, blue_y = np.random.randn(10), np.random.randn(10)
green_x, green_y = np.random.randn(10), np.random.randn(10)
#Remove the label from the data part
plt.scatter(red_x, red_y, c="r", alpha=0.5)
plt.scatter(blue_x, blue_y, c="b", alpha=0.5)
plt.scatter(green_x, green_y, c="g", alpha=0.5)
#Plot empty data with label for legend (actually nothing is plotted)
plt.scatter([], [], c="r", alpha=0.5, label="red")
plt.scatter([], [], c="b", alpha=0.5, label="blue")
plt.scatter([], [], c="g", alpha=0.5, label="green")
plt.legend()
plt.show()
It seems that it can be used when changing the transparency and the size of dots only in the legend part.
Recommended Posts