首页 > 解决方案 > 如何停止 matplotlib 图例文本重叠?

问题描述

我正在使用 python-3.x,我想找到一种方法来停止重叠线右侧的图例,如下图所示:

在此处输入图像描述

我试图让它看起来类似于下 图:-(注意这个图是使用图像编辑器修改的,只是为了澄清我想要的)

在此处输入图像描述

我尝试了很多方法,但它们都不适用于我的情况,例如注释。在我的情况下,如何阻止 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() 

有任何想法吗?

标签: python-3.xnumpymatplotliblegend

解决方案


这个问题的答案可以根据您的情况进行调整:

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')

推荐阅读