简体   繁体   English

如果绘制了多个图形,如何获得该线的 label?

[英]How to get the line's label if multiple graphs have been plotted?

plt.plot(x, y, label = name1)
plt.plot(x, y, label = name2)
plt.plot(x, y, label = name3)
plt.show()

How to get the label when I click the line or better if I can get this information directly in the graph window like I get the x and y axis values on bottom right.当我单击该线时如何获得 label 或更好,如果我可以直接在图表 window 中获得此信息,就像我在右下角获得 x 和 y 轴值一样。

The fastest way would be to add a legend to your graph with plt.legend() right before plt.show()最快的方法是在plt.legend() plt.show()图表中添加图例

For more interactivity, maybe try bokeh instead of matplotlib.要获得更多交互性,可以尝试散景而不是 matplotlib。

Not sure exactly what you are asking for but if you want to represent each line with a name, or the series of the x-values, you could use legend() and input a string or series name as label name in the plot-line:不确定您到底要什么,但如果您想用名称或 x 值系列表示每一行,您可以使用legend()并在绘图线中输入一个字符串或系列名称作为 label 名称:

plt.plot(x1, y, label = "name1")   # Show the string name1
plt.plot(x2, y, label = x2)        # Shows the array x2
plt.legend()                       # Displays the legends

If you want to add title or labels for the axis you could use:如果要为轴添加标题或标签,可以使用:

plt.xlabel('X-axis Label')
plt.ylabel('Y-axis label')
plt.title('Title')

在此处输入图像描述

I am not sure if this is what you are looking for?我不确定这是否是您要找的东西? But you can easily name your graphs by using the Legend.但是您可以使用图例轻松命名您的图表。 the first graph will be the first in your Legendlist.第一个图表将是您 Legendlist 中的第一个。 The important code is between the slash:-)重要的代码在斜杠之间:-)

import matplotlib.pyplot as plt
import numpy as np


# Select length of axes and the space between tick labels
xmin, xmax, ymin, ymax = -10, 10, -10, 10
ticks_frequency = 1

# Plot points
fig, ax = plt.subplots(figsize=(10, 10))

#//////////////////////////////////////////////////////////////////////////////

# x range
x = np.arange(-5, 5., 0.025)

# f1
y1 = 3*x+4
f1 = ax.plot(x, y1, lw = 3, alpha = 0.5, color="blue")

# f2
y2 = 1*x+1
f2 = ax.plot(x, y2, lw = 3, alpha = 0.5, color="orange")

# f3
y3 = -2*x+8
f3 = ax.plot(x, y3, lw = 3, alpha = 0.5, color="red")

# legend
ax.legend(["Gerade 1", "Gerade 2", "Gerade 3"])

#//////////////////////////////////////////////////////////////////////////////

# Set identical scales for both axes
ax.set(xlim=(xmin-1, xmax+1), ylim=(ymin-1, ymax+1), aspect='equal')

# Set bottom and left spines as x and y axes of coordinate system
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')

# Remove top and right spines
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# Create 'x' and 'y' labels placed at the end of the axes
ax.set_xlabel('x', size=14, labelpad=-24, x=1.03)
ax.set_ylabel('y', size=14, labelpad=-21, y=1.02, rotation=0)

# Create custom major ticks to determine position of tick labels
x_ticks = np.arange(xmin, xmax+1, ticks_frequency)
y_ticks = np.arange(ymin, ymax+1, ticks_frequency)
ax.set_xticks(x_ticks[x_ticks != 0])
ax.set_yticks(y_ticks[y_ticks != 0])

# Create minor ticks placed at each integer to enable drawing of minor grid
# lines: note that this has no effect in this example with ticks_frequency=1
ax.set_xticks(np.arange(xmin, xmax+1), minor=True)
ax.set_yticks(np.arange(ymin, ymax+1), minor=True)

# Draw major and minor grid lines
ax.grid(which='both', color='grey', linewidth=1, linestyle='-', alpha=0.2)

# Draw arrows
arrow_fmt = dict(markersize=4, color='black', clip_on=False)
ax.plot((1), (0), marker='>', transform=ax.get_yaxis_transform(), **arrow_fmt)
ax.plot((0), (1), marker='^', transform=ax.get_xaxis_transform(), **arrow_fmt)

plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM