气泡矩阵图(Bubble Matrix Plot),通常用于显示三个变量之间的关系。这种图表类型将数据点表示为气泡的形式,其中气泡的大小通常表示第三个数值变量的大小。图表的X轴和Y轴代表两个分类或定量变量。颜色可能代表另一个分类变量或是另一个连续变量的梯度,这里颜色的深浅对应于颜色条(Color Bar)上的值。
import matplotlib.pyplot as plt
import numpy as np
# Generate example data
x_categories = ['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10']
y_categories = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10']
x = np.repeat(range(len(x_categories)), len(y_categories))
y = np.tile(range(len(y_categories)), len(x_categories))
bubble_sizes = np.random.uniform(10, 1000, size=len(x)) # Bubble sizes
colors = np.random.uniform(0.1, 1, size=len(x)) # Color values
# Create the bubble matrix plot
plt.figure(figsize=(10, 8))
scatter = plt.scatter(x, y, s=bubble_sizes, c=colors, cmap='viridis', alpha=0.6, edgecolors="w", linewidth=0.5)
# Add titles and labels
plt.title('Bubble Matrix Plot Example')
plt.xlabel('XAxis')
plt.ylabel('YAxis')
# Change the axis to show the category names
plt.xticks(ticks=np.arange(len(x_categories)), labels=x_categories)
plt.yticks(ticks=np.arange(len(y_categories)), labels=y_categories)
# Add a color bar
plt.colorbar(scatter, label='Color Bar')
# Add a legend for sizes
for size in [100, 300, 600]:
plt.scatter([], [], c='k', alpha=0.3, s=size, label=str(size))
plt.legend(scatterpoints=1, frameon=False, labelspacing=1, title='Bubble Sizes')
plt.grid(True)
plt.show()
为了进一步美化,我们使用了Spectral色彩映射来提供一个更丰富的颜色梯度。调整了标题和标签的字体大小,以提高可读性。修改了颜色条和图例,使其更具有信息性且易于阅读。添加了网格线,并调整了其样式以更加微妙且不干扰数据的展示。
% MATLAB code to create an aesthetic bubble matrix plot
% Generate some example data
x_categories = 1:10;
y_categories = 1:10;
[X, Y] = meshgrid(x_categories, y_categories);
bubbleSizes = reshape(rand(size(X)), [], 1) * 100; % Random bubble sizes
colors = reshape(rand(size(X)), [], 1); % Random colors for the bubbles
% Create the figure
figure;
% Create the bubble plot using scatter
scatter(X(:), Y(:), bubbleSizes, colors, 'filled');
% Improve aesthetics
colormap('jet'); % Use the 'jet' colormap for color coding the bubbles
colorbar; % Show a color bar
title('Aesthetic Bubble Matrix Plot');
xlabel('XAxis');
ylabel('YAxis');
% Change the axis to show the category names
xticks(x_categories);
xticklabels(arrayfun(@num2str, x_categories, 'UniformOutput', false));
yticks(y_categories);
yticklabels(arrayfun(@num2str, y_categories, 'UniformOutput', false));
% Add a grid
grid on;
set(gca, 'GridLineStyle', '--', 'GridColor', [0.5, 0.5, 0.5], 'GridAlpha', 0.7); % Use a gray color for the grid
% There is no tight_layout in MATLAB, but you can manually adjust subplot margins or use 'axis tight'
% axis tight; % Uncomment this if you want to remove white space around the axes
% Add a legend for the bubble sizes
lgd = legend('Bubble Sizes');
title(lgd, 'Legend');
为了进一步美化,我们使用了更现代的parula颜色映射,并为每个气泡添加了黑色的边缘,以便更好地与背景区分。同时,我对颜色条标签和标题的字体大小进行了调整,并设置了图例的位置和样式,使其更加美观。