13

我正在尝试从模型中绘制主题可视化。我想做一些像散景协方差实现这样的事情。

我的数据是:

data 1: index,                            topics.   
data 2: index, topics, weights(use it for color). 

主题只是一组单词。

我如何将数据提供给散景以绘制上述数据?从示例中可以看出,数据处理并不直观。

使用 matplot,它看起来像这样
显然,查看每个圆圈对应的主题在视觉上没有帮助。这是我的 matplotlib 代码:

x = []
y = []
area = []

for row in joined:
      x.append(row['index']) 
      y.append(row['index'])
      #weight.append(row['score'])
      area.append(np.pi * (15 * row['score'])**2)
scale_values = 1000
plt.scatter(x, y, s=scale_values*np.array(area), alpha=0.5)
plt.show()

有什么想法/建议吗?

4

1 回答 1

15

更新:下面的答案在所有主要观点上仍然是正确的,但是从 Bokeh 0.7 开始,API 已经略有变化,变得更加明确。一般来说,像:

rect(...)

应该替换为

p = figure(...)
p.rect(...)

以下是 Les Mis 示例中的相关行,简化为您的案例。让我们来看看:

# A "ColumnDataSource" is like a dict, it maps names to columns of data.
# These names are not special we can call the columns whatever we like.
source = ColumnDataSource(
    data=dict(
        x = [row['name'] for row in joined],
        y = [row['name'] for row in joined],
        color = list_of_colors_one_for_each_row, 
    )
)

# We need a list of the categorical coordinates
names = list(set(row['name'] for row in joined))

# rect takes center coords (x,y) and width and height. We will draw 
# one rectangle for each row.
rect('x', 'y',        # use the 'x' and 'y' fields from the data source
     0.9, 0.9,        # use 0.9 for both width and height of each rectangle 
     color = 'color', # use the 'color' field to set the color
     source = source, # use the data source we created above
     x_range = names, # sequence of categorical coords for x-axis
     y_range = names, # sequence of categorical coords for y-axis
)

几点注意事项:

  • 对于数字数据x_rangey_range通常会自动提供。我们必须在这里明确给出它们,因为我们使用的是分类坐标。

  • 您可以按照自己的喜好对名称列表进行排序x_rangey_range这是它们在绘图轴上显示的顺序。

  • 我假设您想使用分类坐标。:) 这就是 Les Mes 示例所做的。如果您需要数字坐标,请参阅此答案的底部。

此外,Les Mis 示例稍微复杂一些(它有一个悬停工具),这就是我们手动创建 ColumnDataSource 的原因。如果您只需要一个简单的绘图,您可以跳过自己创建数据源,直接将数据传递给rect

names = list(set(row['name'] for row in joined))

rect(names,    # x (categorical) coordinate for each rectangle
     names,    # y (categorical) coordinate for each rectangle
     0.9, 0.9, # use 0.9 for both width and height of each rectangle
     color = some_colors, # color for each rect
     x_range = names, # sequence of categorical coords for x-axis
     y_range = names, # sequence of categorical coords for y-axis
)

另一个注意事项:这仅在对角线上绘制矩形,其中 x 和 y 坐标相同。从您的描述中,这似乎是您想要的。但为了完整起见,可以绘制具有不同 x 和 y 坐标的矩形。Les Mis 示例就是这样做的。

最后,也许您实际上并不想要分类轴?如果您只想使用坐标的数字索引,则更简单:

inds = [row['index'] for row in joined]

rect(inds,    # x-coordinate for each rectangle
     inds,    # y-coordinate for each rectangle
     0.9, 0.9, # use 0.9 for both width and height of each rectangle
     color = some_colors, # color for each rect
)

编辑:这是一个使用数字坐标的完整可运行示例:

from bokeh.plotting import * 

output_file("foo.html")

inds = [2, 5, 6, 8, 9]
colors = ["red", "orange", "blue", "green", "#4488aa"]

rect(inds, inds, 1.0, 1.0, color=colors)

show()

这是一个使用与分类坐标相同的值:

from bokeh.plotting import * 

output_file("foo.html")

inds = [str(x) for x in [2, 5, 6, 8, 9]]
colors = ["red", "orange", "blue", "green", "#4488aa"]

rect(inds, inds, 1.0, 1.0, color=colors, x_range=inds, y_range=inds)

show()
于 2014-03-29T09:28:56.653 回答