plots in python¶
In [2]:
import matplotlib.pyplot as plt
import numpy as np
A simple plot:
In [7]:
x = np.linspace(0,2*np.pi,361)
fx = np.sin(x)
plt.plot(x,fx)
plt.show()
In [19]:
rng = np.random.default_rng() # make a RNG
u = rng.random(50) * 2 * np.pi
v = np.sin(u) + rng.normal(0,1/4,50)
plt.plot(x,fx,label= "true underlying function")
plt.plot(u,v,'o',label = "noisy observations")
plt.xlabel('x')
plt.legend()
plt.show()
Another way to set up a figure and a set of axes and build a plot and then show it.
In [33]:
fig, ax = plt.subplots( figsize = (10,4))
ax.plot(x,fx, label = "True function")
ax.plot(u,v,'o', label = "observations")
ax.set_title("A cool plot")
ax.legend()
ax2 = ax.twinx()
ax2.hist(u,color= (0.545,0,0,.2))
ax2.set_ylabel('Frequency of observations')
plt.show()
In [ ]: