Python Forum
Line graph with two superimposed lines
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Line graph with two superimposed lines
#5
(Apr-02-2024, 04:20 PM)deanhystad Wrote: Check out secondary axis

https://matplotlib.org/stable/gallery/su..._axis.html

Thank you Dean, subplots did the trick! I did the below standalone code as an experiment and it's doing exactly what I wanted it to, now I just need to adapt it for my application.

import matplotlib.pyplot as plt
import numpy as np

# Sample data
Time = np.arange(1, 61)  # 1 to 60 seconds
A = np.linspace(60, -50, 60)  # A series
B = np.linspace(285, 295, 60)  # B series

# Create the plot
fig, ax1 = plt.subplots()

# Plot A on the primary y-axis
color = 'tab:red'
ax1.set_xlabel('Time (s)')
ax1.set_ylabel('A', color=color)
ax1.plot(Time, A, color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax1.set_ylim([-100, 100])  # Set y-axis limits for A
ax1.grid(True)

# Create a secondary y-axis for B
ax2 = ax1.twinx()
color = 'tab:blue'
ax2.set_ylabel('B', color=color)
ax2.plot(Time, B, color=color)
ax2.tick_params(axis='y', labelcolor=color)
ax2.set_ylim([285, 295])  # Set y-axis limits for B

# Show the plot
plt.title('Comparison of A vs. Time and B vs. Time with Different Scales')
plt.show()
[Image: plot1.png]
Reply


Messages In This Thread
RE: Line graph with two superimposed lines - by sawtooth500 - Apr-02-2024, 08:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to connect mysql from txt 1 line goes good but not all lines in text kingceasarr 4 2,974 Mar-24-2021, 05:45 AM
Last Post: buran
  Python Matplotlib: How do I plot lines with different end point in the same graph? JaneTan 0 1,636 Feb-28-2021, 11:56 AM
Last Post: JaneTan
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 6,172 Aug-10-2020, 11:01 PM
Last Post: medatib531

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020