Reputation: 139
I want to turn a normal plot into a subplot. Here's the code for the plot, which works:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
d = {'A': [1, 2, 3, 4, 5, 6], 'B': [-2.5, -1.00, .25, 1.56, .75, 1.20]}
df = pd.DataFrame(data=d)
x = np.arange(0, 999, 0.1)
y1 = -.75
y2 = .75
plt.fill_between(x, y1, y2, color='lawngreen', alpha='.6')
plt.scatter(df.A, df.B)
plt.plot(df.A, df.B)
plt.axhline(y=0, color='black')
plt.xticks(np.arange(0, 999))
plt.ylim([-4, 4])
plt.xlim([0, df.A.max() + 1])
plt.show()
Then here's what I tried to make it into a subplot. The console doesn't throw any errors, it's just not showing any plot.
fig = Figure()
ax = fig.add_subplot(111)
x = np.arange(0, 999, 0.1)
y1 = -.75
y2 = .75
ax.fill_between(x, y1, y2, color='lawngreen', alpha='.6')
ax.scatter(df.A, df.B)
ax.plot(df.A, df.B)
ax.axhline(y=0, color='black')
ax.set_xticks(np.arange(0, 999))
ax.set_ylim([-4, 4])
ax.set_xlim([0, df.A.max() + 1])
plt.show()
What am I doing wrong?
Upvotes: 0
Views: 13544
Reputation: 8631
Use fig = plt.figure()
instead of fig = Figure()
.
Your code would be:
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(0, 999, 0.1)
y1 = -.75
y2 = .75
ax.fill_between(x, y1, y2, color='lawngreen', alpha='.6')
ax.scatter(df.A, df.B)
ax.plot(df.A, df.B)
ax.axhline(y=0, color='black')
ax.set_xticks(np.arange(0, 999))
ax.set_ylim([-4, 4])
ax.set_xlim([0, df.A.max() + 1])
plt.show()
Output:
Upvotes: 2