1#We create a secondary y-axis for the definded column
2df.plot(secondary_y='name_of_column')
3plt.show()
1import numpy as np
2import matplotlib.pyplot as plt
3
4# Create some mock data
5t = np.arange(0.01, 10.0, 0.01)
6data1 = np.exp(t)
7data2 = np.sin(2 * np.pi * t)
8
9fig, ax1 = plt.subplots()
10
11color = 'tab:red'
12ax1.set_xlabel('time (s)')
13ax1.set_ylabel('exp', color=color)
14ax1.plot(t, data1, color=color)
15ax1.tick_params(axis='y', labelcolor=color)
16
17ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
18
19color = 'tab:blue'
20ax2.set_ylabel('sin', color=color) # we already handled the x-label with ax1
21ax2.plot(t, data2, color=color)
22ax2.tick_params(axis='y', labelcolor=color)
23
24fig.tight_layout() # otherwise the right y-label is slightly clipped
25plt.show()
26
1import numpy as np
2import matplotlib.pyplot as plt
3x = np.arange(0, 10, 0.1)
4y1 = 0.05 * x**2
5y2 = -1 *y1
6
7fig, ax1 = plt.subplots()
8
9ax2 = ax1.twinx()
10ax1.plot(x, y1, 'g-')
11ax2.plot(x, y2, 'b-')
12
13ax1.set_xlabel('X data')
14ax1.set_ylabel('Y1 data', color='g')
15ax2.set_ylabel('Y2 data', color='b')
16
17plt.show()
1import matplotlib.pyplot as plt
2import numpy as np
3import datetime
4import matplotlib.dates as mdates
5from matplotlib.transforms import Transform
6from matplotlib.ticker import (
7 AutoLocator, AutoMinorLocator)
8
9fig, ax = plt.subplots(constrained_layout=True)
10x = np.arange(0, 360, 1)
11y = np.sin(2 * x * np.pi / 180)
12ax.plot(x, y)
13ax.set_xlabel('angle [degrees]')
14ax.set_ylabel('signal')
15ax.set_title('Sine wave')
16
17
18def deg2rad(x):
19 return x * np.pi / 180
20
21
22def rad2deg(x):
23 return x * 180 / np.pi
24
25secax = ax.secondary_xaxis('top', functions=(deg2rad, rad2deg))
26secax.set_xlabel('angle [rad]')
27plt.show()
28