我只是想使用 seaborn 绘制一条虚线。这是我正在使用的代码和我得到的输出
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
n = 11
x = np.linspace(0,2,n)
y = np.sin(2*np.pi*x)
sns.lineplot(x,y, linestyle='--')
plt.show()
我做错了什么?谢谢
最佳答案
似乎 linestyle=
参数不适用于 lineplot()
,并且参数 dashes=
比看起来。
一个(相对)简单的方法可能是使用 ax.lines
获取绘图上的 Line2D 对象列表,然后手动设置线型:
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
n = 11
x = np.linspace(0,2,n)
y = np.sin(2*np.pi*x)
ax = sns.lineplot(x,y)
# Might need to loop through the list if there are multiple lines on the plot
ax.lines[0].set_linestyle("--")
plt.show()
更新:
看来 dashes
参数仅在绘制多条线时适用(通常使用 pandas 数据框)。破折号的指定与 matplotlib 中的相同,是一个(段,间隙)长度的元组。因此,您需要传递一个元组列表。
n = 100
x = np.linspace(0,4,n)
y1 = np.sin(2*np.pi*x)
y2 = np.cos(2*np.pi*x)
df = pd.DataFrame(np.c_[y1, y2]) # modified @Elliots dataframe production
ax = sns.lineplot(data=df, dashes=[(2, 2), (2, 2)])
plt.show()
关于python - 如何在 seaborn lineplot 上绘制虚线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51963725/