Issue
I am trying to recreate something similar to the following graph
Time-frequency graph with two y-axis
There are two y-axis, the left one shows frequency to the -8/3 power, the right one labels the original frequency with uneven distribution.
While I can make a secondary y axis, I am not sure how to use it to only label the value of a data point like they did in the picture.
Edit:Clumped y label
Solution
import matplotlib.pyplot as plt
import numpy as np
# Example data
time = np.array([0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.40, 0.41, 0.42])
frequency = np.array([5, 4.5, 4, 3.5, 3, 2.5, 2, 1.5, 1]) # Example frequency data
# Create figure and axis
fig, ax1 = plt.subplots()
# Plot the main data on ax1
ax1.plot(time, frequency ** (-8/3), 'g-') # Plot frequency to the -8/3 power
# Set the y-axis label
ax1.set_ylabel('Frequency (f)^{-8/3}', color='g')
# Create a secondary y-axis that shares the same x-axis
ax2 = ax1.twinx()
# Function to calculate the original frequency from the transformed frequency
def original_freq(transformed_freq):
return transformed_freq ** (-3/8)
# Get the transformed frequency values and calculate the corresponding original frequency
transformed_freq = frequency ** (-8/3)
original_freq_values = original_freq(transformed_freq)
# Set the secondary y-axis ticks and labels
ax2.set_yticks(transformed_freq)
ax2.set_yticklabels([f'{f:.0f} Hz' for f in original_freq_values], color='b')
# Set the secondary y-axis label
ax2.set_ylabel('Frequency (Hz)', color='b')
# Show the plot
plt.show()
Answered By - Ayalew Mohammed
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.