首页 > 解决方案 > Matplotlib,折线图中的线条过于拥挤

问题描述

matplotlib如下生成折线图。由于它们的值,线条太近并且在某些步骤中拥挤,我怎样才能使这些线条之间的区别变得清晰: 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()

标签: pythonmatplotlib

解决方案


您可以突出显示感兴趣的区域,将其绘制在插图中,同时保持完整曲线不变。这只是要突出显示然后绘制插图的部分:

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)

输出 在此处输入图像描述


推荐阅读