我正在尝试在 matplotlib 中绘制一个带有离散 x 轴的平滑线图,这些 x 轴是字符串(请参见下面的输出图)。我目前得到锯齿状的线条。我在 Stack Overflow 上看到的所有示例都处理连续的 x 轴(example),并且依赖于利用 x 轴 forlinspace()
或spline
from scipy.interpolate
。这是给出锯齿线的绘图代码的摘录:
xstrings = ['Q2W1','Q2W2','Q2W3','Q2W4','Q2W5','Q2W6','Q2W7','Q2W8','Q2W9','Q2W10','Q2QW11','Q2W12','Q2W13']
y = [88,84,83,99,96,85,85,82,65,60,19,45,27]
plt.plot(xstrings, y, linestyle='-', marker='.', color='#009d9a', linewidth=1)
#plt.legend(['data', 'linear', 'cubic'], loc='best')
for a,b in zip(xstrings,y):
xs.append(a)
ys.append(b)
if b < 7:
label = "${:,.2f}".format(b)
elif b < 10:
label = "${:,.1f}".format(b)
else:
label = "${:,.0f}".format(b)
plt.annotate(label, # this is the text
(a,b), # this is the point to label
textcoords="offset points", # how to position the text
xytext=(0,3), # distance from text to points (x,y)
ha='center', fontsize = 6)
plt.xticks(fontsize=6.5, rotation=45)
plt.show()
plt.savefig("Graphs/"+'test.png', dpi=300, bbox_inches='tight')
我希望输出在平滑后看起来像这样(在右侧),但我的 x 轴是一个字符串:
对此的任何帮助将不胜感激!