produce the figure similar to this one in Matlab
12 views (last 30 days)
Show older comments
i wrote this code to produce the chart in python, can any one reproduce it in Matlab to produce the same figure, i need it urgently, please. this chart i made it using Gemini Ai in colab but the quality of images is very bad and i want to produce it in Matlab so that the quality be better, please. i wrote the following code using python and i want it in Matlab, please, urgently, help.

import matplotlib.pyplot as plt
import numpy as np
# Define the number of groups and categories per group
num_groups = 7 # Changed from 4 to 7
num_categories_per_group = 7
# Define 7 distinct colors for categories (reused per group)
colors = [
'#007F7F', '#800000', '#000080', '#808000', '#800080',
'#008000', '#FF4500'
]
# Define custom 'before' and 'after' values for each group
custom_group_data = [
{
'before': np.array([20, 30, 25, 40, 15, 35, 22]),
'after': np.array([25, 28, 30, 35, 18, 38, 28])
},
{
'before': np.array([10, 20, 18, 25, 12, 28, 15]),
'after': np.array([12, 22, 20, 28, 15, 30, 18])
},
{
'before': np.array([30, 35, 28, 45, 20, 40, 32]),
'after': np.array([32, 38, 30, 48, 23, 42, 35])
},
{
'before': np.array([18, 22, 20, 30, 10, 25, 15]),
'after': np.array([20, 25, 22, 33, 12, 28, 18])
},
{
'before': np.array([22, 28, 32, 38, 17, 30, 25]), # New data for Group 5
'after': np.array([24, 30, 35, 40, 19, 33, 27])
},
{
'before': np.array([15, 19, 23, 27, 11, 26, 19]), # New data for Group 6
'after': np.array([17, 21, 25, 29, 13, 28, 21])
},
{
'before': np.array([35, 40, 33, 50, 25, 45, 38]), # New data for Group 7
'after': np.array([37, 42, 36, 52, 27, 48, 40])
}
]
plt.figure(figsize=(15, 5)) # Decreased height and increased width for Word document
# Generate and plot data for each group
for group_idx in range(num_groups):
group_name = f'Group {group_idx + 1}'
categories = [f'Category {i+1}' for i in range(num_categories_per_group)]
# Use custom values for the current group
before_values = custom_group_data[group_idx]['before']
after_values = custom_group_data[group_idx]['after']
# Ensure 'after' values are not negative (though with custom values, this might be handled directly)
after_values[after_values < 0] = 0
# Plot each line within the current group
for i, category in enumerate(categories):
# Label each category only once (when group_idx is 0) to avoid duplicate legend entries
plt.plot(
[group_idx - 0.3, group_idx + 0.3], # Increased offset from 0.2 to 0.3
[before_values[i], after_values[i]],
marker='o',
linestyle='-',
color=colors[i],
label=f'{category}' if group_idx == 0 else '', # Use only 7 labels for categories
alpha=0.7
)
# Get current axes to modify tick parameters and set ylim
ax = plt.gca()
# Adjust y-axis limits to create space for 'Before'/'After' labels below the plot area
current_ymin, current_ymax = ax.get_ylim()
# Extend y-axis downwards by a percentage of the total y-range to make space
new_ymin = current_ymin - (current_ymax - current_ymin) * 0.15
ax.set_ylim(bottom=new_ymin)
# Re-get y_label_pos after setting new y-limit to ensure it's at the very bottom
y_label_pos = ax.get_ylim()[0]
for group_idx in range(num_groups):
# Add 'before' and 'after' labels to the x-axis for each group
# Placed at the new lower y-limit with increased font size
plt.text(group_idx - 0.3, y_label_pos, 'Before', ha='center', va='top', fontsize=12, color='gray') # Increased offset
plt.text(group_idx + 0.3, y_label_pos, 'After', ha='center', va='top', fontsize=12, color='gray') # Increased offset
# Add labels and title
plt.xticks(range(num_groups), [f'Group {i+1}' for i in range(num_groups)], fontsize=14) # Increased x-axis label font size
plt.ylabel('Value', fontsize=14) # Increased y-axis label font size
plt.title('Before and After Comparison for Multiple Groups and Categories', fontsize=16) # Increased title font size
plt.legend(title='Category', bbox_to_anchor=(1.05, 1), loc='upper left') # Ensure legend is displayed
plt.grid(True, linestyle='--', alpha=0.7)
# Adjust x-axis tick label padding to move them further down
ax.tick_params(axis='x', which='both', pad=15) # Increased pad to move x-tick labels down
plt.tight_layout()
plt.show()
0 Comments
Answers (1)
Mathieu NOE
8 minutes ago
hello
maybe this ? (a quick and dirty attempt )
the plot rendering may differ on your PC vs here below :
groups = 3; % data groups
samples = 10; % samples (identical for each group in this example)
%% main code
figure
hold on
grid on
xlim([0 groups*3+1])
ax = gca;
% init
x_labels_store = [];
% main loop
for k = 1:groups
y_before = rand(samples,1);
y_after = rand(samples,1);
x_center = 3*k - 1 ;
x_tick_before = (x_center-1);
x_tick_after = (x_center+1);
% plot each individual lines
for c = 1:samples
color = rand(3,1);
plot([x_tick_before x_tick_after],[y_before(c) y_after(c)],'.','Color',color,'MarkerSize',25)
plot([x_tick_before x_tick_after],[y_before(c) y_after(c)],'-','Color',color,'LineWidth',2)
end
% create some labels
tmp = {"Before","" ,"After"};
x_labels_store = [x_labels_store tmp];
end
set(ax, 'Ticklength', [0 0]);
set(ax,"XTick",(1:groups*3),"XTickLabel",x_labels_store)
for i = 1:groups
text(3*i-1, ax.YLim(1)-0.1, ['GROUP ' num2str(i)], ...
'horizontalalignment', 'center', 'verticalalignment', 'top','FontSize',16);
end
ax.XLabel.String = sprintf('\n\n\n%s', 'My nice X axis');
0 Comments
See Also
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!