如何根据数据集中某列的值制作散点图?

Key*_*ter 5 dataframe julia ijulia-notebook

我得到了一个看起来像这样的数据集

数据

并且我试图将第一列上所有带有 1 的点与带有 0 的点分开,但我想将它们放在同一个图表中。

我知道最终结果应该与此类似 在此处输入图片说明

但是我找不到过滤 Julia 中的点的方法。我在我的项目中使用了 LinearAlgebra、CSV、Plots、DataFrames,到目前为止我还没有找到一种方法来使 DataFrames 存储类型与 Plots 函数很好地协同工作。我不断Cannot convert Float64 to series data for plotting遇到错误,例如当我尝试使用 for 循环作为过滤器单独绘制点时,如下面的代码所示

filter = select(data, :1)
newData = select(data, 2:3)

#graph one initial point to create the plot
plot(newData[1,1], newData[1,2], seriestype = :scatter, title = "My Scatter Plot")

#add the additional points with the 1 in front
for i in 2:size(newData)
    if filter[i] == 1
        plot!(newData[i, 1], newData[i, 2], seriestype = :scatter, title = "My Scatter Plot")
    end
end

Run Code Online (Sandbox Code Playgroud)

其他方法给了我其他错误,但我没有记录这些错误。

我正在使用 Julia 1.4.0 和所有提到的包的最新版本。

快速编辑:

知道我正在尝试复制本文的非线性降维部分可能会有所帮助https://sebastianraschka.com/Articles/2014_kernel_pca.html#principal-component-analysis

Bog*_*ski 5

使用 Plots.jl,您可以执行以下操作(我正在传递一个完全可重现的代码):

julia> df = DataFrame(c=rand(Bool, 100), x = 2 .* rand(100) .- 1);

julia> df.y = ifelse.(df.c, 1, -1) .* df.x .^ 2;

julia> plot(df.x, df.y, color=ifelse.(df.c, "blue", "red"), seriestype=:scatter, legend=nothing)
Run Code Online (Sandbox Code Playgroud)

但是,在这种情况下,我会另外使用 StatsPlots.jl,因为您可以这样写:

julia> using StatsPlots

julia> @df df plot(:x, :y, group=:c, seriestype=:scatter, legend=nothing)
Run Code Online (Sandbox Code Playgroud)

如果您想按组手动执行此操作,则最容易使用该groupby功能:

julia> gdf = groupby(df, :c);

julia> summary(gdf) # check that we have 2 groups in data
"GroupedDataFrame with 2 groups based on key: c"

julia> plot(gdf[1].x, gdf[1].y, seriestype=:scatter, legend=nothing)

julia> plot!(gdf[2].x, gdf[2].y, seriestype=:scatter)
Run Code Online (Sandbox Code Playgroud)

请注意,gdf变量绑定到一个GroupedDataFrame对象,:c在这种情况下,您可以从中获取由分组列 ( )定义的组。