matplotlib secondary x axis

Solutions on MaxInterview for matplotlib secondary x axis by the best coders in the world

showing results for - "matplotlib secondary x axis"
Lorenzo
23 Oct 2020
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