【发布时间】:2021-10-22 13:44:47
【问题描述】:
我有一个直方图:
# Lets load a dataset of house prices in Boston.
from sklearn.datasets import load_diabetes
#sklearn gives you the data as a dictionary, so
diabetes = load_diabetes(as_frame=True)
data = diabetes['frame']
import matplotlib.pyplot as plt
%matplotlib inline
bmi_hist = plt.hist(data['bmi'], density=False)
bmi_hist = plt.ylabel("Frequency")
bmi_hist = plt.xlabel("Normalized BMI")
bp_hist = plt.hist(data['bp'], density=False)
bp_hist = plt.ylabel("Frequency")
bp_hist = plt.xlabel("Normalized BP")
这是上图中两列的直方图。 我想在散点图中比较这两者。我的尝试并不是很成功,因为我知道我需要一个 X 和一个 Y 来绘制。 我以为我会使用与直方图相同的轴:
y_bmi = data['bmi'].value_counts() # frequency
x_bmi = data['bmi'] # normalized value
ax1 = df.plot.scatter(x = x_bmi, y= y_bmi, c='DarkBlue')
但这只能在“数据帧”上使用,所以我必须将 bmi 列的值重复到新的数据帧中吗?还是有更简单的方法?
我们将不胜感激。 非常感谢。
【问题讨论】:
-
data[['bmi', 'bp']].plot(kind='hist', ec='k', alpha=0.5, figsize=(5, 4))是一个更容易实现直方图的方法。 -
类似
import seaborn as sns和sns.lmplot(data=data, x='bmi', y='bp', hue='sex')是比较'bmi'和'bp'之间关系的更好方法。
标签: python pandas matplotlib histogram scatter