python-3.x - 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 的新手。
任何帮助表示赞赏。
解决方案
似乎问题在于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()
推荐阅读
- javascript - jQuery 路线图 - 无法使用 PHP 确定事件
- java - 会话保存后的HQL,主键每次显示0
- python - 如何从变量中创建路径
- pandas - 在 Pandas 中创建数据框 - 读取 Parquet 文件时出现内存不足错误
- javascript - 标头未显示在本机反应中
- javascript - 谷歌开发工具设备工具栏上的 html 页面不是全宽
- xml - 如何使背景图像缓慢向上移动
- c++ - 在 C++ 中重载字符数组的赋值运算符
- azure - Azure 数据工厂附加大量与 csv 文件具有不同架构的文件
- javascript - var 单个绑定 v/s 让多个绑定