iPython 笔记本停止显示条形图
iPython Notebook quits displaying bar chart
我有一个 Python 脚本,我想用它来学习 iPython Notebook,我在处理文本时遇到了显示图表的问题。
这是我正在使用的部分代码。
#import baseline and iteration files
df_final = pd.read_csv(os.path.join(CSV_dir, 'mean_prop_iter.csv'))
baseline = pd.read_csv(os.path.join(CSV_dir, 'mean_prop_base.csv'))
## create base figure
fig = plt.figure()
ax1 = fig.add_subplot(111)
## create histogram
df_final['iter_prop_in'].hist(bins=[0.5, 0.51, 0.52, 0.53, 0.54,
0.55, 0.56, 0.57, 0.58, 0.59, 0.60],
rwidth=1.0, align='mid', facecolor='red')
此时一切看起来都很好。
我的后续命令不是我所期望的。
## define x-axis limits
label_range = np.arange(0.0, 1.1, 0.1)
labels = ax1.set_xticks(label_range)
## turn off ticks along the top and right axes
plt.tick_params(axis='both', top='off', right='off', direction='out')
此处正确显示了新的 x 轴范围,但未显示直方图。 y 轴刻度限制也变为 0 到 1,这是我不希望的。
如果我 运行 在 Python 2.7 (Anaconda Spyder) 中使用相同的命令,直方图会按我的预期显示。
我做错了什么?
这是因为您是在笔记本的不同单元格中进行的。您应该将属于同一图形的绘图命令分组到一个单元格中。
按照您目前的代码,Notebook 认为您指的是一个新图形。
无关提示:您可以考虑在与绘图相关的命令后缀 ;
,这表示返回值输出抑制。省略它往往会在单元格输出中打印出大量乱码。
我有一个 Python 脚本,我想用它来学习 iPython Notebook,我在处理文本时遇到了显示图表的问题。
这是我正在使用的部分代码。
#import baseline and iteration files
df_final = pd.read_csv(os.path.join(CSV_dir, 'mean_prop_iter.csv'))
baseline = pd.read_csv(os.path.join(CSV_dir, 'mean_prop_base.csv'))
## create base figure
fig = plt.figure()
ax1 = fig.add_subplot(111)
## create histogram
df_final['iter_prop_in'].hist(bins=[0.5, 0.51, 0.52, 0.53, 0.54,
0.55, 0.56, 0.57, 0.58, 0.59, 0.60],
rwidth=1.0, align='mid', facecolor='red')
此时一切看起来都很好。
我的后续命令不是我所期望的。
## define x-axis limits
label_range = np.arange(0.0, 1.1, 0.1)
labels = ax1.set_xticks(label_range)
## turn off ticks along the top and right axes
plt.tick_params(axis='both', top='off', right='off', direction='out')
此处正确显示了新的 x 轴范围,但未显示直方图。 y 轴刻度限制也变为 0 到 1,这是我不希望的。
如果我 运行 在 Python 2.7 (Anaconda Spyder) 中使用相同的命令,直方图会按我的预期显示。
我做错了什么?
这是因为您是在笔记本的不同单元格中进行的。您应该将属于同一图形的绘图命令分组到一个单元格中。
按照您目前的代码,Notebook 认为您指的是一个新图形。
无关提示:您可以考虑在与绘图相关的命令后缀 ;
,这表示返回值输出抑制。省略它往往会在单元格输出中打印出大量乱码。