Plotting multiple variables

Introduction to Data Visualization with Julia

Gustavo Vieira Suñe

Data Analyst

Exploring our dataset

  • Invesco QQQ Trust historical prices
price_date open high low close volume
2020-01-02 214.4 216.16 213.98 216.16 30969400
2020-01-03 213.3 215.47 213.28 214.18 27518900
2020-01-06 212.5 215.59 212.24 215.56 21655300

 

  • Define volatility
qqq[!, "volatility"] =  100 * (qqq.high - qqq.low) ./ qqq.open
Introduction to Data Visualization with Julia

Customizing a scatter plot

# Plot volume versus volatility
scatter(
    qqq.volatility,
    qqq.volume,

title="QQQ Daily Traded Volume", xlabel="Daily Range (%)", ylabel="Volume",
# Hide the legend label=false
# Change marker color markercolor=:ivory2 )

A scatter plot of the daily traded volume versus the volatility for the QQQ ETF fund. The graph shows that an increase in volatility leads to an increase in traded volume.

Commonly used colors: :blue, :red, :green, :yellow, :black, :gray, :white

1 http://juliagraphics.github.io/Colors.jl/stable/namedcolors/
Introduction to Data Visualization with Julia

Exclamation mark notation

# Plot volume versus volatility
scatter(qqq.volatility,
    qqq.volume,
    label=false
    markercolor=:ivory2
)

# Add title and axis labels title!("QQQ Daily Traded Volume") xlabel!("Daily Range (%)") ylabel!("Volume")

A scatter plot of the daily traded volume versus the volatility for the QQQ ETF fund. The graph shows that an increase in volatility leads to an increase in traded volume.

Introduction to Data Visualization with Julia

Correlation

  • Relationship between variables

Three scatter plots displaying two variables that are either positively, negatively or not correlated. The corresponding regression lines are also shown in each plot.

Introduction to Data Visualization with Julia

Adding a regression line

# Plot volume versus volatility
scatter(qqq.volatility,
    qqq.volume,
    title="QQQ Daily Traded Volume"
    xlabel="Daily Range (%)",
    ylabel="Volume",
    label=false
    markercolor=:ivory2,

# Add regression line smooth=true,
# Customize regression line linewidth=2.5, linecolor=:magenta3, )

A scatter plot of the daily traded volume versus the volatility for the QQQ ETF fund with a regression line added. The positive slope of the regression line indicates a positive correlation between traded volume and volatility.

Introduction to Data Visualization with Julia

Multiple line plots

# Plot high and low prices
plot(
    qqq.price_date,
    # Pass both variables
    [qqq.high qqq.low],

# Assign both labels label=["High" "Low"],
# Assign widths linewidth=[4 2] ) title!("QQQ Daily Prices") xlabel!("Date") ylabel!("Price")

Lines plots of the daily high and low prices for the QQQ fund as a function of time for a period of fifty days. The prices exhibit fluctuations around a positive linear trend.

Introduction to Data Visualization with Julia

Multiple plots done differently

# Plot high price versus time
plot(qqq.price_date,
    qqq.high,
    label="High",
    linewidth=4)

# Add low prices to the figure plot!(qqq50.price_date, qqq50.low, label="Low", linewidth=2) title!("QQQ Daily Prices") xlabel!("Date") ylabel!("Price")

Lines plots of the daily high and low prices for the QQQ fund as a function of time for a period of fifty days. The prices exhibit fluctuations around a positive linear trend.

Introduction to Data Visualization with Julia

Cheat Sheet

  • label to set or hide legend

  • Exclamation mark notation: modify current plot

  • Customize plot()

    • linewidth
    • linecolor
  • Customize scatter()

    • markercolor
    • smooth to add regression line (linewidth, linecolor to customize regression line) → good way to display correlation
Introduction to Data Visualization with Julia

Let's practice!

Introduction to Data Visualization with Julia

Preparing Video For Download...