具有 4 种视觉编码的 Matplotlib 散点图
Matplotlib Scatterplot with 4 visual encondings
第一步是一个包含多列的 pandas 数据框。
我做的第二步是使用 to_numpy()
函数将此 Dataframe 的某些列转换为 Numpy 数组。
我检索到如下内容:
[[100 200 3.5 1] [100 200 3.5 1] [100 300 6.2 1] [200 125 4.2 1] [100 300 6.2 1] [100 200 3.5 1]]
第一个元素想象的是原始ID
第二个元素是命运 id
第三是起源与命运之间的距离
第 4 个只是一个计数器(1 个元素)(我将它包括在内只是因为我认为可能需要计算元素。如果您提出的解决方案不使用它,请忽略它)
我想要一个具有以下规格的散点图:
- origing_id x 轴
y轴- destiny_id
- 暖色散点的颜色
表示两点之间的距离(第三个元素)
- 大小
散点取决于 origins_id 的对数
/destiny_id 我们 have.for 例如我们有三个 100 200
组合。所以它的尺寸应该比
的尺寸大
只有一个条目的组合 200 125。
我已经尝试过,但我无法在此图中包含所有先决条件。
这在 matplotlib 中如何实现?或者有没有其他更简单的方法直接使用 pandas?
如果我正确理解了您的要求,这应该可以解决问题:
import matplotlib.pyplot as plt
import numpy as np
data = np.array([[100,200,3.5,1],[100,200,3.5,1],[100,300,6.2,1],[200,125,4.2,1],[100,300,6.2,1],[100,200,3.5,1]])
unique, counts = np.unique(data, axis=0, return_counts=True)
x = unique[:,0]
y = unique[:,1]
c = unique[:,2]
## figure out a nice looking scaling factor here
# and remember that the scatter point size is supposed to be an area,
# hence squaring a base factor is ideal
s = (counts*10)**2
fig, ax = plt.subplots()
sca = ax.scatter(x,y,c=c,s=s)
plt.colorbar(sca)
plt.show()
产生:
第一步是一个包含多列的 pandas 数据框。
我做的第二步是使用 to_numpy()
函数将此 Dataframe 的某些列转换为 Numpy 数组。
我检索到如下内容:
[[100 200 3.5 1] [100 200 3.5 1] [100 300 6.2 1] [200 125 4.2 1] [100 300 6.2 1] [100 200 3.5 1]]
第一个元素想象的是原始ID 第二个元素是命运 id 第三是起源与命运之间的距离 第 4 个只是一个计数器(1 个元素)(我将它包括在内只是因为我认为可能需要计算元素。如果您提出的解决方案不使用它,请忽略它)
我想要一个具有以下规格的散点图:
- origing_id x 轴 y轴
- destiny_id
- 暖色散点的颜色 表示两点之间的距离(第三个元素)
- 大小
散点取决于 origins_id 的对数
/destiny_id 我们 have.for 例如我们有三个 100 200
组合。所以它的尺寸应该比
的尺寸大 只有一个条目的组合 200 125。
我已经尝试过,但我无法在此图中包含所有先决条件。 这在 matplotlib 中如何实现?或者有没有其他更简单的方法直接使用 pandas?
如果我正确理解了您的要求,这应该可以解决问题:
import matplotlib.pyplot as plt
import numpy as np
data = np.array([[100,200,3.5,1],[100,200,3.5,1],[100,300,6.2,1],[200,125,4.2,1],[100,300,6.2,1],[100,200,3.5,1]])
unique, counts = np.unique(data, axis=0, return_counts=True)
x = unique[:,0]
y = unique[:,1]
c = unique[:,2]
## figure out a nice looking scaling factor here
# and remember that the scatter point size is supposed to be an area,
# hence squaring a base factor is ideal
s = (counts*10)**2
fig, ax = plt.subplots()
sca = ax.scatter(x,y,c=c,s=s)
plt.colorbar(sca)
plt.show()
产生: