More advanced graphical output

 Here, we will learn a more advanced syntax that harnesses the full power of matplotlib. It gives the user more options and greater control.

import numpy as np
import matplotlib.pyplot as plt

# Define the sinc function, with output for x=0
# defined as a special case to avoid division by zero

def s(x):
    a = np.where(x == 0., 1., np.sin(x)/x)
    return a

x=np.arange(0., 10., 0.1)
y=np.exp(x)
t=np.linspace(-15., 15., 150)
z=s(t)

# create a figure window
fig = plt.figure(figsize=(9, 7))
# subplot: linear plot of exponential
ax1 = fig.add_subplot(2, 2, 1)
ax1.plot(x, y, 'C0')
ax1.set_xlabel('time (ms)')
ax1.set_ylabel('distance (mm)')
ax1.set_title('exponential')

#subplot: semi-log plot of exponential
ax2 = fig.add_subplot(2, 2, 2)
ax2.plot(x, y, 'C2')
ax2.set_yscale('log')

# ax2.semilogy(x, y, 'C2') # same as 2 previous lines
ax2.set_xlabel('time (ms)')
ax2.set_ylabel('distance (mm)')
ax2.set_title('exponential')

# subplot: wide subplot of sinc function
ax3 = fig.add_subplot(2, 1, 2)
ax3.plot(t, z, 'C3')
ax3.axhline(color='gray')
ax3.axvline(color='gray')
ax3.set_xlabel('angle (deg)')
ax3.set_ylabel('electric field')
ax3.set_title('sinc function')
fig.tight_layout()
fig.savefig("multiplePlots1window.pdf")
fig.show()
fig = plt.figure(figsize=(9, 7))
The matplotlib statement above creates a Figure object, assigns it the name fig, and opens a blank figure window
In fact we can use the figure function to open up multiple figure objects with different figure windows. The statements
fig1 = plt.figure()
fig2 = plt.figure()
open up two separate windows, one named fig1 and the other fig2.We can then use the names fig1 and fig2 to plot things in either window.The figure function need not take any arguments if you are satisfied
with the default settings such as the figure size and the background color. On the other hand, by supplying one or more keyword arguments, you can customize the figure size, the background color, and a few other properties. For example, in the program listing , the keyword argument figsize sets the width and height of the figure window. The default size is (8, 6); in our program we set it to (9, 8), which is a bit wider and higher than the default size. In the example above, we also choose to open only a single window, hence the single figure call.

The fig.add_subplot(2, 2, 1) is a matplotlib function that divides the figure window into 2 rows (the first argument) and 2 columns (the second argument). The third argument creates a subplot in the first of the 4 subregions (i.e., of the 2 rows , 2 columns) created by the fig.add_subplot(2, 2, 1) call.

Plots with multiple axes
Plotting two different quantities that share a common independent variable on the same graph can be a compelling way to compare and visualize data. Figure below shows an example of such a plot, where the blue curve is linked to the left blue y-axis and the red data points are linked to the right red y-axis.

 The code below shows how this can be done using matplotlib using the function twinx().

import numpy as np
import matplotlib.pyplot as plt

fig, ax1 = plt.subplots(figsize=(7.5, 4.5))
xa = np.linspace(0.01, 6.0, 150)
ya = np.sin(np.pi*xa)/xa
ax1.plot(xa, ya, '-C0')
ax1.set_xlabel('x (micrometers)')
# Make y-axis label, ticks and numbers match line color.
ax1.set_ylabel('oscillate', color='C0')
ax1.tick_params('y', colors='C0')

ax2 = ax1.twinx() # use same x-axis for a 2nd (right) y-axis
xb = np.arange(0.3, 6.0, 0.3)
yb = np.exp(-xb*xb/9.0)
ax2.plot(xb, yb, 'oC3')
ax2.set_ylabel('decay', color='C3') # axis label
ax2.tick_params('y', colors='C3') # ticks & numbers
fig.tight_layout()
plt.show()


After plotting the first set of data using the axes ax1, calling twinx() instructs matplotlib to use the same x-axis for a second x-y set of data, which we set up with a new set of axes ax2. The set_ylabel and tick_parameters functions are used to harmonize the colors of the y-axes with the different data sets.There is an equivalent function twiny() that allows two sets of data to share a common y-axis and then have separate (top and bottom) x-axes.


Comments

Popular posts from this blog

Python For Engineers CST 445 KTU Open Elective Notes S7 - Dr Binu V P 9847390760

Model Question Paper CST 445 KTU Python For Engineers

Plotting- Simple plots,setting limits,subplot, semilog, loglog plots