英文:
Showing information in scatterplot using a color spectrum
问题
# 给定数据
x_values = np.array([1, 2, 3, 4, 5, 6])
y_values = np.array([4, 6, 3, 5, 7, 6])
information = np.array([
[0.3, 0.5, 0.1, 0.1, 0.2, 0.1],
[0.1, 0.2, 0.1, 0.1, 0.1, 0.5],
[0.5, 0.1, 0.1, 0.1, 0.5, 0.1],
[0.1, 0.1, 0.2, 0.5, 0.1, 0.1],
[0.0, 0.1, 0.5, 0.2, 0.1, 0.2]])
# 我有x和y值以及一些信息要在散点图上用颜色显示。
# 信息数组的每一列代表每个(x,y)点的5个参数。
# 我想用颜色的光谱来显示这些参数的参与度。我该怎么做?
英文:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
# Given data
x_values = np.array([1, 2, 3, 4, 5, 6])
y_values = np.array([4, 6, 3, 5, 7, 6])
information = np.array([
[0.3, 0.5, 0.1, 0.1, 0.2, 0.1],
[0.1, 0.2, 0.1, 0.1, 0.1, 0.5],
[0.5, 0.1, 0.1, 0.1, 0.5, 0.1],
[0.1, 0.1, 0.2, 0.5, 0.1, 0.1],
[0.0, 0.1, 0.5, 0.2, 0.1, 0.2]])
I have x and y values and some information to show using color on a scatter plot. Each of the column from the information array represents 5 parameters for each (x,y) point. I want to show the participation of those parameters using a spectrum of color. How can I do it?
答案1
得分: 2
这是一个合理的解决方案,使用 mpl_toolkits.mplot3d.axes3d.Axes3D.bar3d
正如你所见,有一个讨厌的伪影...,此外似乎存在一个 Matplotlib 的 bug,导致无法添加一个迫切需要的图例(参见我的这个问题)。我使用 Trenton McKinney 在 这条评论 中提供的信息来添加了迫切需要的图例 — 谢谢 Trenton。
以下是代码,唯一的技巧是如何计算 bases
数组:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([4, 6, 3, 5, 7, 6])
informations = np.array([
[0.3, 0.5, 0.1, 0.1, 0.2, 0.1],
[0.1, 0.2, 0.1, 0.1, 0.1, 0.5],
[0.5, 0.1, 0.1, 0.1, 0.5, 0.1],
[0.1, 0.1, 0.2, 0.5, 0.1, 0.1],
[0.0, 0.1, 0.5, 0.2, 0.1, 0.2]])
bases = np.vstack(([0]*len(x), informations.cumsum(axis=0)))[:-1]
fig = plt.figure(figsize=(8, 6))
ax1 = fig.add_subplot(111, projection='3d')
for n, (base, info) in enumerate(zip(bases, informations)):
ax1.bar3d(x-0.5, y-0.5, base, 1, 1, info, label=str(n), shade=True)
plt.xlabel('X')
plt.ylabel('Y')
color = plt.rcParams['axes.prop_cycle']()
proxies = [plt.Rectangle((0, 0), 1, 1, **next(color)) for _ in informations[:,0]]
labels = 'Apricots Bananas Cherries Dates Elderberries'.split()
ax1.legend(proxies, labels, bbox_to_anchor=(1.05, 0.50), loc='center left', frameon=0)
plt.show()
英文:
I think this is a reasonable solution, using mpl_toolkits.mplot3d.axes3d.Axes3D.bar3d
As you can see, there is a nasty artifact..., <strike><strike>further there is (so it seems) a Matplotlib bug that prevents adding a legend, that is sorely needed (see this question of mine).</strike></strike> I've added the sorely needed legend using the info Trenton McKinney provided in this comment — thank you Trenton.
Here it's the code, the only trick is how the bases
array is computed
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([4, 6, 3, 5, 7, 6])
informations = np.array([
[0.3, 0.5, 0.1, 0.1, 0.2, 0.1],
[0.1, 0.2, 0.1, 0.1, 0.1, 0.5],
[0.5, 0.1, 0.1, 0.1, 0.5, 0.1],
[0.1, 0.1, 0.2, 0.5, 0.1, 0.1],
[0.0, 0.1, 0.5, 0.2, 0.1, 0.2]])
bases = np.vstack(([0]*len(x), informations.cumsum(axis=0)))[:-1]
fig = plt.figure(figsize=(8, 6))
ax1 = fig.add_subplot(111, projection='3d')
for n, (base, info) in enumerate(zip(bases, informations)):
ax1.bar3d(x-0.5, y-0.5, base, 1, 1, info, label=str(n), shade=True)
plt.xlabel('X')
plt.ylabel('Y')
color = plt.rcParams['axes.prop_cycle']()
proxies = [plt.Rectangle((0, 0), 1, 1, **next(color)) for _ in informations[:,0]]
labels = 'Apricots Bananas Cherries Dates Elderberries'.split()
ax1.legend(proxies, labels, bbox_to_anchor=(1.05, 0.50), loc='center left', frameon=0)
plt.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论