Как остановить перекрытие текста легенды matplotlib? - PullRequest
0 голосов
/ 11 июня 2019

Я использую python-3.x и хотел бы найти способ остановить легенду в правой части перекрывающейся строки, как вы можете видеть из следующего изображения:

enter image description here

Я пытаюсь сделать его похожим на следующее изображение: - (обратите внимание, что этот рисунок был изменен с помощью редактора изображений, чтобы уточнить, что я хочу)

enter image description here

Я пробовал много способов, но ни один из них не подходит для моего случая, такого как аннотирование.Как я могу остановить наложение текста легенды в matplotlib в моем случае?

Этот код, который я использую: (все используемые значения являются только примером)

data_1 = np.array([[0, 5, 3, 2 , 4, 7.7], [1, 1.5, 9, 7 , 8, 8], [2, 3, 3, 7 , 3, 3], [0, 5, 6, 12,4 , 3],[3, 5, 6, 10 ,2 , 6]])


df = pd.DataFrame({'111': data_1[0], '222': data_1[1], '333': data_1[2], '444': data_1[3], '555': data_1[4]})
# Graphing
#df.plot()
# 1. The color is a nice red / blue / green which is different from the primary color RGB
c = plt.get_cmap('Set1').colors
plt.rcParams['axes.prop_cycle'] = cycler(color = c)

fig, ax = plt.subplots(figsize = (7, 5))

# 2. Remove the legend
# 3. Make the line width thicker
df.plot(ax = ax, linewidth = 3, legend = False)

# 4. Display y-axis label
# 5. Change the display range of x-axis and y-axis
x_min, x_max = 0, 5
y_min, y_max = 0, 13
ax.set(ylim = (y_min, y_max), xlim = (x_min, x_max + 0.03))

# 6. Specify font size collectively
plt.rcParams["font.size"] = 14

# 7. Display graph title, X axis, Y axis name (label), grid line
plt.title("title")
plt.xlabel("x")
plt.ylabel("y")
plt.grid(True)

# 8. Remove the right and top frame
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(True)

# 9. Show index to the right of the plot instead of the normal legend
for i, name in enumerate(df.columns.values):
    ax.text(x_max + 0.03, ax.lines[i].get_data()[1][-1], name, color = f'C{i}', va = 'center')

plt.savefig('plot_lines.png', dpi = 300  ,bbox_inches = 'tight')

plt.show() 

Есть идеи?

Ответы [ 2 ]

0 голосов
/ 22 июня 2019

Ответ на этот вопрос может быть адаптирован к вашему случаю:

spread_labels

Идея состоит в том, чтобы создать график с таким количеством узлов, сколько у вас есть точек данных и связанных точек меток. Вы только хотите, чтобы узлы меток распространились. Это можно сделать с помощью графика network.spring_layout() (см. Документацию здесь ).

Прикрепленный код реализует функцию spring_labels() для ясности. Аргументы интересов

  • hint ( в виде массива ): некоторые точки данных имеют одинаковую координату y, поэтому график будет распределять соответствующие метки в одной и той же позиции. Вы можете присвоить разные значения каждой точке данных с помощью ключевого аргумента hint, чтобы повысить дифференцируемость (здесь я взял точку от второй до последней)
  • spread ( float ): это контролирует, как далеко распространяются узлы; это значение должно быть установлено вручную для оптимизации результатов
  • shift ( float ): сдвиг в направлении x метки x-координаты.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import networkx as nx

from cycler import cycler

def spring_labels(ax, x, y, labels, spread=.03, shift=.1, hint=None, colors=None):

    if hint is None:
        hint = y

    if colors is None:
        colors = ['C{}' for i in range(len(y))]

    # Create graph
    graph = nx.DiGraph()
    # node_labels = labels
    node_data = ['data_{}'.format(l) for l in labels]
    graph.add_nodes_from(node_data + labels)
    graph.add_edges_from(zip(node_data, labels))

    # Initialize position
    graph_init = dict()
    for yi, yh, nd, nl in zip(y, hint, node_data, labels):
        graph_init[nd] = (x, yi)
        graph_init[nl] = (x + shift, yi + (yi - yh) * .1)

    # Draw spring-force graph
    positions = nx.spring_layout(graph, pos=graph_init, fixed=node_data, k=spread)
    for label in labels:
        positions[label][0] = x + shift

    # colors = plt.rcParams['axes.color_cycle']
    # print(colors)
    for (data, label), color in zip(graph.edges, colors):
        ax.plot([positions[label][0], positions[data][0]],
                [positions[label][1], positions[data][1]],
                color=color, clip_on=False)
        ax.text(*positions[label], label, color=color)

data_1 = np.array([[0, 5, 3, 2, 4, 7.7], [1, 1.5, 9, 7, 8, 8], [
                  2, 3, 3, 7, 3, 3], [0, 5, 6, 12, 4, 3], [3, 5, 6, 10, 2, 6]])

df = pd.DataFrame({'111': data_1[0], '222': data_1[1], '333': data_1[
                  2], '444': data_1[3], '555': data_1[4]})
# Graphing
# df.plot()
# 1. The color is a nice red / blue / green which is different from the
# primary color RGB
c = plt.get_cmap('Set1').colors
plt.rcParams['axes.prop_cycle'] = cycler(color=c)

fig, ax = plt.subplots(figsize=(7, 5))

# 2. Remove the legend
# 3. Make the line width thicker
df.plot(ax=ax, linewidth=3, legend=False)

# 4. Display y-axis label
# 5. Change the display range of x-axis and y-axis
x_min, x_max = 0, 5
y_min, y_max = 0, 13
ax.set(ylim=(y_min, y_max), xlim=(x_min, x_max + 0.03))

# 6. Specify font size collectively
plt.rcParams["font.size"] = 14

# 7. Display graph title, X axis, Y axis name (label), grid line
plt.title("title")
plt.xlabel("x")
plt.ylabel("y")
plt.grid(True)

# 8. Remove the right and top frame
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(True)

# 9. Show index to the right of the plot instead of the normal legend
ys_hint = [a.get_data()[1][-2] for a in ax.lines]
ys_max = [a.get_data()[1][-1] for a in ax.lines]
spring_labels(ax, x_max, ys_max, df.columns.values, shift=.2, hint=ys_hint, colors=c)

plt.savefig('plot_lines.png', dpi=300, bbox_inches='tight')
0 голосов
/ 11 июня 2019

Это может считаться худшим хаком в истории matplotlib, но, по крайней мере, для вашего текущего примера, это то, что я только что придумал вкратце.Это еще не идеально, и я был бы счастлив удалить его, если он бесполезен.Но так как я дал некоторое время, чтобы пойти так далеко, я подумал поделиться им с вами.

Идея состоит в том, чтобы отсортировать значения y, а затем использовать коэффициент масштабирования, добавляющий смещение, где смещение вручную установлено на0 для индекса оранжевой кривой, который равен 2 (третье значение в порядке возрастания), и поэтому (i-2)

yvals = np.array([ax.lines[i].get_data()[1][-1] for i in range(len(df.columns.values))])
indexs = np.argsort(yvals)
values = df.columns.values

for i, idx in enumerate(indexs):
    print (yvals[idx]+0.5*i)
    ax.text(x_max + 0.1, yvals[idx]+0.45*(i-2), name, color = f'C{idx}', va = 'center')

enter image description here

...