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
#1
Using matplotlib or another graphing library -

Let's say you have a dataframe with cols Time, A, and B.

Time is just seconds - let's say 1-60. This will be our x axis.

A is a series of numbers from -100 to +100, and B is a series of numbers from 285-295. A and B are both going to be on Y axis. The two lines are A vs Time and B vs time.

Lets say we make the graph 10 units high.

Now I know that I can just go ahead and plot two lines on a single graph. However, the data that I want to visualize are the slopes of the lines relative to each other for the same time period. If one line is down in the -100 to 100 range, and the other is up at 285-295... well it's just harder to look at.

So what I want to do is to have two independent y-ranges on the graph - one from -100 to 100 and the other from 285 to 295 superimposed on top of each other - so if my graph is 10 units tall, the vertical center, at unit 5, will be at value 0 for graph A and at value 290 for graph B - hence they are superimposed on each other. The scaling between the two y-series will be different, but since I'm interested in visually comparing the slopes of the lines, the scaling is irrelevant as the slopes will remain the same.

So is it possible to graph this type of superimposition? If so then how do you do it?
Reply
#2
Hi Sawtooth,
beeing no python-crack, I'd rather manipulate the data to get it in range.
Squeeze (or blow it up) with a factor
Shove it up (or down) with a summand.
Then plot the new data as usual.

byebye
Reply
#3
I'm not 100% certain I'm grasping this but if my guess is right then you could either:

map your ranges to a common one

or

map one of them into the other.


mapping a range to another

/regards
Reply
#4
Check out secondary axis

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

A quick and dirty example.
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 360, 1)
y = np.sin(x * np.pi / 180)
y2 = np.sin((x + 20) * np.pi / 180) + 1

fig, ax = plt.subplots()
ax2 = ax
# ax2 = ax.twinx()   # Remove comment to see secondary axis
ax.plot(x, y, color="blue")
ax2.plot(x, y2, color="green")
plt.show()
Reply
#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


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,925 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,596 Feb-28-2021, 11:56 AM
Last Post: JaneTan
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,929 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