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

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:

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:

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!

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).

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):

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)

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

2 Likes

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.

Most common features in a plot are:

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

There is a large set of examples here and here that we can adapt according to our needs.

1 Like