i have an array of variable y, 32 x 247 in dimension and which i wish to work out the column means for and then plot in order to represent a mean graph of the 32 individual signals. i can access the means to each column in 'data statistics' of the plot function, but cannot then make the means be represented in graphical form on the same axes. Data=rand(32,247); % Example data_mean=mean(Data,2); plot(data_mean); - but the output is only 32 mean values, of each row, rather than 247 mean values, of each column. how can i obtain the means of the columns rather than the rows?
Prashant Kumar answered .
2025-11-20
To plot the means of a dataset, follow these steps depending on the structure and format of your data. Here are examples in Python and MATLAB:
If you have a single array, calculate its mean and plot it as a horizontal line:
import numpy as np
import matplotlib.pyplot as plt
# Example dataset
data = np.random.randn(100)
# Calculate the mean
mean_value = np.mean(data)
# Plot the data and the mean
plt.plot(data, label="Data")
plt.axhline(y=mean_value, color='r', linestyle='--', label=f"Mean: {mean_value:.2f}")
plt.legend()
plt.xlabel("Index")
plt.ylabel("Value")
plt.title("Data with Mean")
plt.show()
If you have multiple data series, calculate and plot their means:
# Example 2D dataset
data = np.random.randn(10, 50) # 10 rows, 50 columns
# Calculate means across rows (axis=1) or columns (axis=0)
row_means = np.mean(data, axis=1)
col_means = np.mean(data, axis=0)
# Plot the column means
plt.plot(col_means, label="Mean of Columns")
plt.legend()
plt.xlabel("Index")
plt.ylabel("Mean Value")
plt.title("Column Means")
plt.show()
% Example dataset
data = randn(1, 100);
% Calculate the mean
mean_value = mean(data);
% Plot the data and the mean
plot(data, 'b');
hold on;
yline(mean_value, 'r--', 'Mean');
legend('Data', 'Mean');
xlabel('Index');
ylabel('Value');
title('Data with Mean');
hold off;
Example 2: For a 2D Array (Multiple Series)
% Example 2D dataset
data = randn(10, 50); % 10 rows, 50 columns
% Calculate means across rows or columns
row_means = mean(data, 2); % Row means
col_means = mean(data, 1); % Column means
% Plot column means
plot(col_means, 'b');
xlabel('Index');
ylabel('Mean Value');
title('Column Means');