小编Mik*_*lla的帖子

Matplotlib Legends不工作

自从升级matplotlib以来,每次尝试创建图例时都会出现以下错误:

/usr/lib/pymodules/python2.7/matplotlib/legend.py:610: UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x3a30810>]
Use proxy artist instead.

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

  warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str(orig_handle),))
/usr/lib/pymodules/python2.7/matplotlib/legend.py:610: UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x3a30990>]
Use proxy artist instead.

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

  warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str(orig_handle),))
Run Code Online (Sandbox Code Playgroud)

这甚至发生在这样一个简单的脚本:

import matplotlib.pyplot as plt

a = [1,2,3]
b = [4,5,6]
c = [7,8,9]

plot1 = plt.plot(a,b)
plot2 = plt.plot(a,c)

plt.legend([plot1,plot2],["plot 1", "plot 2"])
plt.show()
Run Code Online (Sandbox Code Playgroud)

我发现错误指向我的链接在诊断错误源方面毫无用处.

python plot matplotlib

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

参数太多的类:更好的设计策略?

我正在使用神经元模型.我正在设计的一个类是一个细胞类,它是神经元的拓扑描述(几个连接在一起的隔室).它有许多参数,但它们都是相关的,例如:

轴突节段数,顶端双分裂,体长,体细胞直径,顶端长度,分枝随机性,分枝长度等等...总共约有15个参数!

我可以将所有这些设置为某个默认值,但我的课看起来很疯狂,有几行参数.这种事情也偶尔会发生在其他人身上,是否有一些明显更好的方法来设计这个或者我做对了吗?

更新: 正如你们中的一些人已经要求我已经为类添加了我的代码,因为你可以看到这个类有大量的参数(> 15)但它们都被使用并且是定义单元格拓扑所必需的.问题主要在于他们创建的物理对象非常复杂.我附上了这个类产生的对象的图像表示.有经验的程序员如何以不同的方式做到这一点以避免定义中的这么多参数?

在此输入图像描述

class LayerV(__Cell):

    def __init__(self,somatic_dendrites=10,oblique_dendrites=10,
                somatic_bifibs=3,apical_bifibs=10,oblique_bifibs=3,
                L_sigma=0.0,apical_branch_prob=1.0,
                somatic_branch_prob=1.0,oblique_branch_prob=1.0,
                soma_L=30,soma_d=25,axon_segs=5,myelin_L=100,
                apical_sec1_L=200,oblique_sec1_L=40,somadend_sec1_L=60,
                ldecf=0.98):

        import random
        import math

        #make main the regions:
        axon=Axon(n_axon_seg=axon_segs)

        soma=Soma(diam=soma_d,length=soma_L)

        main_apical_dendrite=DendriticTree(bifibs=
                apical_bifibs,first_sec_L=apical_sec1_L,
                L_sigma=L_sigma,L_decrease_factor=ldecf,
                first_sec_d=9,branch_prob=apical_branch_prob)

        #make the somatic denrites

        somatic_dends=self.dendrite_list(num_dends=somatic_dendrites,
                       bifibs=somatic_bifibs,first_sec_L=somadend_sec1_L,
                       first_sec_d=1.5,L_sigma=L_sigma,
                       branch_prob=somatic_branch_prob,L_decrease_factor=ldecf)

        #make oblique dendrites:

        oblique_dends=self.dendrite_list(num_dends=oblique_dendrites,
                       bifibs=oblique_bifibs,first_sec_L=oblique_sec1_L,
                       first_sec_d=1.5,L_sigma=L_sigma,
                       branch_prob=oblique_branch_prob,L_decrease_factor=ldecf)

        #connect axon to soma:
        axon_section=axon.get_connecting_section()
        self.soma_body=soma.body
        soma.connect(axon_section,region_end=1)

        #connect apical dendrite to soma:
        apical_dendrite_firstsec=main_apical_dendrite.get_connecting_section()
        soma.connect(apical_dendrite_firstsec,region_end=0)

        #connect oblique dendrites to apical first section:
        for dendrite in oblique_dends:
            apical_location=math.exp(-5*random.random()) #for now connecting randomly but need to do this …
Run Code Online (Sandbox Code Playgroud)

python oop neuroscience

69
推荐指数
6
解决办法
2万
查看次数

调用println!错误:预期文字/格式参数必须是字符串文字

这个非常简单的Rust程序:

fn main() {
    let c = "hello";
    println!(c);
}
Run Code Online (Sandbox Code Playgroud)

抛出以下编译时错误:

error: expected a literal
 --> src/main.rs:3:14
  |
3 |     println!(c);
  |              ^
Run Code Online (Sandbox Code Playgroud)

在以前版本的Rust中,错误说:

error: format argument must be a string literal.
     println!(c);
              ^
Run Code Online (Sandbox Code Playgroud)

用以下代替程序:

fn main() {
    println!("Hello");    
}
Run Code Online (Sandbox Code Playgroud)

工作良好.

这个错误的含义对我来说并不清楚,谷歌的搜索并没有真正揭示它.为什么传递cprintln!宏会导致编译时错误?这似乎是非常不寻常的行为.

rust

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

python关键字列表

我正在编写一个程序,它为python执行一些代码生成,如果它是一个python关键字,需要区别对待字符串.help(keywords)打印python关键字和一些额外的东西,但我想知道是否有一种pythonic方法来获取具有这些字符串的实际可迭代对象?

python

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

基于概率的真或假输出

是否有一个Python的标准函数,它根据从0到1的随机数的输入概率性地输出True或False?

我的意思是:

def decision(probability):
    ...code goes here...
    return ...True or False...
Run Code Online (Sandbox Code Playgroud)

上面的例子,如果给出一个输入,比方说,0.7将以70%的概率返回True,而以30%的概率返回false

python scientific-computing

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

终端中的Python ASCII图

使用Octave,我可以将数组绘制到终端,例如,绘制一个带有函数值的数组,x^2在我的终端中输出:

   10000 ++---------+-----------+----------+-----------+---------++
         ++         +           +          +           +         ++
         |+         :           :          :           :         +|
         |++        :           :          :           :        ++|
         | +        :           :          :           :        + |
         | ++       :           :          :           :       ++ |
    8000 ++.+..................................................+.++
         |  ++      :           :          :           :      ++  |
         |   ++     :           :          :           :     ++   |
         |    +     :           :          :           :     +    |
         |    ++    :           :          :           :    ++    |
         |     +    :           :          :           :    + …
Run Code Online (Sandbox Code Playgroud)

python matplotlib

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

与distutils共享库依赖项

我是distutils的新手,我有一个问题,真的让我陷入困境.我正在编译一个需要扩展的包,所以我这样做了扩展:

    a_module = Extension(
          "amodule",
          ["initmodule.cpp"],
          library_dirs=libdirs,
          extra_objects = [
                    "unix/x86_64/lib/liba.so"
                    "unix/x86_64/lib/lib.so",
                    "unix/x86_64/lib/libc.so"],
    )
Run Code Online (Sandbox Code Playgroud)

然后我运行安装方法:

    setup(name="apackage", version="7.2",
      package_dir = {'':instdir+'/a/b/python'},
      packages=['apackage','package.tests'],
      ext_modules=[hoc_module]
)
Run Code Online (Sandbox Code Playgroud)

包分发是正确的,我可以"python setup.py安装"罚款,但当我尝试导入我的包时,我收到一个错误 ImportError: liba.so.0: cannot open shared object file: No such file or directory

我意识到当我将liba.so.0的位置添加到我的LD_LIBRARY_PATH时,程序运行正常.不幸的是,我没有编写这些模块,也没有很好地理解编译.我一直试图解决这个问题几天无济于事.

更新:我尝试将liba.a,libb.a等文件传递给extra_objects,但这不起作用,生成以下错误:liba.a:无法读取符号:错误值collect2:ld返回1退出状态.我正在尝试做的是打包一个python模块,它需要编译一个库本身依赖于我需要以某种方式包含在包中的其他库.我怀疑我的问题非常类似于这个:http:/ /mail.python.org/pipermail/distutils-sig/2009-February/010960.html但那个问题没有得到解决,我想也许已经有两年了,已经找到了解决方案?

更新2:现在我通过这样做解决了这个问题:

      data_files=[('/usr/local/lib', glob.glob('unix/x86_64/lib/*'))]
Run Code Online (Sandbox Code Playgroud)

也就是说,我正在将我需要的库复制到/ usr/local/lib中.我对这个解决方案并不是非常满意,尤其是因为它要求我的用户拥有root权限,并且因为这可能仍然无法使用Redhat发行版.所以,如果有人能提出比这个修复更好的建议,请告诉我.

python packaging software-distribution distutils distribution

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

Python正则表达式来过滤字符串列表

我试图用正则表达式过滤字符串列表,如本答案所示.但是代码会产生意外结果:

In [123]: r = re.compile('[0-9]*')
In [124]: string_list = ['123', 'a', '467','a2_2','322','21']
In [125]: filter(r.match, string_list)
Out[125]: ['123', 'a', '467', 'a2_2', '322_2', '21']
Run Code Online (Sandbox Code Playgroud)

我期待输出['123', '467', '21'].

python regex

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

使用非文字模式时,为什么此匹配模式无法访问?

以下代码(游乐场)

let max_column = 7;
edge = match current_column {
    0 => Edge::Left,
    max_column => Edge::Right,
    _ => Edge::NotAnEdge
};
Run Code Online (Sandbox Code Playgroud)

导致以下错误:

warning: unreachable pattern
  --> src/main.rs:10:9
   |
9  |         max_column => Edge::Right,
   |         ---------- matches any value
10 |         _ => Edge::NotAnEdge
   |         ^ unreachable pattern
   |
   = note: #[warn(unreachable_patterns)] on by default
Run Code Online (Sandbox Code Playgroud)

max_column用文字替换变量可以正常工作:

let max_column = 7;
edge = match current_column {
    0 => Edge::Left,
    7 => Edge::Right,
    _ => Edge::NotAnEdge
};
Run Code Online (Sandbox Code Playgroud)

为什么_在第一个示例中无法访问任何值current_column …

rust

16
推荐指数
1
解决办法
1347
查看次数

Ansible,使用不同的参数集多次运行角色

使用不同参数集运行一个角色的最佳做法是什么?

我需要在一台服务器上多次运行一个应用程序(docker container),每个服务器都有不同的环境变量.

ansible docker ansible-playbook

15
推荐指数
3
解决办法
7980
查看次数