To change the x-axis from "Samples" to "Time" in your plot, you can create a time array where each time point is calculated as sample * 0.1. Here's how you can modify your code to achieve this:
-
Import Necessary Libraries: Ensure that you have imported numpy for numerical operations and matplotlib.pyplot for plotting.
-
Create a Time Array: Assuming that your samples start at 1, you can create a time array using numpy.arange. Multiply each sample number by 0.1 to get the corresponding time.
-
Plot Using the Time Array: Use the time array as the x-axis values in your plot instead of the default sample indices.
-
Update Axis Labels: Change the x-axis label from "Samples" to "Time" and specify the appropriate units if necessary.
Here's the complete modified code with these changes:
import numpy as np
import matplotlib.pyplot as plt
# Assuming y_test_original and y_pred are your data arrays
# and Name and Units are defined strings representing the ylabel
# Create a time array
num_samples = len(y_test_original)
time = np.arange(1, num_samples + 1) * 0.1 # If samples start at 1
# Alternatively, if samples start at 0:
# time = np.arange(num_samples) * 0.1
# Plotting
plt.figure(figsize=(12, 6))
plt.plot(time, y_test_original, label='Actual Cable Response', alpha=0.7)
plt.plot(time, y_pred, label='Predicted Cable Response', alpha=0.7)
plt.title('Actual vs Predicted - All Samples')
plt.xlabel('Time (units)') # Replace 'units' with the actual time unit, e.g., seconds
plt.ylabel(f"{Name} {Units}") # Ensure Name and Units are defined properly
plt.legend()
plt.grid(True) # Optional: Add a grid for better readability
plt.tight_layout() # Optional: Adjust subplot params for better layout
plt.show()
Explanation of the Changes:
-
Creating the Time Array:
-
Plotting with Time on the X-Axis:
- Pass the
time array as the first argument to plt.plot() to set it as the x-axis.
- This replaces the default behavior where the x-axis is the sample index.
-
Updating Labels:
-
Optional Enhancements:
- Grid:
plt.grid(True) adds a grid to the plot, which can help in visually estimating values.
- Layout Adjustment:
plt.tight_layout() adjusts the padding of the plot to prevent clipping of labels and titles.
Example with Sample Data
Here's a complete example using some sample data to illustrate the changes:
import numpy as np
import matplotlib.pyplot as plt
# Sample data
num_samples = 100
y_test_original = np.sin(np.linspace(0, 10, num_samples))
y_pred = np.sin(np.linspace(0, 10, num_samples) + 0.5) # Predicted with a phase shift
Name = "Voltage"
Units = "(V)"
# Create a time array
time = np.arange(1, num_samples + 1) * 0.1
# Plotting
plt.figure(figsize=(12, 6))
plt.plot(time, y_test_original, label='Actual Cable Response', alpha=0.7)
plt.plot(time, y_pred, label='Predicted Cable Response', alpha=0.7)
plt.title('Actual vs Predicted - All Samples')
plt.xlabel('Time (s)') # Assuming time is in seconds
plt.ylabel(f"{Name} {Units}")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
This will produce a plot with the x-axis labeled as "Time (s)" instead of "Samples," showing the relationship between the actual and predicted cable responses over time.
Troubleshooting Tips
-
Mismatch in Array Lengths: Ensure that y_test_original and y_pred have the same length as the time array. Mismatched lengths will cause errors.
-
Units Clarification: Clearly specify the time units in the x-axis label for better understanding.
-
Responsive Design: If you're working in a Jupyter notebook or similar environment, ensure that plots are rendered correctly by using %matplotlib inline or the appropriate magic command.
Feel free to adjust the code according to your specific requirements and data structures.