Python for Absolute Beginners: A Step by Step Guide to Learn Python Programming from Scratch, with Practical Coding Examples and Exercises by Warner Andrew

Python for Absolute Beginners: A Step by Step Guide to Learn Python Programming from Scratch, with Practical Coding Examples and Exercises by Warner Andrew

Author:Warner, Andrew [Warner, Andrew]
Language: eng
Format: epub, azw3, pdf
Published: 2020-07-20T00:00:00+00:00


It’s the same plot but instead of a solid blue line, we are seeing four red dots because we added the optional string ‘-ro’. The default is the string ‘-b’ which is for the solid blue line. We can control the axes limits with the following statement.

plt.axis([0, 6, 0, 20])

The first two numbers are for x-axis and the last two for y-axis. Let’s create a script, add following line of codes, and save it with the name firstplot.py.

import matplotlib.pyplot as plt

plt.plot([1,2,3,4], [1,4,9,16], 'ro')

plt.axis([0, 6, 0, 20])

plt.show()

Let’s run the script. If you saved the file in the folder where Python is saved, you can run the script like this.

>>> import firstplot

To conserve resources, Python only runs import once for each entity. Subsequent runs of the command import firstplot will do nothing. We have an alternative: in Python 2.7, you can use the execfile() function to run any file as long as you have it’s complete path. This is also useful if you are saving your scripts in a folder other than where Python is installed. One advantage that execfile() gives us is that we can use it as many times as we want to run our script. For example, let’s grab the file path from Windows.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.