Python 标题、记号、轴标签,matplotlib中未显示任何内容
这是我的代码:Python 标题、记号、轴标签,matplotlib中未显示任何内容,python,python-3.x,matplotlib,Python,Python 3.x,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
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])
查看参考书目(),您将看到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)
结果应该是一样的
以下是我的结果:
您让轴在“添加轴”中填充图形。建议你让布局更加自动化,调用ax=fig.subplot(1,1,1)而不是答案可行,但你知道奇怪的是,当我用轴[0,0,1,1]在jupyter笔记本上运行相同的函数时,它正确地绘制,没有任何遗漏的细节,这就是我的全部困惑点。对于matplotlib,jupyter与普通python控制台呈现的方式有什么不同吗?