sns.set_style() 命令不适用于我的代码,请协助

问题描述 投票:0回答:2

我尝试在代码中使用seaborn包来散点图复数,但即使在调用sns.set_style()之后我也没有得到任何输出。请帮忙。

from matplotlib import pyplot as plt
import math
import seaborn as sns
s = ([1 + 1j, 1 + 69j, 2 + 1j, 2 + 2j,4.5 - 6.4j])  # s is a list which contains all the complex numbers which r to be plotted
                  
            x = [x.real for x in s]
            y = [y.imag for y in s]
            plt.plot(x, y, 'o', c='red')
            plt.grid(True)
            plt.xlabel('Real number')
            plt.ylabel('Imaginary number')
            plt.title('Graph of given complex numbers')
            sns.set_style("dark")
            plt.show()```
python matplotlib numbers seaborn
2个回答
0
投票

请在#sns.set_style#之前添加“sns.set_theme()” 从 matplotlib 导入 pyplot 作为 plt 导入数学 将seaborn导入为sns s = ([1 + 1j, 1 + 69j, 2 + 1j, 2 + 2j,4.5 - 6.4j]) # s 是一个列表,其中包含要绘制 r 的所有复数

        x = [x.real for x in s]
        y = [y.imag for y in s]
        plt.plot(x, y, 'o', c='red')
        plt.grid(True)
        plt.xlabel('Real number')
        plt.ylabel('Imaginary number')
        plt.title('Graph of given complex numbers')
        sns.set_theme() # to make style changable from defaults use this line of code befor using set_style
        sns.set_style("dark")
        plt.show()

我希望现在可以运行


0
投票

我在这里遇到了同样的问题。解决的问题是移动到调用

sns.set_style()
的地方。
sns.set_style()
应在任何
plt.
调用之前调用。

重构你的代码:

from matplotlib import pyplot as plt
import math
import seaborn as sns
s = ([1 + 1j, 1 + 69j, 2 + 1j, 2 + 2j,4.5 - 6.4j])  # s is a list which contains all the complex numbers which r to be plotted
                  
x = [x.real for x in s]
y = [y.imag for y in s]

sns.set_style("dark") # <--- Moving it here fixes it

plt.plot(x, y, 'o', c='red')
plt.grid(True)
plt.xlabel('Real number')
plt.ylabel('Imaginary number')
plt.title('Graph of given complex numbers')

plt.show()

下面有两个情节,一个在脚本的开头设置样式,一个在脚本的结尾。

This is the plot using the sns_set_style() in the end of the script

This is the plot  using the sns_set_style() in the beginning of the script

© www.soinside.com 2019 - 2024. All rights reserved.