{ "cells": [ { "cell_type": "markdown", "source": [ "## Overview\n", "\n", "This tutorial will give some examples of plotting and plotting features in Julia, as well as providing references to some relevant resources. The main plotting library is `Plots.jl`, but there are some others that provide useful features.\n", "\n", "## Some Resources\n", "\n", "* `Plots.jl` [useful tips](https://docs.juliaplots.org/latest/basics/#Useful-Tips)\n", "* `Plots.jl` [examples](https://docs.juliaplots.org/latest/generated/gr/)\n", "* [Plot attributes](http://docs.juliaplots.org/latest/generated/attributes_plot/)\n", "* [Axis attributes](http://docs.juliaplots.org/latest/generated/attributes_axis/#Axis)\n", "* [Color names](http://juliagraphics.github.io/Colors.jl/stable/namedcolors/)" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "## Packages\n", "\n", "To run this notebook, you can download or view the [`Project.toml`](https://raw.githubusercontent.com/vsrikrish/climate-risk-analysis/gh-pages/tutorials/notebooks/julia-plots/Project.toml) and [`Manifest.toml`](https://raw.githubusercontent.com/vsrikrish/climate-risk-analysis/gh-pages/tutorials/notebooks/julia-plots/Manifest.toml). You can also just add packages as needed to the environment, which is by default set to exist in the path of the notebook file." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "using Pkg\n", "Pkg.activate(@__DIR__)\n", "Pkg.instantiate()" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "## Demos" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "using Plots\n", "using Random # hide\n", "Random.seed!(1) # hide" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "### Line Plots\n", "To generate a basic line plot, use `plot`." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "y = rand(5)\n", "plot(y, label=\"original data\", legend=:topright)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "There's a lot of customization here that can occur, a lot of which is discussed in the docs or can be found with some Googling." ], "metadata": {} }, { "cell_type": "markdown", "source": [ "#### Adding Plot Elements\n", "\n", "Now we can add some other lines and point markers." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "y2 = rand(5)\n", "y3 = rand(5)\n", "plot!(y2, label=\"new data\")\n", "scatter!(y3, label=\"even more data\")" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Remember that an exclamation mark (!) at the end of a function name means that function modifies an object in-place, so `plot!` and `scatter!` modify the current plotting object, they don't create a new plot." ], "metadata": {} }, { "cell_type": "markdown", "source": [ "#### Removing Plot Elements\n", "\n", "Sometimes we want to remove legends, axes, grid lines, and ticks." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "plot!(legend=false, axis=false, grid=false, ticks=false)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "#### Aspect Ratio\n", "\n", "If we want to have a square aspect ratio, use `ratio = 1`." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "v = rand(5)\n", "plot(v, ratio=1, legend=false)\n", "scatter!(v)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "### Heatmaps" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "A heatmap is effectively a plotted matrix with colors chosen according to the values. Use `clim` to specify a fixed range for the color limits." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "A = rand(10, 10)\n", "heatmap(A, clim=(0, 1), ratio=1, legend=false, axis=false, ticks=false)\n", "\n", "\n", "M = [ 0 1 0; 0 0 0; 1 0 0]\n", "whiteblack = [RGBA(1,1,1,0), RGB(0,0,0)]\n", "heatmap(c=whiteblack, M, aspect_ratio = 1, ticks=.5:3.5, lims=(.5,3.5), gridalpha=1, legend=false, axis=false, ylabel=\"i\", xlabel=\"j\")" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "#### Custom Colors" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "using Colors\n", "\n", "mycolors = [colorant\"lightslateblue\",colorant\"limegreen\",colorant\"red\"]\n", "A = [i for i=50:300, j=1:100]\n", "heatmap(A, c=mycolors, clim=(1,300))" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "### Plotting Areas Under Curves" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "y = rand(10)\n", "plot(y, fillrange= y.*0 .+ .5, label= \"above/below 1/2\", legend =:top)\n", "\n", "\n", "x = LinRange(0,2,100)\n", "y1 = exp.(x)\n", "y2 = exp.(1.3 .* x)\n", "plot(x, y1, fillrange = y2, fillalpha = 0.35, c = 1, label = \"Confidence band\", legend = :topleft)\n", "\n", "\n", "x = -3:.01:3\n", "areaplot(x, exp.(-x.^2/2)/√(2π),alpha=.25,legend=false)\n", "\n", "\n", "M = [1 2 3; 7 8 9; 4 5 6; 0 .5 1.5]\n", "areaplot(1:3, M, seriescolor = [:red :green :blue ], fillalpha = [0.2 0.3 0.4])\n", "\n", "\n", "using SpecialFunctions\n", "f = x->exp(-x^2/2)/√(2π)\n", "δ = .01\n", "plot()\n", "x = √2 .* erfinv.(2 .*(δ/2 : δ : 1) .- 1)\n", "areaplot(x, f.(x), seriescolor=[ :red,:blue], legend=false)\n", "plot!(x, f.(x),c=:black)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "### Plotting Shapes" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "rectangle(w, h, x, y) = Shape(x .+ [0,w,w,0], y .+ [0,0,h,h])\n", "circle(r,x,y) = (θ = LinRange(0,2π,500); (x.+r.*cos.(θ), y.+r.*sin.(θ)))\n", "plot(circle(5,0,0), ratio=1, c=:red, fill=true)\n", "plot!(rectangle(5*√2,5*√2,-2.5*√2,-2.5*√2),c=:white,fill=true,legend=false)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "### Plotting Distributions" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "The [`StatsPlots.jl`](https://github.com/JuliaPlots/StatsPlots.jl/blob/master/README.md) package is very useful for making various plots of probability distributions." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "using Distributions, StatsPlots\n", "plot(Normal(2, 5))\n", "\n", "\n", "scatter(LogNormal(0.8, 1.5))" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "We can also use this functionality to plot distributions of data in tabular data structures like `DataFrames`." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "using DataFrames\n", "dat = DataFrame(a = 1:10, b = 10 .+ rand(10), c = 10 .* rand(10))\n", "@df dat density([:b :c], color=[:black :red])" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "### Editing Plots Manually" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "pl = plot(1:4,[1, 4, 9, 16])\n", "\n", "\n", "pl.attr" ], "metadata": {}, "execution_count": null }, { "outputs": [], "cell_type": "code", "source": [ "pl.series_list[1]" ], "metadata": {}, "execution_count": null }, { "outputs": [], "cell_type": "code", "source": [ "pl[:size]=(300,200)" ], "metadata": {}, "execution_count": null }, { "outputs": [], "cell_type": "code", "source": [ "pl" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "### Log-Scaled Axes" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "xx = .1:.1:10\n", "plot(xx.^2, xaxis=:log, yaxis=:log)\n", "\n", "\n", "plot(exp.(x), yaxis=:log)" ], "metadata": {}, "execution_count": null } ], "nbformat_minor": 3, "metadata": { "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.8.5" }, "kernelspec": { "name": "julia-1.8", "display_name": "Julia 1.8.5", "language": "julia" } }, "nbformat": 4 }