[英]How do I get axes to show in when plotting via pyplot?
我正在尝试使用 matplotlib.pyplot 可视化一些数据,但没有显示轴。 我正在寻找与此类似的结果,轴上有轴和索引:
我得到的结果是:
这是我正在编写的代码:
import os import matplotlib.pyplot as plt import numpy as np import glob import json NAME = os.path.basename(__file__) print(f"Program \\"{NAME}\\" started.") PATTERN = "Deliverables\\\\*_maint.txt" METRICS = "metrics.json" with open(METRICS, "r") as filey: DATA = json.loads(filey.read()) report = glob.glob(PATTERN)[0] with open(report, "r") as filey: report_content = filey.read() for item in DATA: hits = report_content.count(item) DATA[item]["in final report"] = hits DATA.update() for item in DATA: docs = [] hits = [] for doc in DATA[item]: docs.append(doc) hits.append(DATA[item][doc]) fig = plt.figure() x = np.arange(len(docs)) ax = fig.add_axes([0, 0, 1, 1]) ax.set_title("Number of search results by doctype") ax.set_xticks(x) ax.set_yticks(np.arange(0, 50, 10)) ax.bar(docs, hits) plt.show() input() print(f"Program \\"{NAME}\\" complete.")
编辑:忘记示例数据:我已将其包含在下面:
{"j": {"a": 0, "d": 0, "c": 4, "e": 20, "f": 10, "g": 0}, "h:": {"a": 0, "d": 0, "c": 0, "e": 2, "f": 0, "g": 0}, "i": {"a": 13, "d": 0, "c": 0, "e": 0, "f": 0, "g": 0}, "k": {"a": 6, "d": 0, "c": 0, "e": 0, "f": 0, "g": 0}, "n": {"a": 0, "d": 9, "c": 1, "e": 5, "f": 3, "g": 0}, "o": {"a": 0, "d": 0, "c": 0, "e": 0, "f": 0, "g": 0}, "p": {"a": 0, "d": 24, "c": 0, "e": 0, "f": 0, "g": 0}, "q": {"a": 0, "d": 0, "c": 0, "e": 0, "f": 0, "g": 0}, "r": {"a": 0, "d": 0, "c": 0, "e": 0, "f": 0, "g": 0}, "s": {"a": 0, "d": 0, "c": 0, "e": 1, "f": 1, "g": 0}, "t": {"a": 0, "d": 0, "c": 0, "e": 10, "f": 9, "g": 0}, "m": {"a": 2, "d": 0, "c": 0, "e": 13, "f": 19, "g": 0}, "l": {"a": 0, "d": 0, "c": 0, "e": 1, "f": 2, "g": 0}, "b": {"a": 0, "d": 0, "c": 0, "e": 15, "f": 0, "g": 0}}
交换
ax = fig.add_axes([0, 0, 1, 1])
对于以下情况:
ax = fig.add_subplot(111)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.