首页 > 解决方案 > Matplotlib - 条形图开始不以 0 开头

问题描述

我有一个数据,我希望以条形图的形式呈现它。

数据:

col1 = ['2018 01 01', '2018 01 02', '2018 12 27'] #dates
col2 = ['4554', '14120', '1422'] #usage of the user in seconds for that data in col1

我的代码:

我已经导入了所有模块

import openpyxl as ol
import numpy as np
import matplotlib.pyplot as plt

plt.bar(col1, col2, label="Usage of the user")
plt.xlabel("Date")
plt.ylabel("Usage in seconds")
plt.title('Usage report of ' + str(args.user))
plt.legend()
plt.savefig("data.png")

当我打开 data.png 我得到这个:

点击这里查看图片

该图看起来到处都是,我希望它从零开始。

我是 matplotlib 和 openpyxl 的新手。

任何帮助表示赞赏。

标签: python-3.xmatplotlib

解决方案


似乎问题在于col2在 y 轴上绘制的值是字符串而不是整数。将这些值更新为整数将允许 y 轴开始0并按顺序排列。

col1 = ['2018 01 01', '2018 01 02', '2018 12 27'] #dates
col2 = ['4554', '14120', '1422']

plt.bar(col1, [int(x) for x in col2], label="Usage of the user")
plt.xlabel("Date")
plt.ylabel("Usage in seconds")
plt.title('Usage report')
plt.legend()

在此处输入图像描述


推荐阅读