小编Whu*_*hud的帖子

在Python中绘制同一图表中的列表列表

我试图描绘(x,y)在哪里y = [[1,2,3],[4,5,6],[7,8,9]].

说,len(x) = len(y[1]) = len(y[2]).. y的长度由用户输入决定.我想在同一个图中绘制y的多个图,即(x, y[1],y[2],y[3],...).当我尝试使用循环时,它说dimension error.

我也尝试过: plt.plot(x,y[i] for i in range(1,len(y)))

我该如何策划?请帮忙.

for i in range(1,len(y)):
plt.plot(x,y[i],label = 'id %s'%i)
plt.legend()
plt.show()
Run Code Online (Sandbox Code Playgroud)

python matplotlib nested-lists

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

为什么Tkinter的单选按钮在使用StringVar但不是IntVar时全部启动?

下面是一些创建4个单选按钮的示例代码,2个使用int,2个使用str:

from tkinter import *

class test:
    def __init__(self):
        wind = Tk()

        frame1 = Frame(wind)
        frame1.pack()
        self.v1 = IntVar()
        self.v2 = StringVar()

        int1 = Radiobutton(frame1, text = 'int1', variable = self.v1, value = 1, command = self.ipress)
        int2 = Radiobutton(frame1, text = 'int2', variable = self.v1, value = 2, command = self.ipress)

        str1 = Radiobutton(frame1, text = 'str1', variable = self.v2, value = '1', command = self.spress)
        str2 = Radiobutton(frame1, text = 'str2', variable = self.v2, value = '2', command …
Run Code Online (Sandbox Code Playgroud)

python tk-toolkit tkinter radio-button python-3.x

6
推荐指数
1
解决办法
8446
查看次数

是否有一种更简单的方法来生成一个numpy矩阵,其中数字垂直上下蠕动

对不起,我不知道正确的术语,因此'垂直蠕虫'基本上我希望数字从左下角开始,当它上升到它添加的列时,然后下一列的最低数字在顶部和随着它的下降而增加.例如:

[2, 3, 8
 1, 4, 7
 0, 5, 6]
Run Code Online (Sandbox Code Playgroud)

到目前为止,我想出了这个非常膨胀的代码,它基本上使一个列然后复制它,翻转它,添加最后一列的最大值加1,然后hstack将它们放在一起,例如:

第一栏

[2,
 1,
 0,]
Run Code Online (Sandbox Code Playgroud)

翻转:

[0,
 1,
 2,]
Run Code Online (Sandbox Code Playgroud)

添加最后一个加1的最大值(在这种情况下为3):

 [3,
  4,
  5,]
Run Code Online (Sandbox Code Playgroud)

他们:

[2, 3
 1, 4
 0, 5]
Run Code Online (Sandbox Code Playgroud)

然后,我只是重复这个我想要的列数.加快速度,如果我想要4我可以采取最后的hstack并复制两列并添加最大值.不需要翻转,因为它的列数是偶数.

我需要的矩阵在12乘21矩阵中有252个项目.这是我到目前为止的代码:

import numpy as np

a = np.arange(20,-1,-1).reshape(21,1)
b = a+1 + a[::-1]*2
c = b+1 + a*2
d = np.hstack((a,b,c))
e = np.hstack((d,np.flip(d,0)+ d.shape[0] * d.shape[1]))
f = np.hstack((e,e + e.shape[0] * e.shape[1]))
del a,b,c,d,e

>>> f
array([[ 20,  21,  62,  63, 104, 105, 146, 147, 188, 189, …
Run Code Online (Sandbox Code Playgroud)

python numpy python-3.x

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

在numpy中返回矩阵邻居总和的最简单方法

我正在尝试制作一个需要矩阵的邻居(不包括自身)和 ex 的程序:

 matrix([[0, 0, 0],
        [1, 0, 1],
        [0, 1, 0]])  
Run Code Online (Sandbox Code Playgroud)

会返回:

matrix([[1, 2, 1],
        [1, 3, 1],
        [2, 2, 2]])
Run Code Online (Sandbox Code Playgroud)

我这里有一个工作代码,但它又大又乱,而且我是 numpy 的新手,所以我需要一些帮助来清理和优化它。(我觉得必须有更好的方法)

示例代码:

import numpy as np

def NiSum(m):
    new = []
    for x in range(m.shape[0]-1):
        row = []
        for y in range(m.shape[1]-1):
            Ni = 0
            for a in [1,1],[1,0],[1,-1],[0,1],[0,-1],[-1,1],[-1,0],[-1,-1]:
                Ni += m[x+a[0],y+a[1]]
            row.append(Ni)
        new.append(row)
    return np.matrix(new)


example = np.matrix('0 0 0 0 0 0 0 0; '*3+'0 0 0 1 1 1 0 0; '*3+'0 0 …
Run Code Online (Sandbox Code Playgroud)

python numpy matrix scipy python-3.x

3
推荐指数
1
解决办法
3090
查看次数