如何使用 seaborn 创建具有连接点的多个系列散点图?
How to create multiple series scatter plot with connected points using seaborn?
我有一组数据存储在 pandas 数据框中。我正在尝试使用 seaborn 的 pointplot() 创建具有连接点的多系列散点图。每个系列都有不同的 (x,y) 值,它们在我的数据框中存储为浮点数。每行都有一个标签,用于区分每个系列。我使用的是 Python 2.7、seaborn 版本 0.5.1 和 matplotlib 版本 1.4.3。
我设法找到的一切都告诉我,我可以通过以下方式实现这一目标:
import matplotlib.pyplot as plt
import seaborn as sns
# Suppose my dataframe is called 'df', with columns 'x', 'y', and 'label'.
sns.pointplot(x = 'x', y = 'y', hue = 'label', data = df)
然而,这会导致一些奇怪的行为:
- 正确识别了颜色,但只有 一些 点连接
- x 轴上的数字重叠,看起来好像每个数据点都标有它的值,而不是用适当的、干净的值对其进行缩放(似乎将 x 数据视为 string/label 而不是浮动)。
我试图通过将我的数据框分成几部分来解决这个问题。这并不理想,因为我可能要同时绘制大约 10 个以上的系列,而且我不希望手动拆分数据:
df1 = df[df.test_type.values == "label 1"]
df2 = df[df.test_type.values == "label 2"]
ax = sns.pointplot(x = 'x',y='y', color = "blue", data = df1)
sns.pointplot(x = 'x', y = 'y', data = df2, color="red", ax = ax)
在这种情况下,所有的点都连接在一起并且它们被适当地着色,但是 x 轴再次表现出非常奇怪的行为。尽管每个数据框中的 x 值不同,但绘图将它们对齐,使它们看起来相同。
现在,我不确定如何 post 我的 output/plots 干净,但我的一些问题可以通过以下方法重现:
#import the necessary modules
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
#Here is some sample data. The 'x2' data is slightly offset from 'x1'
x1 = range(0,100,10)
x2 = range(1,100,10)
x = x1+x2
#The y-values I generate here mimic the general shape of my actual data
y1 = x1[::-1]
y2 = [i+25 for i in x1[::-1]]
y = y1+y2
#Two levels of labels that will be applied to the data
z1 = ["1"]*10
z2 = ["2"]*10
z = z1+z2
#A pandas data frame from the above data
df = pd.DataFrame({'x': x, 'y': y, 'z': z})
#Pointplot using the above data
sns.pointplot(x = 'x', y = 'y', data = df, hue = 'z')
运行 此代码产生以下结果:
- 所有系列中的所有 x 值都均匀。请注意,'x2' 值与由“1”翻译的 'x1' 相同,并且它们在每个系列中以 10 的间隔间隔。我没想到会有这种行为。
- x 轴没有 "clean" 外观刻度。它从字面上标记了每个点对应的 x 值。它正确地标记了点,但没有适当地缩放它。它似乎将 x 值视为标签,类似于条形图的行为方式。
- 点的颜色正确,但没有连接点。
总结一下我的问题:
是否有一种 easier/better/more 优雅的方法来使用存储在 pandas 数据框中的数据绘制具有连接点的多序列散点图? Seaborn 的点图看起来很理想,但它并没有像我预期的那样运行,我怀疑它的用途可能与我需要完成的不同。我愿意接受可以实现此目的的其他解决方案(最好使用 python)。
提前致谢。如果我能弄清楚如何从我的代码上传输出和图表,我会更新我的问题。
我是 100% 的 Whosebug 新手。我很想通过 posting 我的代码生成的图来澄清我的问题,但我无法弄清楚这一点。任何有关如何执行此操作的指示也将不胜感激,因此我可以更新问题。
编辑: 事实证明,seaborn 的点图使用 x 轴作为分类轴,这解释了我上面提到的奇怪行为。有没有办法手动将 x 轴行为从分类更改为数字?这似乎是最简单的方法,但我对 python.
中的微调图不是很熟悉
在@mwaskom 和 this question 的帮助下,我找到了解决我发布的问题的方法:
#Assuming df is a pandas data frame with columns 'x', 'y', and 'label'
for key,grp in df.groupby('label'):
plt.plot(grp.x,grp.y,'o-',label = key)
plt.legend(loc = 'best')
我遇到了类似的问题,最后我使用 Seaborn 的 FacetGrid 解决了它。我使用 plt.scatter 表示点,plt.plot 表示连接点的线。
g = sns.FacetGrid(df, hue="z", size=8)
g.map(plt.scatter, "x", "y")
g.map(plt.plot, "x", "y")
注意,这是在 Seaborn 版本 0.6.0 和版本 0.5.1 中完成的。
我有一组数据存储在 pandas 数据框中。我正在尝试使用 seaborn 的 pointplot() 创建具有连接点的多系列散点图。每个系列都有不同的 (x,y) 值,它们在我的数据框中存储为浮点数。每行都有一个标签,用于区分每个系列。我使用的是 Python 2.7、seaborn 版本 0.5.1 和 matplotlib 版本 1.4.3。
我设法找到的一切都告诉我,我可以通过以下方式实现这一目标:
import matplotlib.pyplot as plt
import seaborn as sns
# Suppose my dataframe is called 'df', with columns 'x', 'y', and 'label'.
sns.pointplot(x = 'x', y = 'y', hue = 'label', data = df)
然而,这会导致一些奇怪的行为:
- 正确识别了颜色,但只有 一些 点连接
- x 轴上的数字重叠,看起来好像每个数据点都标有它的值,而不是用适当的、干净的值对其进行缩放(似乎将 x 数据视为 string/label 而不是浮动)。
我试图通过将我的数据框分成几部分来解决这个问题。这并不理想,因为我可能要同时绘制大约 10 个以上的系列,而且我不希望手动拆分数据:
df1 = df[df.test_type.values == "label 1"]
df2 = df[df.test_type.values == "label 2"]
ax = sns.pointplot(x = 'x',y='y', color = "blue", data = df1)
sns.pointplot(x = 'x', y = 'y', data = df2, color="red", ax = ax)
在这种情况下,所有的点都连接在一起并且它们被适当地着色,但是 x 轴再次表现出非常奇怪的行为。尽管每个数据框中的 x 值不同,但绘图将它们对齐,使它们看起来相同。
现在,我不确定如何 post 我的 output/plots 干净,但我的一些问题可以通过以下方法重现:
#import the necessary modules
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
#Here is some sample data. The 'x2' data is slightly offset from 'x1'
x1 = range(0,100,10)
x2 = range(1,100,10)
x = x1+x2
#The y-values I generate here mimic the general shape of my actual data
y1 = x1[::-1]
y2 = [i+25 for i in x1[::-1]]
y = y1+y2
#Two levels of labels that will be applied to the data
z1 = ["1"]*10
z2 = ["2"]*10
z = z1+z2
#A pandas data frame from the above data
df = pd.DataFrame({'x': x, 'y': y, 'z': z})
#Pointplot using the above data
sns.pointplot(x = 'x', y = 'y', data = df, hue = 'z')
运行 此代码产生以下结果:
- 所有系列中的所有 x 值都均匀。请注意,'x2' 值与由“1”翻译的 'x1' 相同,并且它们在每个系列中以 10 的间隔间隔。我没想到会有这种行为。
- x 轴没有 "clean" 外观刻度。它从字面上标记了每个点对应的 x 值。它正确地标记了点,但没有适当地缩放它。它似乎将 x 值视为标签,类似于条形图的行为方式。
- 点的颜色正确,但没有连接点。
总结一下我的问题:
是否有一种 easier/better/more 优雅的方法来使用存储在 pandas 数据框中的数据绘制具有连接点的多序列散点图? Seaborn 的点图看起来很理想,但它并没有像我预期的那样运行,我怀疑它的用途可能与我需要完成的不同。我愿意接受可以实现此目的的其他解决方案(最好使用 python)。
提前致谢。如果我能弄清楚如何从我的代码上传输出和图表,我会更新我的问题。
我是 100% 的 Whosebug 新手。我很想通过 posting 我的代码生成的图来澄清我的问题,但我无法弄清楚这一点。任何有关如何执行此操作的指示也将不胜感激,因此我可以更新问题。
编辑: 事实证明,seaborn 的点图使用 x 轴作为分类轴,这解释了我上面提到的奇怪行为。有没有办法手动将 x 轴行为从分类更改为数字?这似乎是最简单的方法,但我对 python.
中的微调图不是很熟悉在@mwaskom 和 this question 的帮助下,我找到了解决我发布的问题的方法:
#Assuming df is a pandas data frame with columns 'x', 'y', and 'label'
for key,grp in df.groupby('label'):
plt.plot(grp.x,grp.y,'o-',label = key)
plt.legend(loc = 'best')
我遇到了类似的问题,最后我使用 Seaborn 的 FacetGrid 解决了它。我使用 plt.scatter 表示点,plt.plot 表示连接点的线。
g = sns.FacetGrid(df, hue="z", size=8)
g.map(plt.scatter, "x", "y")
g.map(plt.plot, "x", "y")
注意,这是在 Seaborn 版本 0.6.0 和版本 0.5.1 中完成的。