Www.ucolick.org
Python PlottingPlotting in python is actually very simple. Once you understand the basics, looking up whatever you need to do on Google is a short step away. The first thing you need to do to start plotting in python is import a module (another set of functions that somebody else wrote for us) into python. We will use ‘matplotlib’ (and ‘numpy’) to help us do ‘matlab-like’ numerical calculations and plots.import matplotlib.pyplot as pltimport numpy as npYou can always import a module without using the ‘as’ command. Adding in ‘as plt’ will give a prefix—plt—to any matplotlib code we want to use. For example, a plot command for your data might look like plt.plot(data).Now let’s generate some data to plot! Numpy will make all sorts of arrays:# data to plot# make an array with 100 elements, sequentially going from 0 to 2*pi# let’s make y(x) = sin(x)x_axis = np.linspace(0,2*3.14,100)y_axis = np.sin(x_axis)# data to plotplt.plot(x_axis, y_axis, ‘.’)plt.ylabel('Sin(x)’)plt.show()Simple, right!? The last argument in the plot function, ‘.’, changes the default setting of using a solid line to using a dot to represent each individual data point, which is necessary when plotting actual data. Much of the rest of this tutorial will be taken from . Do not hesitate to search google for other questions you might have. It is imperative that you get used to searching for code, isolating what you need, and playing around with it. You don’t need to know or remember all the commands that are included in any particular module. All you need is to have the code saved to reuse and modify.To make a histogram, all you need to do is use the plt.hist(x, nbins) function. It takes a bunch random numbers in an array, divides a plot into an integer number of bins, nbins, and tells you how many numbers between appear per bin. Examine the following code to see an example:import numpy as npimport matplotlib.pyplot as plt# Fixing random state for reproducibilitynp.random.seed(19680801)# make 10,000 random numbers with average “mu” and standard # deviation “sigma”mu, sigma = 100, 15x = mu + sigma * np.random.randn(10000)# the histogram of the datan, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)plt.xlabel('Smarts')plt.ylabel('Probability')plt.title('Histogram of IQ')plt.text(60, .025, r'$\mu=100,\ \sigma=15$')plt.axis([40, 160, 0, 0.03])plt.grid(True)plt.show()If you run this code you should get something like this:The following code shows you how to make multiple plots on one figure, and uses non-linear axes (like a log-log plot). The function plt.figure(1) sets all plotting commands to act on figure 1. Then you can use the subplot command to make multiple plots. For example, when you enter in plt.subplot(221), you are telling python that you want to use a 2x2 set of plots (4 plots total), and that you are editing the 1st plot of the 4, then you write whatever commands you would normally use to make a plot. After you are done with the 1st plot, you can move to the second plot by using the plt.subplot(222) command (and plt.subplot(223) & plt.subplot(224) and so on).import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.ticker import NullFormatter # useful for `logit` scale# Fixing random state for reproducibilitynp.random.seed(19680801)# make up some data in the interval ]0, 1[y = np.random.normal(loc=0.5, scale=0.4, size=1000)y = y[(y > 0) & (y < 1)]y.sort()x = np.arange(len(y))# plot with various axes scalesplt.figure(1)# linearplt.subplot(221)plt.plot(x, y)plt.yscale('linear')plt.title('linear')plt.grid(True)# logplt.subplot(222)plt.plot(x, y)plt.yscale('log')plt.title('log')plt.grid(True)# symmetric logplt.subplot(223)plt.plot(x, y - y.mean())plt.yscale('symlog', linthreshy=0.01)plt.title('symlog')plt.grid(True)# logitplt.subplot(224)plt.plot(x, y)plt.yscale('logit')plt.title('logit')plt.grid(True)# Format the minor tick labels of the y-axis into empty strings with# `NullFormatter`, to avoid cumbering the axis with too many labels.plt.gca().yaxis.set_minor_formatter(NullFormatter())# Adjust the subplot layout, because the logit one may take more space# than usual, due to y-tick labels like "1 - 10^{-3}"plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35)plt.show()The result of running this code looks like: ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.