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])
您可以使用Seaborn生成热图。首先对数据框进行装箱/整形。这是随机数据,所以热图不是很有趣
以下示例使用seaborn的tips数据集。直方图是通过将
total_bill
分组到存储箱中创建的。然后,根据每组中的提示对条进行着色下面是带状彩色贴图(
cmap = plt.get_cmap('Spectral', 9)
)的外观:下面是另一个使用
'mpg'
数据集的示例,该数据集具有汽车重量的直方图和每加仑英里数的颜色相关问题 更多 >
编程相关推荐