使用颜色谱在散点图中显示信息。

huangapple go评论64阅读模式
英文:

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=&#39;3d&#39;)

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(&#39;X&#39;)
plt.ylabel(&#39;Y&#39;)

color = plt.rcParams[&#39;axes.prop_cycle&#39;]()
proxies = [plt.Rectangle((0, 0), 1, 1, **next(color)) for _ in informations[:,0]]
labels = &#39;Apricots Bananas Cherries Dates Elderberries&#39;.split()
ax1.legend(proxies, labels, bbox_to_anchor=(1.05, 0.50), loc=&#39;center left&#39;, frameon=0)

plt.show()

huangapple
  • 本文由 发表于 2023年5月26日 13:02:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定