渲染 X 轴刻度时的奇怪行为
Strange behaviour when rendering X-Axis ticks
我正在通过 panda 的 df.plot()
方法用 matplotlib 渲染图表,但 X 轴上的刻度标签似乎弄乱了。我的“真实”刻度标签上方似乎有一层数字文本。我不知道这是从哪里来的。奇怪的是,这只发生在我们的生产和暂存环境中,而不是在开发环境中。也许它也与 gunicorn 或 django 的开发服务器有关。有人以前有过这种经历吗?
这是绘图结果:
这是我使用 .set_xticks([], [])
:
清除刻度时的输出
这是我的部分代码,有一些抽象,但我认为它应该是可以理解的。 self.data
是一个 pandas 数据框。它输出 base64 编码的 png 数据:
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')
我设法解决了它,这是我的错:)。我不知何故错过了主要和次要的滴答声。
我通过在 self.draw()
方法之后立即关闭次要刻度标签来修复它:
self._plot.tick_params(axis='x', which='minor', bottom=True, top=False, labelbottom=False)
这并不能解释为什么它们没有出现在开发环境中,但我现在很好。也许不同的默认设置 !?
我正在通过 panda 的 df.plot()
方法用 matplotlib 渲染图表,但 X 轴上的刻度标签似乎弄乱了。我的“真实”刻度标签上方似乎有一层数字文本。我不知道这是从哪里来的。奇怪的是,这只发生在我们的生产和暂存环境中,而不是在开发环境中。也许它也与 gunicorn 或 django 的开发服务器有关。有人以前有过这种经历吗?
这是绘图结果:
这是我使用 .set_xticks([], [])
:
这是我的部分代码,有一些抽象,但我认为它应该是可以理解的。 self.data
是一个 pandas 数据框。它输出 base64 编码的 png 数据:
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')
我设法解决了它,这是我的错:)。我不知何故错过了主要和次要的滴答声。
我通过在 self.draw()
方法之后立即关闭次要刻度标签来修复它:
self._plot.tick_params(axis='x', which='minor', bottom=True, top=False, labelbottom=False)
这并不能解释为什么它们没有出现在开发环境中,但我现在很好。也许不同的默认设置 !?