matplotlib,修复带有圆形破折号的轮廓线

时间:2016-04-23 19:34:21

标签: matplotlib

从matplotib 1.5开始,集合似乎没有属性set_dash_capstyle(' round'),我用这个hack来获得圆形虚线轮廓线。这不是一个问题,而是一个不太优雅的解决方案,我无法找到答案。

default butt dashed contour lines (top), round dashed contour lines (bottom)

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

# make data
D = np.ones((60, 60))
for i, x in enumerate(D):
    for j, y in enumerate(x):
        r = np.sqrt((0.03*float(i)-1)**2+(0.03*float(j)-1)**2)
        D[i][j] = np.sin(r)

fig, ax = plt.subplots(2,1,figsize=(4,8))

im1 = ax[0].imshow(D, vmin=np.min(D), vmax=np.max(D), aspect='auto', origin='lower')
levels = (0.3, 0.6)
cs1 = ax[0].contour(D, levels, hold='on', colors='k', origin='lower', linestyles='dashed', linewidths=5.0)
ax[0].set_title("Dashed contour lines 'butt' style")

im2 = ax[1].imshow(D, vmin=np.min(D), vmax=np.max(D), aspect='auto', origin='lower')
levels = (0.3, 0.6)
cs2 = ax[1].contour(D, levels, hold='on', colors='k', origin='lower', linestyles='dashed', linewidths=5.0)
# obtain contour lines, and delete them
cnt_line=[]
for c in cs2.collections:
    for p in c._paths:
        cnt_line.append(p.vertices)
    c.remove()
# plot contour lines again
for i, l in enumerate(cnt_line):
    ax[1].plot(l[:,0],l[:,1], color='k', ls='dashed', lw=5.0)
    ax[1].lines[i].set_dash_capstyle('round')
ax[1].set_title("Dashed contour lines 'round' style")

plt.show()

0 个答案:

没有答案