问题
A scatter plot object has a method called .set_color to update the colors of the markers and .set_offsets to update their position but how can I update the marker shapes?
回答1:
A scatter plot returns a path collection, it contains the paths of the markers. In order to change the marker shape you need to set the path to the new marker path.
For built-in markers, these paths can be obtained from the MarkerStyle class. For custom markers see my SO answer here.
Example - scatter plot with dot markers later changed to plus markers:
from matplotlib import pyplot as plt
from matplotlib.markers import MarkerStyle
sp = plt.scatter([1,2],[1,2], marker='.')
new_marker = MarkerStyle("+")
sp.set_paths((new_marker.get_path(),))
sp.set_sizes([8])
plt.show()
The only caveat is that you need to set the marker size too, by default the new markers are plotted rather big.
来源:https://stackoverflow.com/questions/65415408/update-marker-shape-of-a-scatter-plot