Python Forum
Line graph with two superimposed lines - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Line graph with two superimposed lines (/thread-41884.html)



Line graph with two superimposed lines - sawtooth500 - Apr-02-2024

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?


RE: Line graph with two superimposed lines - HerrAyas - Apr-02-2024

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


RE: Line graph with two superimposed lines - popejose - Apr-02-2024

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


RE: Line graph with two superimposed lines - deanhystad - Apr-02-2024

Check out secondary axis

https://matplotlib.org/stable/gallery/subplots_axes_and_figures/secondary_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()



RE: Line graph with two superimposed lines - sawtooth500 - Apr-02-2024

(Apr-02-2024, 04:20 PM)deanhystad Wrote: Check out secondary axis

https://matplotlib.org/stable/gallery/subplots_axes_and_figures/secondary_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]