我正在通過 panda 的df.plot()方法使用 matplotlib 渲染圖表,但不知何故,X 軸上的刻度標簽似乎搞砸了。我的“真實”刻度標簽上方似乎有一層數字文本。我不知道這是從哪里來的。奇怪的是,這只發生在我們的生產和登臺環境中,而不是在開發環境中。也許它也與 gunicorn 或 django 的開發服務器有關。以前有人有過這種經歷嗎?
這是繪圖結果:

這是我清除刻度時的輸出.set_xticks([], []):

這是我的代碼的一部分,有一些抽象,但我認為它應該是可以理解的。self.data是一個熊貓資料框。它輸出 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')
uj5u.com熱心網友回復:
我設法解決了它,這是我的錯:)。我不知怎的錯過了主要和次要的蜱蟲。我通過在方法之后立即關閉次要刻度標簽來修復它self.draw():
self._plot.tick_params(axis='x', which='minor', bottom=True, top=False, labelbottom=False)
這并不能解釋為什么它們沒有出現在開發環境中,但我現在很好。也許不同的默認設定!?
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/430606.html
標籤:Python django 熊猫 matplotlib
下一篇:滑塊上的日期刻度(mm/dd)
