Matplotlib - Subplot没有显示

时间:2018-05-06 17:34:49

标签: python matplotlib

我想将正常情节转变为子情节。这是情节的代码,有效:

ListofLists->next

然后我试图将其变成一个子图。控制台不会抛出任何错误,它只是没有显示任何情节。

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

使用fig = plt.figure()代替fig = Figure()

您的代码将是:

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

输出:

enter image description here