Python Plot Two Lines With Different Y Axis

[Solved] Python Plot Two Lines With Different Y Axis | Basic - Code Explorer | yomemimo.com
Question : python plot two lines with different y axis

Answered by : antonio-buccola

import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0.01, 10.0, 0.01)
data1 = np.exp(t)
data2 = np.sin(2 * np.pi * t)
fig, ax1 = plt.subplots()
color = 'tab:red'
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp', color=color)
ax1.plot(t, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
color = 'tab:blue'
ax2.set_ylabel('sin', color=color) # we already handled the x-label with ax1
ax2.plot(t, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.show()

Source : https://matplotlib.org/devdocs/gallery/subplots_axes_and_figures/two_scales.html | Last Update : Tue, 14 Sep 21

Answers related to python plot two lines with different y axis

Code Explorer Popular Question For Basic