0

这是我的代码:

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()

这段代码的结果是 ->在此处输入图像描述

没有刻度,没有标签,没有标题,什么都看不见,我被迷惑了。将感谢您的帮助。

4

1 回答 1

1

唯一的问题在于这一行:

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

查看参考书目(https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.figure.Figure.html),您会看到 add_axes() 函数的第一个参数是“rect”,它指的是新轴的尺寸 [left, bottom, width, height],所有数量均以图形宽度和高度的分数表示。因此,在您的代码中,您准确地给出了图形的尺寸,所以标题、刻度、标签......在那里但被隐藏了。所以你必须留出一些空间,减少一点情节的尺寸。你可以通过修改来做到这一点:

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

或者,您可以将该行替换为:

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

结果应该是一样的。

这是我的结果:

在此处输入图像描述

于 2020-05-04T18:55:26.847 回答