python canvas获取的图片流数据为空
生活随笔
收集整理的這篇文章主要介紹了
python canvas获取的图片流数据为空
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
def StartDraw(self):canvas = plt.figure().canvasplt.rcParams['font.sans-serif'] = ['SimHei'] # 用來正常顯示中文標簽plt.rcParams['axes.unicode_minus'] = False # 用來正常顯示負號win = np.array(self.win)drawn = np.array(self.drawn)lost = np.array(self.lost)crwin = np.array(self.crwin)crdrawn = np.array(self.crdrawn)crlost = np.array(self.crlost)changeTime = np.array(self.changeTime)crChangeTime = np.array(self.crChangeTime)# 獲取開始時間和終止時間startDay = min(crChangeTime[0], changeTime[0])endDay = max(crChangeTime[-1], changeTime[-1])# 設置外邊框的樣式及長度plt.figure(facecolor='gray', edgecolor='white', figsize=(24,12))ax = plt.gca()# 設置內邊框樣式ax.patch.set_facecolor("black") # 設置ax1區域背景顏色ax.patch.set_alpha(10)# 設置ax1區域背景顏色透明度# 去掉脊柱(坐標系)ax.xaxis.set_major_formatter(mdate.DateFormatter('%y-%m-%d %H:%M',)) # 橫坐標標簽顯示的日期格式plt.xticks(pd.date_range(startDay, endDay, freq=self.freq), rotation=45) # 橫坐標范圍ax.spines['top'].set_visible(False) # 去掉上邊框ax.spines['right'].set_visible(False) # 去掉右邊框ax.spines['left'].set_visible(False) # 去掉上邊框plt.ylabel('odds change', color='black', size='15')plt.xlabel('with the time', color='black', size='15')# 標題plt.title(self.VS)# 設置圖形注釋,參數依次為注釋文字,箭頭位置,箭尾位置。在箭頭參數中參數為箭頭顏色plt.annotate('win', xy=(-0.05, win[0] - 0.15), color='#02F8FB')plt.annotate('drawn', xy=(-0.05, drawn[0] - 0.15), color='#02F8FB')plt.annotate('lost', xy=(-0.05, lost[0] - 0.15), color='#02F8FB')plt.plot(changeTime, win, linestyle='dashdot', marker='^', linewidth=0.5, color='#29FF16', label='win')plt.plot(changeTime, drawn, linestyle='dashdot', marker='^', linewidth=0.5, color='#7CFFF2', label='drawn')plt.plot(changeTime, lost, linestyle='dashdot', marker='^', linewidth=0.5, color='#FF0913', label='lost')plt.annotate('win', xy=(-0.05, win[0] - 0.15), color='#02F8FB')plt.annotate('drawn', xy=(-0.05, drawn[0] - 0.15), color='#02F8FB')plt.annotate('lost', xy=(-0.05, lost[0] - 0.15), color='#02F8FB')plt.plot(crChangeTime, crwin, linestyle='-', linewidth=1, color='#29FF16', label='crwin')plt.plot(crChangeTime, crdrawn, linestyle='-', linewidth=1, color='#7CFFF2', label='crdrawn')plt.plot(crChangeTime, crlost, linestyle='-', linewidth=1, color='#FF0913', label='crlost')# 開啟網格plt.grid(True)plt.grid(color='white', linestyle='--', linewidth=1, alpha=0.2)# 繪制圖注位置plt.legend(bbox_to_anchor=(0.005, 1), loc=3, borderaxespad=0)fileName = self.crChangeTime[-1].strftime('%Y-%m-%d') + ' ' + self.VS+'.png'##一定要注意這一句,不然canvas獲取不到圖片流canvas = plt.get_current_fig_manager().canvas# 獲取流數據方便上傳到Minio# 獲取輸入輸出流對象buffer = io.BytesIO()# 將畫布上的內容打印到輸入輸出流對象canvas.print_png(buffer)# 獲取流的值IODataForMinio = buffer.getvalue()# 正式上傳Miniobuffer.write(IODataForMinio)ioLength = io.BytesIO(IODataForMinio).getbuffer().nbytesfileName = self.crChangeTime[-1].strftime('%Y-%m-%d') + ' ' + self.VS + '.png'UploadMinio(io.BytesIO(IODataForMinio),ioLength,fileName)buffer.close()
canvas = plt.get_current_fig_manager().canvas
參考文章
總結
以上是生活随笔為你收集整理的python canvas获取的图片流数据为空的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如果HR这么跟你说,我建议你再考虑一下,
- 下一篇: 互联网晚报 | 12月28日 星期二 |