# Plotting two specific columns from a dataframe on one plot using PlutoPlotly

**URL:** <https://discourse.julialang.org/t/plotting-two-specific-columns-from-a-dataframe-on-one-plot-using-plutoplotly/110316>\
**Category:** Visualization\
**Tags:** plotting, plotlyjs, plutoplotly\
**Created:** [February 16, 2024, 8:06pm UTC](https://discourse.julialang.org/t/plotting-two-specific-columns-from-a-dataframe-on-one-plot-using-plutoplotly/110316 "2024-02-16T20:06:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pex/32/49986_2.png) [@PeX](https://discourse.julialang.org/u/PeX)\
**Post date:** [February 16, 2024, 8:06pm UTC](https://discourse.julialang.org/t/plotting-two-specific-columns-from-a-dataframe-on-one-plot-using-plutoplotly/110316/1 "2024-02-16T20:06:08Z")

</div>

Hi everyone,  
I have a DataFrame with one date data and the other columns are just numbers.  
I want to create a plot of two specific columns `df.data1` and `df.data3` against the date column on the x-axis.  
When I try something like the following:

```julia
p1 = scatter(df.Date, df.data1)
p2 = scatter(df.Date, df.data3)
plot([p1,p2], xaxis_type= "date")

```

I get an error:  
`MethodError: no method matching scatter(::Vector{Dates.Date}, ::Vector{Float64})`  
However If I try to plot just one column like so:

```julia
plot(df.Date, df.data1, xaxis_type= "date")

```

It works!

Is there any other way maybe to add more columns to the plot?

Thank you!

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [February 16, 2024, 8:49pm UTC](https://discourse.julialang.org/t/plotting-two-specific-columns-from-a-dataframe-on-one-plot-using-plutoplotly/110316/2 "2024-02-16T20:49:19Z")

</div>

Plotting with PlutoPlotly is extremely easy if you are not looking for many ingredients in your plots. Moreover, you do not need to create a `Date` object (like you have to in some other plotting packages) if your data frame includes a column with dates that Julia accepts as a type of `Date.`

Suppose you have a data frame like the one in the figure below (USdata.csv).

 ![a](https://global.discourse-cdn.com/julialang/original/3X/9/7/972c44e7b1e1708a2063cb72958db66e0dc57604.png)

Notice that the column `Quarters` is accepted as a `Date` type (you can by typing: `typeof(USdata.Quarters)` and because PlutoPlotly takes into account this information, the plotting is straightforward: For example, dates in the x-axis, and three columns in the y-axis (see first image below):

```julia
plot(USdata.Quarters, [USdata.GDP USdata.INV USdata.M2])

```

If instead you force a column with the name `:Date` to be inserted in the x-axis, but there is no column with that name, the plot will still be produced, but no dates will pop up in such axis (second image below)

```julia
plot(USdata, x = :Date, y = :GDP)

```

 ![aa](https://global.discourse-cdn.com/julialang/original/3X/a/9/a9865a8beb79e4665ea926da378284f733295087.png)

---

<div class="post-metadata">

**Author:** ![PeX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pex/32/49986_2.png) [@PeX](https://discourse.julialang.org/u/PeX)\
**Post date:** [February 16, 2024, 9:17pm UTC](https://discourse.julialang.org/t/plotting-two-specific-columns-from-a-dataframe-on-one-plot-using-plutoplotly/110316/3 "2024-02-16T21:17:54Z")

</div>

Thank you so much for the very informative and helpful reply! Any chance you can share how to label each line? the automatic names `trace 0, trace 1 .. ` are confusing, it’s better to use column names automatically or just manually write the labels.

---

<div class="post-metadata">

**Author:** ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)\
**Post date:** [February 16, 2024, 10:13pm UTC](https://discourse.julialang.org/t/plotting-two-specific-columns-from-a-dataframe-on-one-plot-using-plutoplotly/110316/4 "2024-02-16T22:13:05Z")

</div>

Most common features in a plot are:

```julia
begin
	p1 = plot(USdata.Quarters, [USdata.GDP USdata.INV USdata.M2])
	restyle!(p1, 
		name = ["GDP" "INV" "M2"],
		line_color = ["BlueViolet" "maroon" "navy"]) # any other colors will do
	relayout!(p1, 
		title_text = "My title using relayout but you can do it with Layout", 
		title_x = 0.5, # centers the title
		xaxis_title = "Quarters", yaxis_title = " Billion dollars", 
		height = 450, width = 900) # in PlutoPlotly default will be height=400, width=Pluto window width
	p1
end

```

 ![a](https://global.discourse-cdn.com/julialang/original/3X/1/f/1f81012be70033a2f61eba641f576040a87222a9.png)

There is a large set of examples [here](https://plotly.com/julia/) and [here](https://plotly.com/python/) that we can adapt according to our needs.
