【问题标题】:Changing Alignment of labels in Matplotlib Sankey Diagrams在 Matplotlib Sankey 图中更改标签的对齐方式
【发布时间】:2015-11-02 18:57:54
【问题描述】:

我正在尝试使 Matplotlib 中的 Sankey 函数对齐它的标签,以便图形更具可读性。

import numpy as np
import matplotlib.pyplot as plt

from matplotlib.sankey import Sankey

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Example Systems")
flows = [1, -.5, -.25,-.25]
sankey = Sankey(ax=ax, unit=None)
sankey.add(flows=flows, label='one',
           labels=['1', 'horizontal allignment is currently set to center.', 'it would be nice if I could change it to justify right.', '4'],
           pathlengths = [.5,.2,.5, .75],
           orientations=[0, -1,-1, 0])

diagrams = sankey.finish()
diagrams[-1].patch.set_hatch('/')
plt.legend(loc='best')
plt.show()

在 sankey.py 代码中,我发现水平对齐设置为 Center ha=center

Github source

 # Add the path labels.
        texts = []
        for number, angle, label, location in zip(flows, angles, labels,
                                                  label_locations):
            if label is None or angle is None:
                label = ''
            elif self.unit is not None:
                quantity = self.format % abs(number) + self.unit
                if label != '':
                    label += "\n"
                label += quantity
            texts.append(self.ax.text(x=location[0], y=location[1],
                                      s=label,
                                      ha='center', va='center'))
        # Text objects are placed even they are empty (as long as the magnitude
        # of the corresponding flow is larger than the tolerance) in case the
        # user wants to provide labels later.

是否有可能以某种方式使 Sankey.py 中的文本与默认值不同?

【问题讨论】:

    标签: python matplotlib pythonxy


    【解决方案1】:

    您可以使用finish() 返回的对象设置文本的对齐方式。同意,有人可能会主张在add() 中添加设置对齐的选项。

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Example Systems")
    flows = [1, -.5, -.25,-.25]
    sankey = Sankey(ax=ax, unit=None)
    sankey.add(flows=flows, label='one',
               labels=['1', 'horizontal allignment is currently set to center.', 'it would be nice if I could change it to justify right.', '4'],
               pathlengths = [.5,.2,.5, .75],
               orientations=[0, -1,-1, 0])
    
    diagrams = sankey.finish()
    diagrams[-1].patch.set_hatch('/')
    plt.legend(loc='best')
    
    for d in diagrams:
        for t in d.texts:
            t.set_horizontalalignment('left')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2017-07-17
      • 2015-03-21
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-23
      • 2017-02-02
      • 1970-01-01
      相关资源
      最近更新 更多