Изменение выравнивания меток в диаграммах Санки Matplotlib

Санки

Я пытаюсь сделать функцию Sankey в Matplotlib, чтобы выровнять ее метки, чтобы график был более читаемым.

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

 # 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 выровняться иначе, чем по умолчанию?


person eagletusk    schedule 02.11.2015    source источник


Ответы (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()

введите здесь описание изображения

person Jordi    schedule 27.11.2017