【问题标题】:How to arrange matplotlib scatterplot surface data?如何排列 matplotlib 散点图表面数据?
【发布时间】:2014-06-16 16:39:01
【问题描述】:

我从 matplotlib 开始,想生成一个气泡图。我从the scatter demo 开始,但我不明白如何准备我的数据。我的目标是得到这个(表面的比例为 1:2:3:4)

我的代码是

import matplotlib.pyplot as plt

x = [1, 2]
y = [1, 2]
x_label = ["a", "b"]
y_label = ["x", "y"]
size = [100, 200, 300, 400] # I tried wild combinations here
plt.xticks(x, x_label)
plt.yticks(y, y_label)
plt.scatter(x, y, s=size, alpha=0.5)
plt.show()

这会生成以下图(正确的轴,两个相对大小为 1:2 的气泡)

不知道如何格式化数据输入,size应该由len(x)*len(y)元素组成,但是如何排列呢?

【问题讨论】:

  • 你只画了两个点;你需要x=[1,1,2,2]y=[1,2,1,2]
  • (然后将xticksyticks中的xy替换为[1,2][1,2]
  • 就是这样!谢谢。我现在明白我需要生成三个表中的所有三元组(以涵盖所有组合)。您介意将您的 cmets 复制/粘贴到答案中以便我接受吗?

标签: python matplotlib scatter-plot bubble-chart


【解决方案1】:

您的绘图命令错误;您需要为要绘制的每个点指定xy,因此xy 都有4 个值。

docs for more info(特别是关于x和y的形状)

import matplotlib.pyplot as plt

x = [1, 2,1,2]
y = [1, 1,2,2]
x_label = ["a", "b"]
y_label = ["x", "y"]
size = [100, 200, 300, 400] # I tried wild combinations here
plt.xticks([1,2], x_label)
plt.yticks([1,2], y_label)
plt.scatter(x, y, s=size, alpha=0.5)
plt.show()

创造

【讨论】: