Last active
March 19, 2019 23:43
-
-
Save chausies/5abc9548c42e26a5c70caefa0d1da423 to your computer and use it in GitHub Desktop.
A very simple example showing you how to make more or less nice plots in python
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 __future__ import division, print_function # Always do this just to | |
# ensure compatability | |
# between python 2.7 and 3 | |
import matplotlib.pylab as P | |
# Always do these next to lines to enable LaTeX to be used on the | |
# matplotlib plots | |
P.rc('text', usetex=True) | |
P.rc('font', family='serif') | |
x = P.linspace(0, 2, 15) # x is an array going from 0 to 2 evenly divided | |
# into 15 points | |
y_1 = x | |
theta_i = x**2 | |
y_3_hat = P.sqrt(x) | |
P.plot(x, y_1, linewidth=2, color='red', | |
label=r"$y_1$") # note that the `r` means "raw", and is needed if | |
# you're using latex. Else, it's unnecessary. | |
P.plot(x, theta_i, linewidth=2, color='blue', | |
label=r"$\theta_{i}$") | |
P.scatter(x, y_3_hat, linewidth=2, color='green', | |
label=r"$\widehat{y}_3$") # I prefer `widehat` to `hat` xP | |
P.title("This is a test plot of 3 functions") | |
P.xlabel(r"$x$-axis") | |
P.ylabel("What even are these units?") | |
P.legend() # Enables the legend that shows the labels you gave to each plot | |
# Save the plot if you want | |
P.savefig("example.eps") # eps is the best format for LaTeX. It's a vector | |
# format, like SVG, so no quality gets lost. PNG | |
# and other formats work as well | |
# Clear the plot if you want | |
# P.clf() | |
# Show the plot if you want (note that this stops execution until you close | |
# the plot) | |
P.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment