# Plot multiple similar CSVs with AoG

**URL:** <https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279>\
**Category:** Visualization\
**Tags:** algebraofgraphics\
**Created:** [February 8, 2023, 2:32pm UTC](https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279 "2023-02-08T14:32:56Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [February 8, 2023, 2:32pm UTC](https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279/1 "2023-02-08T14:32:56Z")

</div>

I am loading multiple CSV files and want to plot them on top of each other, but I can’t figure out an easy way to change the line color and style to identify them in a legend.

```julia
df1 = DataFrame("X Data" => 1:3, "Unused Data" => 4:6, "Y Data" => 7:9) # CSV.read("file1.csv", DataFrame)
df2 = DataFrame("X Data" => 0:3, "Unused Data" => 4:7, "Y Data" => fill(8,4)) # CSV.read("file2.csv", DataFrame)
draw(
    (data(df1) + data(df2)) *
    mapping("X Data", "Y Data") *
    visual(Lines)
)

```

 ![sample](https://global.discourse-cdn.com/julialang/original/3X/a/7/a754e573c5a41df00ccd68a7cc764e7a4bcfb795.jpeg)

I imagine there should be a way to make line 4 something like

```julia
(data(df1) * mapping(linestyle="Sample 1", color="Sample 1") + data(df2) * mapping(linestyle="Sample 2", color="Sample 2"))

```

but I can’t get the syntax right.

I have six CSVs. I’d like two different line styles and three different colors for grouping them.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [February 8, 2023, 2:39pm UTC](https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279/2 "2023-02-08T14:39:12Z")

</div>

The color attribute is not data dependent. As such it should be passed to the `visual` command, not the `mapping` one.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [February 8, 2023, 3:02pm UTC](https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279/3 "2023-02-08T15:02:14Z")

</div>

I can manually change the visuals, but then I get no legend.

```julia
fig = draw(
    (data(df1) * visual(Lines, color=:red, linestyle=:solid) + data(df2) * visual(Lines, color=:blue, linestyle=:dash)) *
    mapping("X Data", "Y Data")
)

```

 ![sample](https://global.discourse-cdn.com/julialang/original/3X/4/2/4251c97abf66230acf4ef7232b3b31c1b7d19e9a.jpeg)

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [February 8, 2023, 3:33pm UTC](https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279/4 "2023-02-08T15:33:57Z")

</div>

Use `vcat` to make a single dataframe out of all, for dataframes it supports a `src` keyword I think with which you can create a new column in the process which holds the index that each row comes from. This column you can use as the `color` variable in AoG.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 8, 2023, 3:45pm UTC](https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279/5 "2023-02-08T15:45:56Z")

</div>

I think you are referring to the `source` kwarg in `CSV.File`/`CSV.read`:

```julia
  • source: [only applicable for vector of inputs to CSV.File] a Symbol, String, or Pair of Symbol or String to Vector. As a single Symbol or String, provides the column name that will be added to the
       parsed columns, the values of the column will be the input "name" (usually file name) of the input from whence the value was parsed. As a Pair, the 2nd part of the pair should be a Vector of values
       matching the length of the # of inputs, where each value will be used instead of the input name for that inputs values in the auto-added column.

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [February 8, 2023, 3:48pm UTC](https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279/6 "2023-02-08T15:48:49Z")

</div>

This one:

> <https://github.com/JuliaData/DataFrames.jl/blob/4446a3dd227e39b5e2029c4e092e26ee53328cb7/src/abstractdataframe/abstractdataframe.jl#L1629>

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 8, 2023, 4:11pm UTC](https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279/7 "2023-02-08T16:11:33Z")

</div>

Ah right, my recommendation was based on the title - if the data is in CSVs it can be read into a single DataFrame with a source column with CSV, but if the separate DataFrames are desired for other purposes then indeed it might make sense to just vcat them for this.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [February 8, 2023, 8:55pm UTC](https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279/8 "2023-02-08T20:55:52Z")

</div>

Thanks. I actually made use of both suggestions to get my code quite compact.  
The `source` argument to `CSV.read` creates one grouping and the `source` argument to `vcat` creates the other grouping.

```julia
using DataFrames, CSV, CairoMakie, AlgebraOfGraphics

# File Names
original_files = [
    "1432H R models 2018/FirstThreadAxialStress.xls",
    "1432H R models 2018/OldRepairAxialStress.xls",
    "1432H R models 2018/NewRepairAxialStress.xls",
]
new_files = [
    "1432H R models 2023/FirstThreadAxialStress.xls",
    "1432H R models 2023/OldRepairAxialStress.xls",
    "1432H R models 2023/NewRepairAxialStress.xls",
]

# Load and Categorize Data
df = vcat(
    CSV.read.(
        [original_files, new_files],
        DataFrame,
        source = "Path Location" => [
            "First Thread",
            "Old Repair Diameter",
            "New Repair Diameter",
        ]
    )...,
    source = "Model" => ["Original Repair", "New Repair"]
)

# Plot Data
fig = draw(
    data(df)
    * mapping(
        "S (in)" => "Distance from Inner Wall (in)",
        "Normal Stress (psi)" => (x -> x./1000) => "Axial Stress (ksi)",
        linestyle="Model",
        color="Path Location",
    )
    * visual(Lines),
    axis=(; title="Post-Repair Stresses"),
)
save("Post-Repair Stresses.png", fig)
display(fig)

```

 ![Post-Repair Stresses](https://global.discourse-cdn.com/julialang/original/3X/8/9/8975974690df2a9b30e337d939a9ec25bf14d036.png)

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [February 8, 2023, 10:15pm UTC](https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279/9 "2023-02-08T22:15:21Z")

</div>

If I use GLMakie, is there an easy way to save a figure after zooming in with the mouse? `save("file.png", fig)` just saves the original figure.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [February 9, 2023, 11:43am UTC](https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279/10 "2023-02-09T11:43:44Z")

</div>

Hm it probably resets to the set limits when saving, try `limits!(ax, ax.finallimits[])`

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [February 9, 2023, 2:24pm UTC](https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279/11 "2023-02-09T14:24:31Z")

</div>

That worked but is hard to remember.

```julia
# zoom figure where you want it
limits!(fig.figure.current_axis.x, fig.figure.current_axis.x.finallimits[])
save("filename.png", fig)

```

or

```julia
# zoom figure where you want it
limits!(current_axis(), current_axis().finallimits[])
save("filename.png", fig)

```

Why is there an `x` on the end?

```julia
julia> fig.figure.current_axis
Base.RefValue{Any}(Axis (6 plots))

julia> fieldnames(typeof(fig.figure.current_axis))
(:x,)

julia> fig.figure.current_axis.x
Axis with 6 plots:
 ┣━ Lines{Tuple{Vector{Point{2, Float32}}}}
 ┣━ Lines{Tuple{Vector{Point{2, Float32}}}}
 ┣━ Lines{Tuple{Vector{Point{2, Float32}}}}
 ┣━ Lines{Tuple{Vector{Point{2, Float32}}}}
 ┣━ Lines{Tuple{Vector{Point{2, Float32}}}}
 ┗━ Lines{Tuple{Vector{Point{2, Float32}}}}

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [February 9, 2023, 3:13pm UTC](https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279/12 "2023-02-09T15:13:52Z")

</div>

Yeah it’s just a workaround after all. I wonder if it’s a good behavior to reset the axes like this but I assume it comes from the change when we didn’t run autolimits for every new plot anymore.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [February 9, 2023, 3:23pm UTC](https://discourse.julialang.org/t/plot-multiple-similar-csvs-with-aog/94279/13 "2023-02-09T15:23:25Z")

</div>

I found a [GitHub Issue](https://github.com/MakieOrg/Makie.jl/issues/2315) for the `limits!` workaround.

Can you reveal any more information about the `x`? I would think that `current_axis() == fig.figure.current_axis` not `fig.figure.current_axis.x`.
