-
-
Save mauro3/6089af612380cb39ca00016d4f632cc2 to your computer and use it in GitHub Desktop.
Julia-Intro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From | |
https://github.com/JuliaComputing/JuliaBoxTutorials/tree/master/introductory-tutorials/intro-to-julia/short-version | |
lightly adapted. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Intro to plotting in Julia\n", | |
"\n", | |
"There are a few different ways to plot in Julia (including calling PyPlot). <br>\n", | |
"\n", | |
"Here we'll show you how to use `Plots.jl`." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"using Pkg; Pkg.add(\"Plots\")\n", | |
"using Plots" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"One of the advantages to `Plots.jl` is that it allows you to seamlessly change backends. In this notebook, we'll try out the `gr()` and `plotlyjs()` backends.<br>\n", | |
"\n", | |
"In the name of scientific inquiry, let's use this notebook to examine the relationship between the global temperature and the number of pirates between roughly 1860 and 2000." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"globaltemperatures = [14.4, 14.5, 14.8, 15.2, 15.5, 15.8]\n", | |
"numpirates = [45000, 20000, 15000, 5000, 400, 17]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**To get plotting, let's load the GR backend**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"gr()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"and now we can use commands like `plot` and `scatter` to generate plots." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plot(numpirates, globaltemperatures, label=\"line\") \n", | |
"scatter!(numpirates, globaltemperatures, label=\"points\") " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The `!` at the end of the `scatter!` function name makes `scatter!` a mutating function, indicating that the scattered points will be added onto the pre-existing plot.\n", | |
"\n", | |
"In contrast, see what happens when you replace `scatter!` in the above with the non-mutating function `scatter`.\n", | |
"\n", | |
"Next, let's update this plot with the `xlabel!`, `ylabel!`, and `title!` commands to add more information to our plot." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"xlabel!(\"Number of Pirates [Approximate]\")\n", | |
"ylabel!(\"Global Temperature (C)\")\n", | |
"title!(\"Influence of pirate population on global warming\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This still doesn't look quite right. The number of pirates has decreased since 1860, so reading the plot from left to right is like looking backwards in time rather than forwards. Let's flip the x axis to better see how pirate populations have caused global temperatures to change over time!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"xflip!()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"And there we have it!\n", | |
"\n", | |
"Note: We've had some confusion about this exercise. :) This is a joke about how people often conflate correlation and causation.\n", | |
"\n", | |
"**Without changing syntax, we can create this plot with the `unicodeplots()` backend**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"using Pkg; Pkg.add(\"UnicodePlots\")\n", | |
"unicodeplots()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plot(numpirates, globaltemperatures, label=\"line\") \n", | |
"scatter!(numpirates, globaltemperatures, label=\"points\") \n", | |
"xlabel!(\"Number of Pirates [Approximate]\")\n", | |
"ylabel!(\"Global Temperature (C)\")\n", | |
"title!(\"Influence of pirate population on global warming\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"collapsed": true | |
}, | |
"source": [ | |
"And notice how this second plot differs from the first!" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Julia 1.1.0", | |
"language": "julia", | |
"name": "julia-1.1" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"mimetype": "application/julia", | |
"name": "julia", | |
"version": "1.1.0" | |
}, | |
"toc": { | |
"nav_menu": { | |
"height": "66px", | |
"width": "252px" | |
}, | |
"navigate_menu": true, | |
"number_sections": true, | |
"sideBar": true, | |
"threshold": "2", | |
"toc_cell": false, | |
"toc_section_display": "block", | |
"toc_window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment