小编Jas*_*pel的帖子

Matplotlib不同大小的子图

我需要在图中添加两个子图.一个子图需要大约是第二个(相同高度)的三倍.我使用GridSpeccolspan论证完成了这个,但我想这样做,figure所以我可以保存为PDF.我可以使用figsize构造函数中的参数调整第一个数字,但是如何更改第二个图的大小?

python plot matplotlib figure

206
推荐指数
6
解决办法
24万
查看次数

Python Matplotlib Y-Axis在Plot右侧打勾

我有一个简单的线图,需要将y轴刻度从绘图的(默认)左侧移动到右侧.有关如何做到这一点的任何想法?

python matplotlib

101
推荐指数
4
解决办法
9万
查看次数

按位置选择pandas列

我只是试图通过整数访问命名的pandas列.

您可以使用按位置选择行df.ix[3].

但如何按整数选择列?

我的数据帧:

df=pandas.DataFrame({'a':np.random.rand(5), 'b':np.random.rand(5)})
Run Code Online (Sandbox Code Playgroud)

python indexing pandas

82
推荐指数
4
解决办法
14万
查看次数

Python - 返回第一个N键:来自dict的值对

考虑以下字典,d:

d = {'a': 3, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
Run Code Online (Sandbox Code Playgroud)

我想从d返回前N个键:值对(在这种情况下N <= 4).这样做最有效的方法是什么?

python dictionary

78
推荐指数
10
解决办法
12万
查看次数

使用PyMongo时,Collection对象不可调用错误

继PyMongo 教程之后,在insert_one集合上调用方法时出现错误.

In [1]: import pymongo

In [2]: from pymongo import MongoClient

In [3]: client = MongoClient()

In [4]: db = client.new_db

In [5]: db
Out[5]: Database(MongoClient('localhost', 27017), u'new_db')

In [6]: posts = db.posts

In [7]: posts.insert_one({'a':1})
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-2271c01f9a85> in <module>()
----> 1 posts.insert_one({'a':1})

C:\Anaconda\lib\site-packages\pymongo-2.8-py2.7-win32.egg\pymongo\collection.py in __call__(self, *a
rgs, **kwargs)
   1771                         "call the '%s' method on a 'Collection' object it is "
   1772                         "failing because no such method exists." %
-> …
Run Code Online (Sandbox Code Playgroud)

python mongodb pymongo

46
推荐指数
2
解决办法
4万
查看次数

三个列表压缩成dicts列表

考虑以下:

>>> # list of length n
>>> idx = ['a', 'b', 'c', 'd']

>>> # list of length n
>>> l_1 = [1, 2, 3, 4]

>>> # list of length n
>>> l_2 = [5, 6, 7, 8]

>>> # first key
>>> key_1 = 'mkt_o'

>>> # second key
>>> key_2 = 'mkt_c'
Run Code Online (Sandbox Code Playgroud)

我怎么把这个烂摊子看起来像这样?

{
    'a': {'mkt_o': 1, 'mkt_c': 5},
    'b': {'mkt_o': 2, 'mkt_c': 6},
    'c': {'mkt_o': 3, 'mkt_c': 6},
    'd': {'mkt_o': 4, 'mkt_c': 7},
    ...
} …
Run Code Online (Sandbox Code Playgroud)

python mapping zip dictionary

20
推荐指数
2
解决办法
2008
查看次数

Python:寻找非线性方程的多个根

假设以下功能:

f(x) = x * cos(x-4)

有了x = [-2.5, 2.5]这个功能跨越0f(0) = 0f(-0.71238898) = 0.

这是通过以下代码确定的:

import math
from scipy.optimize import fsolve
def func(x):
    return x*math.cos(x-4)
x0 = fsolve(func, 0.0)
# returns [0.]
x0 = fsolve(func, -0.75)
# returns [-0.71238898]
Run Code Online (Sandbox Code Playgroud)

使用fzero(或任何其他Python根查找程序)在一次调用中查找两个根的正确方法是什么?是否有不同的scipy功能呢?

fzero 参考

python optimization scipy

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

在Swagger 2.0中定义多个模型的数组

这是我第一次涉足Swagger,所以请保持温柔.

我有以下定义:

definitions:
  Payload:
    type: object
    properties:
      indicators:
        type: array
        items:
          $ref: '#/definitions/Indicator'
  Indicator:
    type: object
    properties:
      type:
        type: string
      computeOn:
        type: array
        items:
          type: string
        default:
          - close
      parameters:
        type: object
  BBANDS:
    properties:
      type:
        type: string
        default: BBANDS
      computeOn:
        type: array
        items:
          type: string
        default:
          - close
      parameters:
        type: object
        properties:
          timeperiod:
            type: integer
            format: int32
            default: 5
          nbdevup:
            type: integer
            format: int32
            default: 2
          nbdevdn:
            type: integer
            format: int32
            default: 2
          matype:
            type: integer
            format: int32
            default: 0 …
Run Code Online (Sandbox Code Playgroud)

swagger swagger-2.0

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

在网上绘制matplotlib

以下代码当然会创建一个名为test的PNG并将其保存在服务器上:

from matplotlib.figure import Figure                         
from matplotlib.backends.backend_agg import FigureCanvasAgg  

fig = Figure(figsize=[4,4])                                  
ax = fig.add_axes([.1,.1,.8,.8])                             
ax.scatter([1,2], [3,4])                                     
canvas = FigureCanvasAgg(fig)                                
canvas.print_figure("test.png")
Run Code Online (Sandbox Code Playgroud)

然后要在浏览器中查看图像,我们必须转到example.com/test.png.这意味着我们必须首先使用Python代码调用页面来创建test.png文件,然后转到PNG文件.有没有办法从创建图像的Python页面绘制PNG和输出?谢谢!

python matplotlib

10
推荐指数
2
解决办法
9925
查看次数

在Python中计算复合的返回系列

问候所有,我有两个系列的数据:每日原始股票价格回报(正或负浮动)和交易信号(买= 1,卖= -1,无交易= 0).

原始价格回报只是今天价格的对数除以昨天的价格:

log(p_today / p_yesterday)
Run Code Online (Sandbox Code Playgroud)

一个例子:

raw_return_series = [ 0.0063 -0.0031 0.0024 ..., -0.0221 0.0097 -0.0015]
Run Code Online (Sandbox Code Playgroud)

交易信号系列如下:

signal_series = [-1. 0. -1. -1. 0. 0. -1. 0. 0. 0.]
Run Code Online (Sandbox Code Playgroud)

根据交易信号获得每日回报:

daily_returns = [raw_return_series[i] * signal_series[i+1] for i in range(0, len(signal_series)-1)]
Run Code Online (Sandbox Code Playgroud)

这些每日回报可能如下所示:

[0.0, 0.00316, -0.0024, 0.0, 0.0, 0.0023, 0.0, 0.0, 0.0] # results in daily_returns; notice the 0s
Run Code Online (Sandbox Code Playgroud)

我需要使用daily_returns系列来计算复合的返回系列.但是,假设daily_returns系列中有0个值,我需要将"通过时间"的最后一个非零复合返回到下一个非零复合返回.

例如,我像这样计算复合回报(注意我将随着时间"向后"):

compound_returns = [(((1 + compounded[i + 1]) * (1 + daily_returns[i])) - 1) for i in range(len(compounded) - …
Run Code Online (Sandbox Code Playgroud)

python list-comprehension time-series pandas

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