how can i plot means of a data set?

Illustration
junaidzaman - 2020-09-03T14:40:06+00:00
Question: how can i plot means of a data set?

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?

Expert Answer

Profile picture of Prashant Kumar 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:

In Python

Example 1: For a Simple 1D Array

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()

Example 2: For a 2D Array (Multiple Series)

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()

In MATLAB

Example 1: For a Simple 1D Array

 

% 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');

General Notes

  1. Choose whether to compute means across rows or columns based on the dataset's structure.
  2. Use appropriate axis labels and titles to clarify the plot.
  3. Add a legend or markers to distinguish the mean from other data.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!