0

I am currently using Atom and when i run my code the output graphs are being shown in a sequential order such that I can only see the next graph after closing the first graph.

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()

How to correct this so as to see all the graphs simultaneously? Being used to Jupyter I am having trouble performing data visualization on other platforms.

Ashwin
  • 1
  • 3
  • Voted to reopen; this question asks how to display all figures at once, the flagged duplicate is on how to create `subplots` in one figure, which is something completely different. – Bart Jun 01 '18 at 09:40
  • This might be what you are actually after: https://stackoverflow.com/questions/7744697/how-to-show-two-figures-using-matplotlib – Bart Jun 01 '18 at 09:45

1 Answers1

2

Define plot axis and specify it when you make the first hist. Then make twin axis and use to plot the second one. Parameter alpha allows you to distinguish hists.

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()

Use plt.show() at the end to show the figure once.

Ilya
  • 183
  • 5