python - 使用 Matplotlib 在 Python 中绘制数据框 - 水平条形图
问题描述
这是我在论坛上的第一个问题。
我正在使用 Pandas 读取 .dta 数据库。在这个数据库中,我有不同年份进行的一项调查的答案。在这种情况下,2011-2012-2013-2014。
我想要做的是一个带有两个变量交叉的水平条形图,但是每年的结果都在同一个图表中看到,而不是为进行调查的每一年制作图表。
这可能与 Matplotlib 有关吗?
提前非常感谢大家。
解决方案
您可以使用此代码水平绘制图表
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
plt.rcdefaults()
fig, ax = plt.subplots()
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))
ax.barh(y_pos, performance, xerr=error, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?')
plt.show()
Copy to clipboard
推荐阅读
- html - CSS悬停/边框问题
- python - 从 Anaconda 启动 Jupyter Notebook 时出现问题
- openshift - 语法错误:预期
, 但找到 '-' (语法) - maven - 站点目标的 Javadoc 插件模块错误
- google-apps-script - Google Apps 脚本嵌入 iFrame 时不加载
- javascript - 在 Angular 的交互式 Graph 中渲染 2200 万个数据点
- javascript - 在猫鼬中通过 updateOne 预挂钩更新影子字段?
- c# - System.AggregateException:“无法构建某些服务”在我的 ASP.net 核心中
- redux - Redux-thunk - 如何将获取的数据结构化为索引存储模式?
- php - 共享托管服务器通过原始 PHP api 在短时间内返回以前的数据