簡體   English   中英

標題、刻度、軸標簽,matplotlib 中沒有顯示任何內容

[英]Title, tick, axis labels, nothing is showing in matplotlib

這是我的代碼:

import numpy as np
import matplotlib.pyplot as plt

def plot_graph():
  fig = plt.figure()
  data = [[top3_empsearch, top5_empsearch, top7_empsearch], [top3_elastic, top5_elastic, top7_elastic]]
  X = np.arange(3)
  ax = fig.add_axes([0, 0, 1, 1])
  ax.bar(X + 0.00, data[0], color='b', width=0.25)
  ax.bar(X + 0.25, data[1], color='g', width=0.25)
  ax.set_ylabel('Accuracy (in %)')
  plt.title('Percentage accuracy for selected result in Top-3, Top-5, Top-7 in employee search vs elastic search')
  plt.yticks(np.arange(0, 101, 10))
  colors = {'empsearch':'blue', 'elastic':'green'}
  labels = list(colors.keys())
  handles = [plt.Rectangle((0,0),1,1, color=colors[label]) for label in labels]

  plt.legend(handles, labels)
  plt.style.use('dark_background')
  plt.show()

plot_graph()

這段代碼的結果是 -> 在此處輸入圖像描述

沒有刻度,沒有標簽,沒有標題,什么都看不見,我被迷惑了。 將感謝您的幫助。

唯一的問題在於這一行:

ax = fig.add_axes([0, 0, 1, 1])

Looking to the bibliography ( https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.figure.Figure.html ), you will see that the first parameter of add_axes() function is "rect", which refers to新軸的尺寸 [left, bottom, width, height],所有數量均以圖形寬度和高度的分數表示。 因此,在您的代碼中,您准確地給出了圖形的尺寸,所以標題、刻度、標簽......在那里但被隱藏了。 所以你必須留出一些空間,減少一點情節的尺寸。 你可以通過修改來做到這一點:

ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])

或者,您可以將該行替換為:

ax = fig.add_subplot(1,1,1) 

結果應該是一樣的。

這是我的結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM