Pages

Friday, July 20, 2018

Simple Graph Plot in Python


First, let's explore basic function graphing using the iPython console. (I'm using Spyder's iPython console.)

We'll consider an elementary power function, namely the quadratic function $ f(x) =  x^{2} $

Steps:
  1. import pylab and numpy
  2. set the range of x-values. Here, we'll go from -5 to  +5, with a step of 0.01
  3. define the function y in terms of xVals (x-values)
  4. label the x and y axes
  5. give the graph a title. Here, we keep it simple with f(x) = x^2
  6. plot the graph. Here, xVals are the x-values, y are the y-values, and 'b' makes the curve blue
Enter the code directly into the iPython console
  
Our graph, nice and simple
If you don't want the graph to appear inline (inside the iPython console), in Spyder you can go to...
Tools >> Preferences >> iPython Console >> Graphics >> Graphics Backend, and set the backend to Automatic.

Next time, we'll plot some data points.

No comments:

Post a Comment