#!/usr/bin/env python # coding: utf-8 # # Plotting with Matplotlib # # Matplotlib is the "standard" Python plotting package (although there are a few other good contenders). # # Let's get on to that all important step of visualizing data. We will be using the matplotlib Python package for that. Let's start by plotting the function $f(x) = x^2$. # # First, let's generate the numbers using Numpy: # In[23]: x = np.arange(0,10,0.4) f = x ** 2 # Now let's create a noisy version of $f$ by adding some random Gaussian noise: # In[28]: mu, sigma = 0, 5 # mean and standard deviation f_noisy = f + np.random.normal(mu, sigma, len(f)) f_noisy # To plot the data, first import the **pyplot** module of matplotlib: # In[30]: import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') # In[37]: plt.plot(x, f, '-k', label="f(x)") # plot the function using a black line plt.plot(x, f_noisy, 'ob', label="noisy f") # plot the noisy version using blue circles # We can add additional features to the plot such as a legend and axis labels, and note the use of semi-colons to suppress the output of the commands: # In[44]: plt.plot(x, f, '-k', label="f(x)"); # plot the function using a black line plt.plot(x, f_noisy, 'ob', label="noisy f"); # plot the noisy version using blue circles plt.xlabel("x", size="xx-large"); plt.ylabel("f(x)", size="xx-large"); plt.ylim(-5, 100); plt.legend(loc="upper left"); # ### Example 2: histograms # # Let's generate two datasets from a normal distribution and plot their histograms: # In[45]: mu = 100 # mean of distribution sigma = 15 # standard deviation of distribution x1 = mu + sigma * np.random.randn(10000) x2 = mu + 10 + sigma * np.random.randn(10000) # Next, we'll generate a histogram of the two datasets, showing some features of Matplotlib's hist function. The 'normed' flag normalizes the bin heights such that it represents a probability distribution. 'alpha' is the opacity. # In[46]: num_bins = 50 # the histogram of the data plt.hist(x1, num_bins, normed=True, facecolor='green', alpha=0.4, label='first'); plt.hist(x2, num_bins, normed=True, facecolor='blue', alpha=0.4, label='second'); plt.xlabel('x',size="xx-large"); plt.ylabel('normalized counts', size="xx-large"); plt.legend(loc='upper right');