Update marker shape of a scatter plot

ε祈祈猫儿з 提交于 2021-02-10 14:06:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!