matplotlib.pyplot.scatter()导入seaborn软件包时变色?

2024-04-25 07:00:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用jupyter笔记本中的matplotlib.pyplot.scatter()生成一些散点图,我发现如果导入seaborn包,散点图就会失去颜色。我想知道有没有人有类似的问题?在

下面是一个示例代码

import matplotlib.pyplot as plt
import seaborn as sb

plt.scatter(range(4),range(4), c=range(4))

输出是

enter image description here

没有seaborn的散点图是:

enter image description here


Tags: 代码import示例matplotlib颜色as笔记本jupyter
2条回答

解决此问题的另一种方法是为plt.scatter()指定cmap选项,这样它就不会受到seaborn的影响:

ax = plt.scatter(range(4),range(4), c=range(4), cmap='gist_rainbow')
plt.colorbar(ax)

结果是: enter image description here

此处cmap有许多选项:

http://matplotlib.org/examples/color/colormaps_reference.html

这似乎就是它的行为方式。在seaborn 0.3中,默认颜色比例更改为灰度。如果您将代码更改为:

plt.scatter(range(4),range(4), c=sb.color_palette())

您将得到一个颜色与您的原始图像相似的图像。 有关更多信息,请参阅choosing color palettes上的Seaborn文档。在

相关问题 更多 >