ax.plot(loc = 'upper right')
时间: 2023-06-25 10:07:10 浏览: 190
这是 Matplotlib 中绘制图形时设置图例位置的代码。`ax` 是 Axes 对象,`plot` 是在该对象上绘制折线图的方法。`loc` 参数用于指定图例的位置,`'upper right'` 表示将图例放置在 Axes 对象的右上角。其他可选值包括 `'upper left'`、`'lower left'`、`'lower right'` 等。
相关问题
fig, ax = plt.subplots() ax.plot(new_name_lst, x_list, label='velocity_x') ax.plot(new_name_lst, y_list, label='velocity_y') ax.plot(new_name_lst, list3, label='velocity') # leg = ax.legend(loc='upper left', fancybox=True, shadow=True) leg.get_frame().set_alpha(0.5) lines = [l for l in ax.lines] lined = dict() for legline, origline in zip(leg.get_lines(), lines): legline.set_picker(True) lined[legline] = origline def onpick(event): legline = event.artist origline = lined[legline] visible = not origline.get_visible() origline.set_visible(visible) if visible: legline.set_alpha(1.0) else: legline.set_alpha(0.2) fig.canvas.draw() fig.canvas.mpl_connect('pick_event', onpick) plt.show()这个代码加一个跟随鼠标的虚线
你可以尝试添加以下代码来实现跟随鼠标的虚线:
```python
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
# 假设你已经有了名为 new_name_lst、x_list、y_list 和 list3 的变量
ax.plot(new_name_lst, x_list, label='velocity_x')
ax.plot(new_name_lst, y_list, label='velocity_y')
ax.plot(new_name_lst, list3, label='velocity')
leg = ax.legend(loc='upper left', fancybox=True, shadow=True)
leg.get_frame().set_alpha(0.5)
lines = [l for l in ax.lines]
lined = dict()
for legline, origline in zip(leg.get_lines(), lines):
legline.set_picker(True)
lined[legline] = origline
def onpick(event):
legline = event.artist
origline = lined[legline]
visible = not origline.get_visible()
origline.set_visible(visible)
if visible:
legline.set_alpha(1.0)
else:
legline.set_alpha(0.2)
fig.canvas.draw()
# 添加跟随鼠标的虚线
if isinstance(event.artist, plt.Line2D):
xdata = event.artist.get_xdata()
ydata = event.artist.get_ydata()
x = event.mouseevent.xdata
y = event.mouseevent.ydata
idx = np.searchsorted(xdata, x)
if idx < len(xdata) and abs(x - xdata[idx]) > abs(x - xdata[idx-1]):
idx -= 1
if idx >= len(xdata) or idx == 0:
return
x = xdata[idx]
y = ydata[idx]
if not hasattr(ax, 'vline'):
ax.vline = ax.axvline(x=x, color='k', linestyle='--')
else:
ax.vline.set_xdata([x, x])
ax.vline.set_ydata([min(y, ax.get_ylim()[1]), max(y, ax.get_ylim()[0])])
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
```
这个代码会在图形中添加一个跟随鼠标的垂直虚线,当鼠标拾取到某个线条时,虚线会跟随鼠标移动到该线条上。
优化这段代码,将两个图形合并成一个 import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 10, 1000) lines = [np.sin(x), np.cos(x), -np.cos(x), -np.sin(x)] line_styles = ['-', '-.', '--', ':'] line_colors = ['red', 'blue', 'green', 'black'] # 创建第一个坐标轴,画 part A 和 part B fig, ax1 = plt.subplots() ax1.plot(x, lines[0], linestyle=line_styles[0], color=line_colors[0], label=f'part A') ax1.plot(x, lines[1], linestyle=line_styles[1], color=line_colors[1], label=f'part B') ax1.legend(loc='upper right') # 创建第二个坐标轴,画 part C 和 part D fig, ax2 = plt.subplots() ax2.plot(x, lines[2], linestyle=line_styles[2], color=line_colors[2], label=f'part C') ax2.plot(x, lines[3], linestyle=line_styles[3], color=line_colors[3], label=f'part D') ax2.legend(loc='lower right') plt.show()
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 1000)
lines = [np.sin(x), np.cos(x), -np.cos(x), -np.sin(x)]
line_styles = ['-', '-.', '--', ':']
line_colors = ['red', 'blue', 'green', 'black']
# 创建一个坐标轴,画出所有的图形
fig, ax = plt.subplots()
ax.plot(x, lines[0], linestyle=line_styles[0], color=line_colors[0], label=f'part A')
ax.plot(x, lines[1], linestyle=line_styles[1], color=line_colors[1], label=f'part B')
ax.plot(x, lines[2], linestyle=line_styles[2], color=line_colors[2], label=f'part C')
ax.plot(x, lines[3], linestyle=line_styles[3], color=line_colors[3], label=f'part D')
ax.legend(loc='upper right')
plt.show()
阅读全文
相关推荐
















