python-3.x - Matplotlib 修复 y 轴
问题描述
我正在尝试使用 matplotlib 创建水平条形图。我的数据点是以下两个数组
distance = [100, 200, 300, 400, 500, 3000]
value = [10, 15, 50, 74, 95, 98]
我生成水平条形图的代码如下
plt.barh(distance, value, height=75)
plt.savefig(fig_name, dpi=300)
plt.close()
问题是我的图像是这样的
有没有办法确保所有块的宽度相同并跳过 500 到 300 之间的空格
解决方案
您可以这样做确保 Matplotlib 将您的标签视为标签,而不是数字。您可以通过将它们转换为字符串来做到这一点:
import matplotlib.pyplot as plt
distance = [100, 200, 300, 400, 500, 3000]
value = [10, 15, 50, 74, 95, 98]
distance = [str(number) for number in distance]
plt.barh(distance, value, height=0.75)
请注意,您必须更改高度。
推荐阅读
- excel - 收到错误运行时错误“-2147417848 (80010108)”:对象“范围”的方法“值”失败
- php - 在 Mailster、Wordpress 站点中与 Qform 集成
- reactjs - 如何运行测试并恢复之前的组件状态
- javascript - 如何覆盖 Datatables 上的数据?
- android - 在 RecyclerView.Adapter 中更改 textView 颜色
- c# - Task.Run(async) 递归的性能影响
- java - Weblogic - 为什么 Java ClassLoader 不从空清单 jar 中提到的 jar 加载类
- c# - C# ASP.NET Core Entity Framework Core 异步 ToQueryable 比较
- ios - AlamoFire5 空响应可解码
- hololens - HoloLens 2 无法使用 Microsoft HoloLens Windows 10 桌面应用程序连接