This tutorial will show you how to make tables and plots of data in python.
We start with tabulation. For this end, we will use the tabulate package. Full details on how to install and use this package can be found in their documentation at https://pypi.org/project/tabulate/. We will illustrate a few examples below.
Here is how to use tabulate to display some data
with each column labeled by the entries in headers
.
import tabulate
headers = ["h", "L2 error", "Linfty error"]
data = [[0.1, 1e-2, 2e-3], [0.05, 1e-4, 2e-4], [0.025, 1e-6, 2e-5]]
table = tabulate.tabulate(data, headers=headers)
print(table)
h L2 error Linfty error ----- ---------- -------------- 0.1 0.01 0.002 0.05 0.0001 0.0002 0.025 1e-06 2e-05
Observe that our data is given as a list of lists. When we tabulate this data, python interprets each inner list as a row in the table. The data stored in the table
variable is simply plain text. If one wishes to use their table in, say, an html page or a $\LaTeX$ document, one can do the following:
# For html
table = tabulate.tabulate(data, headers=headers, tablefmt="html")
print("html code for table:")
print(table)
print()
# For LaTeX
table = tabulate.tabulate(data, headers=headers, tablefmt="latex")
print("latex code for table:")
print(table)
html code for table: <table> <thead> <tr><th style="text-align: right;"> h</th><th style="text-align: right;"> L2 error</th><th style="text-align: right;"> Linfty error</th></tr> </thead> <tbody> <tr><td style="text-align: right;">0.1 </td><td style="text-align: right;"> 0.01 </td><td style="text-align: right;"> 0.002 </td></tr> <tr><td style="text-align: right;">0.05 </td><td style="text-align: right;"> 0.0001</td><td style="text-align: right;"> 0.0002</td></tr> <tr><td style="text-align: right;">0.025</td><td style="text-align: right;"> 1e-06 </td><td style="text-align: right;"> 2e-05 </td></tr> </tbody> </table> latex code for table: \begin{tabular}{rrr} \hline h & L2 error & Linfty error \\ \hline 0.1 & 0.01 & 0.002 \\ 0.05 & 0.0001 & 0.0002 \\ 0.025 & 1e-06 & 2e-05 \\ \hline \end{tabular}
As we see above, the table
variable now stores the raw code for either html or $\LaTeX$, which can then be copy-pasted directly into an html document or $\LaTeX$ file.
Alternatively, one can also write the output directly to a text file. An example of how to do this is provided below.
table = tabulate.tabulate(data, headers=headers)
# uncomment the lines below to open a file named table.txt, write the table to the file, and then
# save it
#fl = open('table.txt', 'w')
#fl.write(table)
#fl.close()
Now we will show how to make plots of data in python. For this, we will use the matplotlib library. For a more comprehensive resource on how to use this library, visit their website at https://matplotlib.org/.
Suppose we have some data that represents some points in the $xy$ plane, and we wish to plot them. An easy way to do this is given below:
import matplotlib.pyplot
import numpy
# first we generate our xy data
xs = numpy.linspace(0,10,100)
ys = [numpy.sin(x/2) + numpy.cos(2*x) for x in xs]
# now we make a plot of the data
# first we create a blank plot
fig, ax = matplotlib.pyplot.subplots()
# now we add our data to the plot
ax.plot(xs, ys)
# next, we show the plot
matplotlib.pyplot.show()
# once we are satisfied with our plot, we save it
# uncomment the line below to save the plot with the filename 'filename.png'
#fig.savefig('filename.png')
We end with one final example. Suppose we wish to plot a second set of data on the same figure, as well as give our graph a title. Here's how we can do that:
zs = [numpy.cos(3*x) for x in xs]
fig, ax = matplotlib.pyplot.subplots()
ax.plot(xs, ys, label="ys")
ax.plot(xs, zs, label="zs")
matplotlib.pyplot.title("plot title")
matplotlib.pyplot.legend() # displays the graph labels
matplotlib.pyplot.show()
These are only a few things one can do with the matplotlib library (specifically the pyplot package). Read the documentation to learn more.