我目前正在使用Atom,当我运行我的代码时,输出图形按顺序显示,这样我只能在关闭第一个图形后看到下一个图形。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
housing = pd.read_csv("C:\\Users\\<username>\\handson-ml\\datasets\\housing\\housing.csv")
housing.hist(bins=50, figsize=(20,15))
plt.show()
housing["income_cat"] = np.ceil(housing["median_income"]/1.5)
housing["income_cat"].where(housing["income_cat"]<5, 5.0, inplace=True)
plt.hist(housing["income_cat"])
plt.show()
如何更正此问题以便同时查看所有图表?习惯于Jupyter我在其他平台上执行数据可视化时遇到了麻烦。
答案 0 :(得分:2)
定义绘图轴并在进行第一个hist时指定它。然后制作双轴并用于绘制第二个。参数alpha
允许您区分嘶嘶声。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
housing = pd.read_csv("C:\\Users\\<username>\\handson-ml\\datasets\\housing\\housing.csv")
fig, ax = plt.subplots()
housing.hist(bins=50, figsize=(20,15), ax=ax, alpha=.2)
housing["income_cat"] = np.ceil(housing[1]/1.5)
housing["income_cat"].where(housing["income_cat"]<5, 5.0, inplace=True)
ax2 = ax.twinx()
housing['income_cat'].hist(ax=ax2, color='r', alpha=.2)
plt.show()
最后使用plt.show()
来显示一次数字。