我正在学习如何最近玩matplotlib。但是,出现了一些问题。我使用以下命令读入名为students.data的非标准数据文件。
student_dataset = pd.read_csv("students.data", index_col=0)
这就是students.data的样子。
然后我使用以下命令在其中绘制一个带有四个直方图子图的图形。
fig = plt.figure(0) #Use it to create subplots.
fig.subplots_adjust(hspace=0.5, wspace=0.5) #Adjust height-spacing to
#de-overlap titles and ticks
ax1 = fig.add_subplot(2, 2, 1)
my_series1 = student_dataset["G1"]
my_series1.plot.hist(alpha=0.5, color = "blue", histtype = "bar", bins = 30)
ax2 = fig.add_subplot(2, 2, 2)
my_series2 = student_dataset["G2"]
my_series2.plot.hist(alpha=1, color = "green", histtype = "step", bins = 20)
ax3 = fig.add_subplot(2, 2, 3)
my_series3 = student_dataset["G3"]
my_series3.plot.hist(alpha=0.5, color = "red", histtype = "stepfilled")
ax4 = fig.add_subplot(2, 2, 4)
my_series1.plot.hist(alpha=0.5, color = "blue")
my_series2.plot.hist(alpha=0.5, color = "green")
my_series3.plot.hist(alpha=0.5, color = "red")
结果正是我想要的东西。但是,当我尝试对散点子图进行此操作时,它们在不同的图中分开。我无法弄清楚原因。这是命令。
fig = plt.figure(2)
ax1 = fig.add_subplot(2, 2, 1)
student_dataset.plot.scatter(x = "freetime", y = "G1")
ax2 = fig.add_subplot(2, 2, 2)
student_dataset.plot.scatter(x = "freetime", y = "G2")
ax3 = fig.add_subplot(2, 2, 3)
student_dataset.plot.scatter(x = "freetime", y = "G3")
在搜索了一天后,我发现the solution几乎适合我的目标。但是,为什么呢?为什么我原来的方法不起作用?
以下是新命令和结果。
fig, axes = plt.subplots(2, 2, figsize=(6, 6), sharex=False, sharey=False)
x = student_dataset["freetime"].values
for i in range(3):
axes[i//2, i%2].scatter(x, student_dataset.iloc[:, i + 25].values)
fig.tight_layout()
很抱歉,我不能在这篇文章中添加更多图片来描述我的问题。希望你能理解我的观点。
提前致谢。
答案 0 :(得分:0)
您可以选择使用链接问题的选项2,
fig = plt.figure(2)
ax1 = fig.add_subplot(2, 2, 1)
student_dataset.plot.scatter(x = "freetime", y = "G1", ax=ax1)
ax2 = fig.add_subplot(2, 2, 2)
student_dataset.plot.scatter(x = "freetime", y = "G2", ax=ax2)
ax3 = fig.add_subplot(2, 2, 3)
student_dataset.plot.scatter(x = "freetime", y = "G3", ax=ax3)
如果你没有指定ax
,大熊猫会产生一个新的数字。
目前我对plot.hist
不需要ax
关键字的原因没有任何好的解释;它可能与它直接调用plt.hist
函数而不是首先预处理数据有关。