Python 如何使用 seaborn 创建多线图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52308749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:04:20  来源:igfitidea点击:

How do I create a multiline plot using seaborn?

pythonpython-3.xdataframeplotseaborn

提问by SSPdude

I am trying out Seaborn to make my plot visually better than matplotlib. I have a dataset which has a column 'Year' which I want to plot on the X-axis and 4 Columns say A,B,C,D on the Y-axis using different coloured lines. I was trying to do this using the sns.lineplot method but it allows for only one variable on the X-axis and one on the Y-axis. I tried doing this

我正在尝试 Seaborn 使我的情节在视觉上比 matplotlib 更好。我有一个数据集,它有一个“Year”列,我想在 X 轴上绘制它,4 列使用不同颜色的线在 Y 轴上表示 A、B、C、D。我试图使用 sns.lineplot 方法来做到这一点,但它只允许 X 轴上的一个变量和 Y 轴上的一个变量。我试过这样做

sns.lineplot(data_preproc['Year'],data_preproc['A'], err_style=None)
sns.lineplot(data_preproc['Year'],data_preproc['B'], err_style=None)
sns.lineplot(data_preproc['Year'],data_preproc['C'], err_style=None)
sns.lineplot(data_preproc['Year'],data_preproc['D'], err_style=None)

But this way I don't get a legend in the plot to show which coloured line corresponds to what. I tried checking the documentation but couldn't find a proper way to do this.

但是这样我就不会在图中得到一个图例来显示哪个彩色线对应于什么。我尝试检查文档,但找不到正确的方法来执行此操作。

回答by dnswlt

Seaborn favors the "long format" as input. The key ingredient to convert your DataFrame from its "wide format" (one column per measurement type) into long format (one column for all measurement values, one column to indicate the type) is pandas.melt. Given a data_preprocstructured like yours, filled with random values:

Seaborn 倾向于使用“长格式”作为输入。将 DataFrame 从其“宽格式”(每个测量类型一列)转换为长格式(一列用于所有测量值,一列用于指示类型)的关键因素是pandas.melt。给定一个data_preproc像你这样的结构,填充随机值:

num_rows = 20
years = list(range(1990, 1990 + num_rows))
data_preproc = pd.DataFrame({
    'Year': years, 
    'A': np.random.randn(num_rows).cumsum(),
    'B': np.random.randn(num_rows).cumsum(),
    'C': np.random.randn(num_rows).cumsum(),
    'D': np.random.randn(num_rows).cumsum()})

A single plot with four lines, one per measurement type, is obtained with

具有四条线的单个图,每种测量类型一条,通过以下方式获得

sns.lineplot(x='Year', y='value', hue='variable', 
             data=pd.melt(data_preproc, ['Year']))

(Note that 'value' and 'variable' are the default column names returned by melt, and can be adapted to your liking.)

(请注意,'value' 和 'variable' 是由 返回的默认列名melt,可以根据您的喜好进行调整。)

回答by IonicSolutions

See the documentation:

请参阅文档

sns.lineplot(x="Year", y="signal", hue="label", data=data_preproc)

You probably need to re-organize your dataframe in a suitable way so that there is one column for the xdata, one for the ydata, and one which holds the label for the data point.

您可能需要以合适的方式重新组织数据框,以便一列用于x数据,一列用于y数据,另一列用于保存数据点的标签。

You can also just use matplotlib.pyplot. If you import seaborn, much of the improved design is also used for "regular" matplotlib plots. Seaborn is really "just" a collection of methods which conveniently feed data and plot parameters to matplotlib.

您也可以只使用matplotlib.pyplot. 如果您 import seaborn,大部分改进的设计也用于“常规” matplotlib 图。Seaborn 实际上“只是”一组方法,可以方便地将数据和绘图参数提供给 matplotlib。

回答by WolvhLorien

This:

这个:

sns.lineplot(data=data_preproc)

will do what you want.

会做你想做的。