带有matplotlib散点图的颜色的季节性循环 [英] seaborn cycle through colours with matplotlib scatter
问题描述
在绘制散点图时如何获得 seaborn 颜色?
How can I get seaborn colors when doing a scatter plot?
import matplotlib.pyplot as plt
import seaborn as sns
ax=fig.add_subplot(111)
for f in files:
ax.scatter(args) # all datasets end up same colour
#plt.plot(args) # cycles through palette correctly
推荐答案
您必须告诉matplotlib使用哪种颜色.例如,使用 seaborn 的默认调色板:
You have to tell matplotlib which color to use. To Use, for example, seaborn's default color palette:
import matplotlib.pyplot as plt
import seaborn as sns
import itertools
ax=fig.add_subplot(111)
palette = itertools.cycle(sns.color_palette())
for f in files:
ax.scatter(args, color=next(palette))
itertools.cycle
确保我们不会用完颜色并在使用完最后一个后再次从第一个开始.
The itertools.cycle
makes sure we don't run out of colors and start with the first one again after using the last one.
更新:
根据@Iceflower的评论,通过创建自定义调色板
As per @Iceflower's comment, creating a custom color palette via
palette = sns.color_palette(None, len(files))
可能是更好的解决方案.不同之处在于,我最上面的原始答案会根据需要多次遍历默认颜色,而此解决方案创建的调色板具有与文件一样多的色相.这意味着不会重复任何颜色,但是颜色之间的差异可能非常细微.
might be a better solution. The difference is that my original answer at the top iterates through the default colors as often as it has to, whereas this solution creates a palette with as much hues as there are files. That means that no color is repeated, but the difference between colors might be very subtle.
这篇关于带有matplotlib散点图的颜色的季节性循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!