Python 使用 seaborn 的简单线图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31069191/
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
Simple line plots using seaborn
提问by Titus Pullo
I'm trying to plot a ROC curve using seaborn (python).
With matplotlib I simply use the function plot
:
我正在尝试使用 seaborn (python) 绘制 ROC 曲线。使用 matplotlib 我只需使用函数plot
:
plt.plot(one_minus_specificity, sensitivity, 'bs--')
where one_minus_specificity
and sensitivity
are two lists of paired values.
其中one_minus_specificity
和sensitivity
是两个配对值列表。
Is there a simple counterparts of the plot function in seaborn? I had a look at the gallery but I didn't find any straightforward method.
seaborn 中是否有简单的 plot 函数对应物?我查看了图库,但没有找到任何简单的方法。
采纳答案by hitzg
Since seaborn also uses matplotlib to do its plotting you can easily combine the two. If you only want to adopt the styling of seaborn the set_style
function should get you started:
由于 seaborn 还使用 matplotlib 进行绘图,因此您可以轻松地将两者结合起来。如果您只想采用 seaborn 的样式,该set_style
函数应该可以帮助您入门:
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
sns.set_style("darkgrid")
plt.plot(np.cumsum(np.random.randn(1000,1)))
plt.show()
Result:
结果:
回答by mikkokotila
Yes, you can do the same in Seaborn directly. This is done with tsplot() which allows either a single array as input, or two arrays where the other is 'time' i.e. x-axis.
是的,您可以直接在 Seaborn 中执行相同操作。这是通过 tsplot() 完成的,它允许单个数组作为输入,或者两个数组,其中另一个是“时间”,即 x 轴。
import seaborn as sns
data = [1,5,3,2,6] * 20
time = range(100)
sns.tsplot(data, time)
回答by kmario23
It's possible to get this done using seaborn.lineplot()
but it involves some additional work of converting numpy arrays to pandas dataframe. Here's a complete example:
可以使用seaborn.lineplot()
它来完成这项工作,但它涉及将 numpy 数组转换为 Pandas 数据帧的一些额外工作。这是一个完整的例子:
# imports
import seaborn as sns
import numpy as np
import pandas as pd
# inputs
In [41]: num = np.array([1, 2, 3, 4, 5])
In [42]: sqr = np.array([1, 4, 9, 16, 25])
# convert to pandas dataframe
In [43]: d = {'num': num, 'sqr': sqr}
In [44]: pdnumsqr = pd.DataFrame(d)
# plot using lineplot
In [45]: sns.set(style='darkgrid')
In [46]: sns.lineplot(x='num', y='sqr', data=pdnumsqr)
Out[46]: <matplotlib.axes._subplots.AxesSubplot at 0x7f583c05d0b8>
And we get the following plot:
我们得到以下图: