matplotlib
生成折线图,如下所示。由于它们的值,这些行太紧密了,并且从某些步骤开始变得拥挤,我该如何区分这些行之间的区别:
Line Graph by Matplotlib
我的代码是:
import matplotlib.pyplot as plt
import numpy as np
####data#####
A = np.array([0.8325, 0.8847, 0.902, 0.9104, 0.917, 0.9197, 0.9248, 0.9296, 0.9338, 0.9374, 0.9393, 0.9415, 0.9441, 0.945, 0.9475, 0.95, 0.9522, 0.953, 0.9556, 0.9567,])
B = np.array([0.8702, 0.9012, 0.9118, 0.92, 0.9268, 0.9312, 0.9349, 0.9395, 0.9413, 0.9447, 0.9477, 0.9497, 0.9523, 0.9535, 0.9556, 0.9573, 0.9585, 0.9604, 0.9614, 0.9634])
C = np.array([0.8518, 0.8926, 0.9061, 0.914, 0.9201, 0.9241, 0.9271, 0.9321, 0.9347, 0.938, 0.9411, 0.9424, 0.9447, 0.9469, 0.9481, 0.9511, 0.9524, 0.9542, 0.9551, 0.9571,])
D = np.array([0.7114, 0.8093, 0.8616, 0.8877, 0.9024, 0.9114, 0.9156, 0.9194, 0.9258, 0.9298, 0.9341, 0.937, 0.9405, 0.9429, 0.9446, 0.9472, 0.9497, 0.9513, 0.9523, 0.9546])
#### plot#######
step = np.arange(1,21)
plt.plot(step, A, color='r', label='A')
plt.plot(step, B, linestyle='-', color='m', label='B')
plt.plot(step, C, linestyle='--', color='c', label='C')
plt.plot(step, D, linestyle='-.', color = 'k', label='D')
plt.xlabel('steps')
plt.ylabel('Accuracy')
#plt.title('')
plt.legend()
答案 0 :(得分:1)
您可以突出显示感兴趣的区域,以插图形式绘制它,同时保持完整的曲线不变。这只是要突出显示然后绘制插图的部分:
patch = patches.Rectangle((10,0.92),10.5,0.05,linewidth=1,linestyle='--',edgecolor='gray',facecolor='none')
ax.add_patch(patch)
left, bottom, width, height = [0.3, 0.2, 0.57, 0.3]
ax2 = fig.add_axes([left, bottom, width, height])
ax2.plot(step[8:], A[8:], '-r')
ax2.plot(step[8:], B[8:], '-m')
ax2.plot(step[8:], C[8:], '--c')
ax2.plot(step[8:], D[8:], '-.k')
ax.plot([4.9, 10], [0.84, 0.92], '--', c='gray', lw=1)
ax.plot([21, 20.5], [0.84, 0.92], '--', c='gray', lw=1)
答案 1 :(得分:-1)
当您必须显示彼此靠近的图形时,一个常见的技巧是使它们相对于彼此垂直移动。但是,当然,要注意它们之间的细微差别将更加困难。
plt.plot(step, A, color='r', label='A')
plt.plot(step, B+.05, linestyle='-', color='m', label='B+.05')
plt.plot(step, C+.10, linestyle='--', color='c', label='C+.10')
plt.plot(step, D+.15, linestyle='-.', color = 'k', label='D+.15')