Trying curve plotting

Our first problem will require you to draw a function with pyplot. Drawing a function is quite straightforward; you just have to get a series of x coordinates and map them to the y axis by using the function that you want to plot. Since the mapping results are stored away into two vectors, the plot function will deal with the curve representation. The precision of the representation will be greater if the mapped points are enough (50 points is a good sampling number):

In: import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 5, 50)
y_cos = np.cos(x)
y_sin = np.sin(x)

Using the NumPy linspace() function, we will create a series of 50 equally distanced numbers ranging from 0 to 5. We can use them to map our y to the cosine and sine functions:

In: plt.figure() # initialize a figure
plt.plot(x,y_cos) # plot series of coordinates as a line
plt.plot(x,y_sin)
plt.xlabel('x') # adds label to x axis
plt.ylabel('y') # adds label to y axis
plt.title('title') # adds a title
plt.show() # close a figure

Here is your first plot:

The pyplot.plot command can plot more curves in a sequence, with each curve taking a different color according to an internal color schema, which can be customized by explicating the favored color sequence. To do so, you have to manipulate the list containing the sequence of colors that matplotlib uses:

In: list(mpl.rcParams['axes.prop_cycle']) 

Out: [{'color': '#1f77b4'},
{'color': '#ff7f0e'},
{'color': '#2ca02c'},
{'color': '#d62728'},
{'color': '#9467bd'},
{'color': '#8c564b'},
{'color': '#e377c2'},
{'color': '#7f7f7f'},
{'color': '#bcbd22'},
{'color': '#17becf'}]
#1f77b4, #ff7f0e, #2ca02c, and all the others are all colors expressed in hexadecimal form. In order to figure out how they look, you can use the colorhexa website, providing you with useful information on each of them: https://www.colorhexa.com/.

The hack can be done by using the cycler function and feeding it with a list of string names referring to the colors you want to use in sequence:

In: mpl.rcParams['axes.prop_cycle'] = mpl.cycler('color', 
['blue', 'red', 'green'])

Moreover, the plot command, if not given any other information, will assume that you are going to plot a line. Therefore, it will link all the provided points in a curve. If you add a new parameter such as '.' – that is, plt.plot(x,y_cos,'.') – you signal that you instead want to plot a series of separated points (the string for a line is '-', but we will soon show another example).

In this way, if you've customized rcParams['axes.prop_cycle'] as proposed previously, the next graphs will first have a blue curve, then the second will be red, and the third green. Then, the color loop will restart. We leave this decision to you. All the examples in this chapter will just follow the standard color sequence, but you are free to experiment with better color settings.

Please note that you can also set the title of the graph and label the axis by the title, xlabel, and ylabel from pyplot.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset