带有散景的全息视图上的和弦图的倒置标签文本(半圈)

问题描述 投票:0回答:2

https://holoviews.org/reference/elements/bokeh/Chord.html 中的示例中,如何将标签文本的方向更改 180° 以避免旋转导致标签文本上下颠倒左半圆(见图)。后端是散景的。通过此更改,标签文本将更具可读性。

这是我到目前为止所做的:

import numpy as np

def rotate_label(plot, element):
        angles = plot.handles['text_1_source'].data['angle']
        angles[np.where((angles < -1.5707963267949) | (angles > 1.5707963267949))] += 3.1415926535898

chord.opts(cmap='Category20b',
           edge_cmap='Category20b', 
           edge_color=dim('source').str(), 
           labels='index', 
           node_color=dim('index').str(),
           hooks=[rotate_label]
           )

第一张图片(当前):

第二张图(目标):

python data-visualization bokeh holoviews chord-diagram
2个回答
1
投票

这是相当困难的。如果标签文本只有几个字符长,那么带有钩子的解决方案可能是合适的。我确信代码可以写得更清楚,但我没有 numpy 的经验。

import numpy as np

def rotate_label(plot, element):
    white_space = "      "
    angles = plot.handles['text_1_source'].data['angle']
    characters = np.array(plot.handles['text_1_source'].data['text'])
    plot.handles['text_1_source'].data['text'] = np.array([x + white_space if x in characters[np.where((angles < -1.5707963267949) | (angles > 1.5707963267949))] else x for x in plot.handles['text_1_source'].data['text']])
    plot.handles['text_1_source'].data['text'] = np.array([white_space + x if x in characters[np.where((angles > -1.5707963267949) | (angles < 1.5707963267949))] else x for x in plot.handles['text_1_source'].data['text']])
    angles[np.where((angles < -1.5707963267949) | (angles > 1.5707963267949))] += 3.1415926535898
    plot.handles['text_1_glyph'].text_align = "center"

chord.opts(cmap='Category20b',
           edge_cmap='Category20b', 
           edge_color=dim('source').str(), 
           labels='index', 
           node_color=dim('index').str(),
           hooks=[rotate_label]
           )

变量

white_space
包含几个空白字符,如有必要,可以调整这些空白字符以定位标签文本。
居中标签文本并不理想 (
plot.handles['text_1_glyph'].text_align = "center"
)。更好的解决方案是将左半圆的标签文本与
right
对齐,右半圆的标签文本与
left
对齐。但我不知道该怎么做。


0
投票

在你的代码中。 text_1_source 是什么?

© www.soinside.com 2019 - 2024. All rights reserved.