I am trying to plot a smooth line graph in matplotlib with a discrete x-axis that are strings (please see output figure below). I am currently getting jagged lines. All the examples, I have seen on Stack Overflow deal with a continuous x-axis (example), and rely on leveraging the x-axis for linspace()
or spline
from scipy.interpolate
. Here is a excerpt of the plotting code that gives the jagged lines:
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')
I am expecting the output to look something like this (on the right) post-smoothing but my x-axis is a string:
Any help with this would be appreciated !