首页 > 解决方案 > 使用 seaborn/matplotlib 将直线图变成曲线图

问题描述

我想知道是否有人知道如何使用 matplotlib 或 seaborn 来弯曲原来的直线?原因是我有很多线从 A 点到 B 点。

我在这里附上了一个具体的例子。我从 A 点到 B 点有三条线。如果不弯曲其中两条,我将无法分辨出有三条线(第一个图)。只有弯曲两条线,你才能知道实际上有三条线从 A 到 B (第二个图;我手动添加了曲线)。

原来是直线图 曲线图

标签: pythonmatplotlib

解决方案


我认为“弯曲”这条线并实际制作虚假数据没有任何意义。但是给你:

import matplotlib.patches as patches
import matplotlib.pyplot as plt
plt.close()
fig, ax = plt.subplots()
x = np.arange(50)
y = np.arange(50)
ax.plot(x, y)

a_red = patches.FancyArrowPatch((x[0], y[0]), (x[-1], y[-1]), connectionstyle="arc3,rad=0.1", arrowstyle="Simple, tail_width=0.5, head_width=4, head_length=8", color='r')
a_gre = patches.FancyArrowPatch((x[0], y[0]), (x[-1], y[-1]), connectionstyle="arc3,rad=-0.1", arrowstyle="Simple, tail_width=0.5, head_width=4, head_length=8", color='g')
plt.gca().add_patch(a_red)
plt.gca().add_patch(a_gre)

输出:

在此处输入图像描述


推荐阅读