<jupyter_start><jupyter_text>Tutorial 2.3: Plotting Basics*ASTR 211: Observational Astronomy, Spring 2021* \*Written by <NAME>* You will have to plot things *a lot* if you continue in the astronomy major/astronomy as a career. (If you work with Prof. Redfield especially -- the man loves plots so much, our research group got matching T-shirts that read "BRING A PLOT".) However, we'll only get into the basics here. We'll talk more in-depth on the topic in the next tutorial.First of all, in order to plot something, you need some data. Namely, an x-axis and a y-axis. This data, as you might guess, is stored in lists/arrays, so let's generate some fake data to use:<jupyter_code>import numpy as np
x = np.linspace(-100,100,100)
y = x**2<jupyter_output><empty_output><jupyter_text>This should give us a parabola when we plot it, as we've just taken the square of all the value on our x-axis. Now, in order to do the actual plotting, we'll need another library called `matplotlib`, specifically the `pyplot` module. We import it like this, the common alias being `plt`:<jupyter_code>import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Now we can get to work. All we need to do now is give the `matplotlib` function `plot()` our axes:<jupyter_code>plt.plot(x,y)<jupyter_output><empty_output><jupyter_text>It's looking good so far, but it's missing labels, and a title. **Always title your plot and label your axes appropriately.** In order to add axis labels, we use the functions `xlabel()` and `ylabel()`, and to add a title, we use `title()`:<jupyter_code>plt.plot(x,y)
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.title("Title")<jupyter_output><empty_output><jupyter_text>Now, perhaps this isn't the only function you want on your plot. For example, the infamous plot of the Planck function of different stellar temperatures has multiple blackbody curves on it. We can totally do that with `matplotlib`. Let's make a couple more fake functions to demonstrate this:<jupyter_code>y2 = 4*x**2
y3 = 10*x**2<jupyter_output><empty_output><jupyter_text>In order to _overplot_ these functions on the plot we already have, we just add more `plot()` functions with these new axes:<jupyter_code>plt.plot(x,y)
plt.plot(x,y2)
plt.plot(x,y3)
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.title("Title")<jupyter_output><empty_output><jupyter_text>Now, with all these lines, it might be hard to tell which correponds to which function. We can create a legend for the plot with the `legend()` function, but first we have to give each plot a _label_ (make sure to call `legend()` _after_ your plotting lines, or it'll have no labels to fill itself with):<jupyter_code>plt.plot(x,y, label='x^2')
plt.plot(x,y2, label='4x^2')
plt.plot(x,y3, label='10x^2')
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.title("Title")
plt.legend()<jupyter_output><empty_output><jupyter_text>(The legend goes wherever it wants; don't worry about formatting for now.) Now, just for the sake of demonstration, perhaps we want to have a scatter plot rather than a line plot. Instead of `plot()`, we can use `scatter()`:<jupyter_code>plt.scatter(x,y, label='x^2')
plt.scatter(x,y2, label='4x^2')
plt.scatter(x,y3, label='10x^2')
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.title("Title")
plt.legend()<jupyter_output><empty_output><jupyter_text>With a lot of points, this looks kind of silly, but you can tell that they're discrete values. Well great, you've got your plot. Let's say you're satisfied. If you want to save the plot, you can add the `savefig()` function to the end of your list of commands, which takes a string with the name you want to give the file as an argument. It'll save the figure in the same directory as your code by default:<jupyter_code>plt.scatter(x,y, label='x^2')
plt.scatter(x,y2, label='4x^2')
plt.scatter(x,y3, label='10x^2')
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.title("Title")
plt.legend()
plt.savefig("test.pdf")<jupyter_output><empty_output><jupyter_text>Note that the `savefig()` function overwrites any file with the name that you give it!That's about it for the basics, but there are two more things I'll mention. First, just like printing, Jupyter likes to give you the output without you explicitly asking for it. The `print()` equivalent for `matplotlib` is the `show()` function:<jupyter_code>plt.scatter(x,y, label='x^2')
plt.scatter(x,y2, label='4x^2')
plt.scatter(x,y3, label='10x^2')
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.title("Title")
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Note that you don't have to use the `show()` function if you're not planning on reading the plot to the screen, i.e. you just want to save it as a file. Second, once you're done with a plot, you should make it a good habit to use the `close()` function. When you use `show()` to read it to your screen, you do this implicitly by closing the plot window when you're done. In other cases, though, you should add the `close()` function to the end of your list of commands. Sometimes it will leave Python open and running, which can slow down your computer. Also, if you don't use `close()` after plotting, and go on to make another plot later in your code, you just might find that the results of the earlier plot show up on both, since you haven't told Python that you were done with the first one.<jupyter_code>plt.scatter(x,y, label='x^2')
plt.scatter(x,y2, label='4x^2')
plt.scatter(x,y3, label='10x^2')
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.title("Title")
plt.legend()
plt.savefig("test.pdf")
plt.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11591355599214145, 'fraction_numerical': 0.012145025897481693, 'mean_word_length': 5.619385342789598, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '31933', 'license': 'MIT', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1802, 'n_words': 729, 'path': 'files/astr211_tut2-3.ipynb', 'repo_name': 'mvtea/mvtea.github.io', 'size': '5605'}