简体   繁体   English

如何在seaborn lineplot上绘制虚线?

[英]How to plot a dashed line on seaborn lineplot?

I'm simply trying to plot a dashed line using seaborn.我只是想用seaborn绘制一条虚线。 This is the code I'm using and the output I'm getting这是我正在使用的代码和我得到的输出

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()

在此处输入图片说明

What am I doing wrong?我究竟做错了什么? Thanks谢谢

It seems that linestyle= argument doesn't work with lineplot() , and the argument dashes= is a bit more complicated than it might seem.似乎lineplot() linestyle=参数不适用于lineplot() ,并且参数dashes=比看起来要复杂一些。

A (relatively) simple way of doing it might be to get a list of the Line2D objects on the plot using ax.lines and then set the linestyle manually:一种(相对)简单的方法可能是使用ax.lines获取绘图上 Line2D 对象的列表,然后手动设置ax.lines

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()

在此处输入图片说明

Update:更新:

It appears the dashes argument applies only when plotting multiple lines (usually using a pandas dataframe).看来dashes参数仅在绘制多条线时适用(通常使用熊猫数据框)。 Dashes are specified the same as in matplotlib, a tuple of (segment, gap) lengths.破折号的指定与 matplotlib 中的相同,这是一个(段,间隙)长度的元组。 Therefore, you need to pass a list of tuples.因此,您需要传递一个元组列表。

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()

在此处输入图片说明

As has been mentioned before, seaborn's lineplot overrides the linestyle based on the style variable, which according to the docs can be a "name of variables in data or vector data ".正如之前提到的,seaborn 的 lineplot 覆盖了基于style变量的线型,根据文档,它可以是“数据或矢量数据中的变量名称”。 Note the second option of directly passing a vector to the style argument.请注意将向量直接传递给style参数的第二个选项。 This allows the following simple trick to draw dashed lines even when plotting only single lines, either when providing the data directly or as dataframe:这允许使用以下简单的技巧来绘制虚线,即使只绘制单条线,无论是直接提供数据还是作为数据框:

If we provide a constant style vector, say style=True , it will be broadcast to all data.如果我们提供一个常量样式向量,比如style=True ,它将被广播到所有数据。 Now we just need to set dashes to the desired dash tuple (sadly, 'simple' dash specifiers such as '--', ':', or 'dotted' are not supported), eg dashes=[(2,2)] :现在我们只需要将dashes设置为所需的破折号元组(遗憾的是,不支持“简单”破折号说明符,例如“--”、“:”或“dotted”),例如dashes=[(2,2)]

import seaborn as sns
import numpy as np
x = np.linspace(0, np.pi, 111)
y = np.sin(x)
sns.lineplot(x, y, style=True, dashes=[(2,2)])

带破折号的简单线图

You are in fact using lineplot the wrong way.实际上,您以错误的方式使用lineplot Your simplified case is more appropriate for matplotlib 's plot function than anything from seaborn .您的简化案例比seaborn任何内容都更适合matplotlibplot函数。 seaborn is more for making the plots more readable with less direct intervention in the script, and generally gets the most mileage when dealing with pandas dataframes seaborn更在脚本使曲线更易读用更少的直接干预,一般得到最多的里程与打交道时pandas dataframes

For example例如

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

n = 100
x = np.linspace(0,2,n)
y1 = np.sin(2*np.pi*x)
y2 = np.sin(4*np.pi*x)
y3 = np.sin(6*np.pi*x)

df = pd.DataFrame(np.c_[y1, y2, y3], index=x)

ax = sns.lineplot(data=df)
plt.show()

yields产量

在此处输入图片说明

As to how to set the styles the way you want for the variables you're trying to show, that I'm not sure how to handle.至于如何以您想要的方式为您尝试显示的变量设置样式,我不确定如何处理。

In the current version of seaborn 0.11.1, your code works perfectly fine.在当前版本的 seaborn 0.11.1 中,您的代码运行良好。

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=x,y=y, linestyle='--')
plt.show();

在此处输入图片说明

While the other answers work, they require a little bit more handiwork.虽然其他答案有效,但它们需要更多的手工。


You can wrap your seaborn plot in an rc_context .您可以将您的 seaborn 图包装在rc_context

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)

with plt.rc_context({'lines.linestyle': '--'}):
    sns.lineplot(x, y)
plt.show()

This results in the following plot.这导致了下图。

绘图结果

If you would like to see other options regarding lines, have a look using the following line.如果您想查看有关线路的其他选项,请使用以下行查看。

[k for k in plt.rcParams.keys() if k.startswith('lines')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM