小编ler*_*ygr的帖子

使用Pandas条形图上的值注释条形图

我正在寻找一种方法来使用我的DataFrame中的舍入数值来在Pandas条形图中注释我的条形图.

>>> df=pd.DataFrame({'A':np.random.rand(2),'B':np.random.rand(2)},index=['value1','value2'] )         
>>> df
                 A         B
  value1  0.440922  0.911800
  value2  0.588242  0.797366
Run Code Online (Sandbox Code Playgroud)

我想得到这样的东西:

条形图注释示例

我尝试使用此代码示例,但注释都集中在x刻度上:

>>> ax = df.plot(kind='bar') 
>>> for idx, label in enumerate(list(df.index)): 
        for acc in df.columns:
            value = np.round(df.ix[idx][acc],decimals=2)
            ax.annotate(value,
                        (idx, value),
                         xytext=(0, 15), 
                         textcoords='offset points')
Run Code Online (Sandbox Code Playgroud)

python plot matplotlib dataframe pandas

60
推荐指数
3
解决办法
5万
查看次数

获取列名称,其中value是pandas dataframe中的值

我试图在每个时间戳找到数据框中的列名,其值与同一时间戳的时间序列中的列匹配.

这是我的数据帧:

>>> df
                            col5        col4        col3        col2        col1
1979-01-01 00:00:00  1181.220328  912.154923  648.848635  390.986156  138.185861
1979-01-01 06:00:00  1190.724461  920.767974  657.099560  399.395338  147.761352
1979-01-01 12:00:00  1193.414510  918.121482  648.558837  384.632475  126.254342
1979-01-01 18:00:00  1171.670276  897.585930  629.201469  366.652033  109.545607
1979-01-02 00:00:00  1168.892579  900.375126  638.377583  382.584568  132.998706

>>> df.to_dict()
{'col4': {<Timestamp: 1979-01-01 06:00:00>: 920.76797370744271, <Timestamp: 1979-01-01 00:00:00>: 912.15492332839756, <Timestamp: 1979-01-01 18:00:00>: 897.58592995700656, <Timestamp: 1979-01-01 12:00:00>: 918.1214819496729}, 'col5': {<Timestamp: 1979-01-01 06:00:00>: 1190.7244605667831, <Timestamp: 1979-01-01 00:00:00>: 1181.2203275146587, <Timestamp: 1979-01-01 18:00:00>: 1171.6702763228691, <Timestamp: 1979-01-01 12:00:00>: 1193.4145103184442}, …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

18
推荐指数
4
解决办法
3万
查看次数

Matplotlib:用鼠标绘制一个矩形形状的选区

我希望能够使用鼠标事件在matplotlib图上绘制选区.我没有找到有关如何使用python执行此操作的信息.

最后,我希望能够在使用matplotlib底图创建的地图上用鼠标绘制感兴趣的区域并检索角坐标.

任何人都有想法,例如,参考?

谢谢,

格雷格

class Annotate(object):
  def __init__(self):
      self.ax = plt.gca()
      self.rect = Rectangle((0,0), 1, 1, facecolor='None', edgecolor='green')
      self.x0 = None
      self.y0 = None
      self.x1 = None
      self.y1 = None
      self.ax.add_patch(self.rect)
      self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
      self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)
      self.ax.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)
  def on_press(self, event):
      print 'press'
      self.x0 = event.xdata
      self.y0 = event.ydata    
      self.x1 = event.xdata
      self.y1 = event.ydata
      self.rect.set_width(self.x1 - self.x0)
      self.rect.set_height(self.y1 - self.y0)
      self.rect.set_xy((self.x0, self.y0))
      self.rect.set_linestyle('dashed')
      self.ax.figure.canvas.draw()
  def on_motion(self,event):
      if self.on_press is True:
          return
      self.x1 = event.xdata
      self.y1 = event.ydata
      self.rect.set_width(self.x1 - …
Run Code Online (Sandbox Code Playgroud)

python mouse matplotlib selection matplotlib-basemap

9
推荐指数
2
解决办法
1万
查看次数

带有pandas的OLS:日期时间索引作为预测变量

我想使用pandas OLS函数来为我的数据系列拟合趋势线.有谁知道如何使用熊猫系列中的日期时间索引作为OLS中的预测器?

例如,假设我有一个简单的时间序列:

>>> ts
2001-12-31    19.828763
2002-12-31    20.112191
2003-12-31    19.509116
2004-12-31    19.913656
2005-12-31    19.701649
2006-12-31    20.022819
2007-12-31    20.103024
2008-12-31    20.132712
2009-12-31    19.850609
2010-12-31    19.290640
2011-12-31    19.936210
2012-12-31    19.664813
Freq: A-DEC
Run Code Online (Sandbox Code Playgroud)

我想使用索引作为预测器对其进行OLS:

model = pd.ols(y=ts,x=ts.index,intercept=True)
Run Code Online (Sandbox Code Playgroud)

但由于x是日期时间索引的列表,因此该函数返回错误.有人有想法吗?

我可以使用scipy.stats的linregress,但我想知道它是否可能与Pandas.

谢谢,格雷格

python datetime series linear-regression pandas

9
推荐指数
1
解决办法
8580
查看次数

用 Python Click 编写的 CLI 响应缓慢

我在 Python 3.6.6(在 conda 环境下)上使用 Click 7.0 编写的 CLI 响应缓慢。

使用 pip 安装包(使用 setuptools)后,调用 CLI 时打印帮助消息需要时间:

$ time cli
Usage: cli [OPTIONS] COMMAND [ARGS]...

  Welcome in the CLI!

Options:
  --version  Show the version and exit.
  --help     Show this message and exit.

real    0m0,523s
user    0m0,482s
sys     0m0,042s
Run Code Online (Sandbox Code Playgroud)

但是,直接从源调用 CLI 时,我没有遇到这种延迟:

$ time python myproject/cli.py 
Usage: cli.py [OPTIONS] COMMAND [ARGS]...

  Welcome in the CLI!

Options:
  --version  Show the version and exit.
  --help     Show this message and exit.

real    0m0,088s
user    0m0,071s …
Run Code Online (Sandbox Code Playgroud)

python setuptools anaconda conda python-click

7
推荐指数
1
解决办法
1012
查看次数