I'm rendering a chart with matplotlib through panda's df.plot()
method, but somehow the tick labels on the X-Axis seem to be messed up. There seems to be a layer of numeric text above my "real" tick labels. I have no idea where this comes from. Strangely enough, this only happens in our production and staging environment, not in dev. Maybe it also has to do with gunicorn or django's dev server. Has somebody made this experience before?
Here's the plot result:
and this is the output when I clear the ticks with .set_xticks([], [])
:
Here's a portion of my code, there's some abstraction but I think it should be understandable. self.data
is a pandas dataframe. It outputs base64-encoded png data:
class Line(ChartElement):
def draw(self):
self._plot = self.data.plot(kind='line', color=self.colors)
self._rotation = 'vertical'
self._horizontalalignment='center'
# self._plot.set_xticks([], [])
# ...
class ChartElement():
# ...
def base64(self):
self.draw()
figfile = BytesIO()
if not self._plot:
raise ValueError("Plot not generated.")
if self._legend:
self._plot.legend(self._legend, **self._legend_args)
#plt.legend(self._legend, loc='upper center', bbox_to_anchor=(0.4, .05), ncol=5, fancybox=False, prop={'size': 6})
plt.setp(self._plot.get_xticklabels(), rotation=self._rotation, horizontalalignment=self._horizontalalignment, fontsize=8)
self._plot.set_ylabel(self.y_title)
self._plot.set_xlabel(self.x_title)
#plt.figure()
fig = self._plot.get_figure()
fig.autofmt_xdate()
if self.size and isinstance(self.size, list):
fig.set_size_inches(inch(self.size[0]), inch(self.size[1]))
fig.savefig(figfile, format='png', bbox_inches='tight')
figfile.seek(0)
figdata_png = base64.b64encode(figfile.getvalue())
plt.clf()
plt.cla()
plt.close('all')
plt.close()
return figdata_png.decode('utf8')
CodePudding user response:
I managed to solve it, and it was my fault :). I somhow missed out on major and minor ticks.
I fixed it by turning the minor tick labels off right after the self.draw()
method:
self._plot.tick_params(axis='x', which='minor', bottom=True, top=False, labelbottom=False)
This doesn't explain why they don't show up in the dev environment but I'm good for now. Maybe different default settings !?