逐行运行代码与选择运行(在使用Spyder的Ipython中) [英] Running code line by line versus as a selection (in Ipython using Spyder)

查看:5368
本文介绍了逐行运行代码与选择运行(在使用Spyder的Ipython中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在慢慢地从R转换到Python,而一些更微妙的差异让我有点疯狂。我在博客



选项1,第2步





选项2





我猜测结果会以某种方式相同引擎盖,我尝试使用 plt.show 渲染图表, plt.draw fig.draw 。但到目前为止还没有运气。



我认为这个问题的答案与IPython中的非常基本功能和/或如何将这些元素分配给内存有关,但整件事让我感到困惑。我希望你们中的一些人能够抽出时间来解释这个问题,或许就如何解决这些问题提供进一步的建议。



谢谢!



编辑:



我在Windows上使用Spyder 2.3.8和Python 3.5.1

解决方案

在Spyder的IPython控制台中,如果在单元格中检测到图形对象,将显示该图。

因为 fig,ax = plt.subplots()中有一个数字对象,所以显示(空)数字。



如果之后在轴上执行绘图命令,则不会检测到图形对象,因此只有单元格的返回显示为文本。



plt.show()在这里没有帮助(不要问我为什么没有实施)。



但是,您可以随时简单地说明对图的引用, fig 以获取图的图像。




I'm slowly transitioning from R to Python, and some of the more subtle differences are driving me a bit nuts. I found a really interesting guide on Effectively Using Matplotlib in the blog Practical Business Python

Here, the author shows how to build a chart step by step using the following lines of code (short version):

# Modules
import pandas as pd
import matplotlib.pyplot as plt

# Get the data
df = pd.read_excel("https://github.com/chris1610/pbpython/blob/master/data/sample-salesv3.xlsx?raw=true")
df.head()

# Rearrange data
top_10 = (df.groupby('name')['ext price', 'quantity'].agg({'ext price': 'sum', 'quantity': 'count'})
      .sort_values(by='ext price', ascending=False))[:10].reset_index()
top_10.rename(columns={'name': 'Name', 'ext price': 'Sales', 'quantity': 'Purchases'}, inplace=True)

# Customize plot
plt.style.use('ggplot')

# And here's the part that puzzles me:
fig, ax = plt.subplots()
top_10.plot(kind='barh', y="Sales", x="Name", ax=ax)

I'm messing around with this in Spyder, and I've noticed that there is a difference betweene running parts of this code line by line versus running the same lines as a selection.

Option 1, step 1:

Option 1, step 2

Option 2

I was guessing that the result somehow would be the same "under the hood", and I've tried rendering the chart using plt.show, plt.draw and fig.draw. But no luck so far.

I assume that the answer to this has got something to do with very basic functionality in IPython and / or how these elements are assigned to memory, but the whole thing just leaves me confused. I'm hoping some of you find the time to explaing this, and perhaps offer further suggestions on how to wrestle with these things.

Thanks!

Edit:

I'm using Spyder 2.3.8 with Python 3.5.1 on Windows

解决方案

In the IPython console within Spyder, the figure will be shown if a figure object is detected in the cell.
Because the fig, ax = plt.subplots() has a figure object in it, the (empty) figure is shown.

If afterwards a plotting command on the axes is effectuated, no figure object is detected, so only the return of the cell is shown as text.

plt.show() will not help here (don't ask me why it hasn't been implemented).

However, you can at any point simply state the reference to the figure, fig to obtain the image of the figure.

这篇关于逐行运行代码与选择运行(在使用Spyder的Ipython中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆