利用Python绘制酷炫OD流量图的新方法

在百度迁徙的文章里,我们画的OD地图是这样的:

这个看着其实也还行,不过仔细观察也会发现几个小问题:
1. OD线的粗细代表了流量的大小,但没有加图例,难以有客观的认识;
2. 由于OD线是直线,双向的OD会重叠在一起,比如上海到北京和北京到上海的OD线是重合的,在图面上无法区分。
为了解决这些问题,应对方法一般是:
1. 对流量大小利用颜色或者粗细进行区分,并且为了图面整体的协调,最好先对流量进行分组(自然断点法)。
2. 将OD的直线改为曲线,这样双向OD即可区分开来,并且更加美观。
先展示一下利用以上思路绘制的OD图:

这里用到的数据是按照一定规则随机生成的C市出租车的OD数据,以街道为交通小区进行OD的集计处理。下面讲一下主要的思路:
这里主要用到了matplotlib的ax.annotate命令,利用这个命令,我们可以画出各种各样的弧线:
这里抄一段官方tutorial的代码。
import matplotlib.pyplot as plt
def demo_con_style(ax, connectionstyle):
x1, y1 = 0.3, 0.2
x2, y2 = 0.8, 0.6
ax.plot([x1, x2], [y1, y2], ".")
ax.annotate("",
xy=(x1, y1), xycoords='data',
xytext=(x2, y2), textcoords='data',
arrowprops=dict(arrowstyle="->", color="0.5",
shrinkA=5, shrinkB=5,
patchA=None, patchB=None,
connectionstyle=connectionstyle,
ax.text(.05, .95, connectionstyle.replace(",", ",\n"),
transform=ax.transAxes, ha="left", va="top")
fig, axs = plt.subplots(3, 5, figsize=(8, 4.8))
demo_con_style(axs[0, 0], "angle3,angleA=90,angleB=0")
demo_con_style(axs[1, 0], "angle3,angleA=0,angleB=90")
demo_con_style(axs[0, 1], "arc3,rad=0.")
demo_con_style(axs[1, 1], "arc3,rad=0.3")
demo_con_style(axs[2, 1], "arc3,rad=-0.3")
demo_con_style(axs[0, 2], "angle,angleA=-90,angleB=180,rad=0")
demo_con_style(axs[1, 2], "angle,angleA=-90,angleB=180,rad=5")
demo_con_style(axs[2, 2], "angle,angleA=-90,angleB=10,rad=5")
demo_con_style(axs[0, 3], "arc,angleA=-90,angleB=0,armA=30,armB=30,rad=0")
demo_con_style(axs[1, 3], "arc,angleA=-90,angleB=0,armA=30,armB=30,rad=5")
demo_con_style(axs[2, 3], "arc,angleA=-90,angleB=0,armA=0,armB=40,rad=0")
demo_con_style(axs[0, 4], "bar,fraction=0.3")
demo_con_style(axs[1, 4], "bar,fraction=-0.3")
demo_con_style(axs[2, 4], "bar,angle=180,fraction=-0.2")