python - 如何创建一个直方图,其中每个条形图都是相应 bin 的热图?
问题描述
我有一个数据框:
df.head()[['price', 'volumen']]
price volumen
48 45 3
49 48 1
50 100 2
51 45 1
52 56 1
它表示具有特定价格的对象的数量。我根据volume
列创建了一个直方图:
我想添加有关每个箱的价格分布的信息。我的想法是使用热图而不是单色列。例如,红色代表高价,黄色代表低价。
这是一个示例图来说明总体思路:
解决方案
您可以使用 Seaborn 生成热图。bin / 首先塑造数据框。这是随机数据,所以热图不是那么有趣。
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
s = 50
df = pd.DataFrame({"price":np.random.randint(30,120, s),"volume":np.random.randint(1,5, s)})
fig, ax = plt.subplots(2, figsize=[10,6])
df.loc[:,"volume"].plot(ax=ax[0], kind="hist", bins=3)
# reshape for a heatmap... put price into bins and make 2D
dfh = df.assign(pbin=pd.qcut(df.price,5)).groupby(["pbin","volume"]).mean().unstack(1).droplevel(0,axis=1)
axh = sns.heatmap(dfh, ax=ax[1])
推荐阅读
- soapui - 努力在soap ui中的测试用例级别引用属性值
- django - 显示选项的描述性状态
- angular - 我如何根据食物描述调整我的食物卡如果它是空的或空白的?
- ios - “Apple 开发者计划许可协议”已更新
- css - css nth-child() 检查板模式
- java - libgdx - setProjectionMatrix
- javascript - Firebase 似乎试图在身份验证序列运行之前开始侦听快照更新。有任何解决这个问题的方法吗?
- ruby-on-rails - 在 rails 中使用omniauth-saml 设置和配置google saml
- c - 从 C 中的 stdin 泄漏内存
- c - C 信号不能正常工作