繁体   English   中英

pyplot 除了基本图形外什么都不显示

[英]pyplot showing nothing except basic graph

pyplot 不听任何我的样式命令,也不显示任何轴刻度。

我从https://www.tutorialspoint.com/matplotlib/matplotlib_bar_plot.htmand复制了这个例子,但无法像他们那样工作,也无法在我添加数据时使用

team_data = {
    "England": {
        "avg_game_difference": 5.0476190476190474,
        "articles_count": 51
    },
    "France": {
        "avg_game_difference": 3.857142857142857,
        "articles_count": 12
    },
    "Ireland": {
        "avg_game_difference": 5.2,
        "articles_count": 22
    },
    "Italy": {
        "avg_game_difference": 7.0,
        "articles_count": 5
    },
    "Scotland": {
        "avg_game_difference": 2.5,
        "articles_count": 9
    },
    "Wales": {
        "avg_game_difference": 3.5833333333333335,
        "articles_count": 24
    },
    "New Zealand": {
        "avg_game_difference": 1.25,
        "articles_count": 9
    }
}

fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
categories = list(team_data.keys())
data = [i['articles_count'] for i in team_data.values()]
ax.bar(categories, data)
ax.set_title('Teams with most articles written')
ax.set_xlabel('Teams')
ax.set_ylabel('Number articles written')
plt.savefig('output.png')

见 output.png

在此处输入图像描述

试试这个:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
categories = list(team_data.keys())
data = [i['articles_count'] for i in team_data.values()]
ax.bar(categories, data)
ax.set_title('Teams with most articles written')
ax.set_xlabel('Teams')
ax.set_ylabel('Number articles written')
plt.savefig('output.png')

这个答案详细解释了add_axes和 subplot 之间的区别,但这里重要的是add_axes([0, 0, 1, 1])用你的 plot 填充整个 canvas 不留标签或标题空间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM