在Matlab的Python的matplotlib中是否有明确的等效命令hold on?我试图在同一轴上绘制所有图形.一些图表是一个内部产生for循环,而这些是从单独绘制su和sl:
import numpy as np
import matplotlib.pyplot as plt
for i in np.arange(1,5):
z = 68 + 4 * np.random.randn(50)
zm = np.cumsum(z) / range(1,len(z)+1)
plt.plot(zm)
plt.axis([0,50,60,80])
plt.show()
n = np.arange(1,51)
su = 68 + 4 / np.sqrt(n)
sl = 68 - 4 / np.sqrt(n)
plt.plot(n,su,n,sl)
plt.axis([0,50,60,80])
plt.show()
Run Code Online (Sandbox Code Playgroud) 我正在使用Python 模块中的函数randn和normal函数numpy.random.这些函数与我在http://docs.scipy.org手册中读到的函数非常相似(它们都涉及高斯分布),但是我应该注意哪些更微妙的差异?如果是这样,在什么情况下我会更好地使用特定的功能?
我最近在Windows 7上安装了Anaconda(使用默认设置).当我尝试通过双击打开.py文件时,我得到了Open with...选项.如何将默认程序设置为Spyder?
如何删除数组中的每个第n个元素?
import numpy as np
x = np.array([0,10,27,35,44,32,56,35,87,22,47,17])
n = 3 # remove every 3rd element
Run Code Online (Sandbox Code Playgroud)
......有点像x[0::n]?我试过这个,但当然不行:
for i in np.arange(0,len(x),n):
x = np.delete(x,i)
Run Code Online (Sandbox Code Playgroud) 删除数组中负面元素的最有效方法是什么?我已尝试numpy.delete并从表单的数组和代码中删除所有特定值x[x != i].
对于:
import numpy as np
x = np.array([-2, -1.4, -1.1, 0, 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10, 14, 16.2])
Run Code Online (Sandbox Code Playgroud)
我想最终得到一个数组:
[0, 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10, 14, 16.2]
Run Code Online (Sandbox Code Playgroud) 我试图了解"狗腿"方法在Python scipy.optimize.minimize函数中的工作原理.我正在调整帮助页面底部的示例.
根据笔记,狗腿洞方法需要Jacobian和Hessian参数.为此,我使用numdifftools包:
import numpy as np
from scipy.optimize import minimize
from numdifftools import Jacobian, Hessian
def fun(x,a):
return (x[0] - 1)**2 + (x[1] - a)**2
x0 = np.array([2,0]) # initial guess
a = 2.5
res = minimize(fun, x0, args=(a), method='dogleg',
jac=Jacobian(fun)([2,0]), hess=Hessian(fun)([2,0]))
print(res)
Run Code Online (Sandbox Code Playgroud)
编辑:
如果我按照以下帖子的建议进行更改,
res = minimize(fun, x0, args=a, method='dogleg',
jac=Jacobian(lambda x: fun(x,a)),
hess=Hessian(lambda x: fun(x,a)))
Run Code Online (Sandbox Code Playgroud)
我收到一个错误TypeError: <lambda>() takes 1 positional argument but 2 were given.我究竟做错了什么?
在初始猜测中计算雅可比行列式和Hessian也是正确的x0吗?
我已经看过matplotlib的3d图的例子了,但这些都没有给我我想要绘制的内容,例如:

该图显示了y轴(N)上的一系列测量值,每个测量值都有一个强度谱(p/2hk_L),即对于您在图中看到的每条线,N是固定的.用于绘制这样的数据的最简单函数是什么?
我已经针对这个问题修改了Comparing FFT of Function to Analytical FT Solution in Matlab 中的代码。我正在尝试执行 FFT 并将结果与Wikipedia 表中的分析表达式进行比较。
我的代码是:
a = 1.223;
fs = 1e5; %sampling frequency
dt = 1/fs;
t = 0:dt:30-dt; %time vector
L = length(t); % no. sample points
t = t - 0.5*max(t); %center around t=0
y = ; % original function in time
Y = dt*fftshift(abs(fft(y))); %numerical soln
freq = (-L/2:L/2-1)*fs/L; %freq vector
w = 2*pi*freq; % angular freq
F = ; %analytical solution
figure; …Run Code Online (Sandbox Code Playgroud) 我正在尝试计算像这篇文章中定义的上不完全伽马函数。如果我使用
from scipy.special import gamma,gammainc
from numpy import linspace
a = 0
z = (2+3j)*np.linspace(0,10)
gamma(a)*(1-gammainc(a,z))
Run Code Online (Sandbox Code Playgroud)
哪里z是一个复杂的向量我得到一个错误
TypeError: ufunc 'gammainc' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Run Code Online (Sandbox Code Playgroud)
是否有替代函数来进行计算?当我尝试使用 WolframAlpha 的Gamma函数执行此操作时,似乎没有问题。