我已经将文本添加到绘图中,在每行中编码,然后调整它看起来像样,增加或减少宽度,或更改位置.但是,有没有办法让Python知道你想要文本的位置以及你想要它的设置方式?然后我可以添加文本,Python将计算出细节.
例如,看看下面的图片:
在图中,我在左上角有3行文字,在图的线上有一行.
我不得不调整3条线以获得合适的间距.这不是一项艰巨的任务,但如果我能说这里是文本,这里就是位置,那将很容易,然后Python以适当的间距堆叠它.
对于单独的线路,我不得不进行调整,因此它不在线上并降低线路.对于这种情况,有可能告诉python我想在情节上方的文字和80%下线?
我习惯于LaTeX在没有硬编码坐标的情况下进行调整的地方.优点是
(1) if I want to change the location, I can change the percentage shift and not the coordinate.
(2) if the line is angled, the text will adjust to the line.
Run Code Online (Sandbox Code Playgroud)
(2)的优点是我试图将文字放在图中顶部向上倾斜的文本上.
可以这样做,还是我要求多少?如果是这样,我该怎么做?
以下是实现该图的代码:
import numpy as np
import pylab
r1 = 1 # AU Earth
r2 = 1.524 # AU Mars
deltanu = 75 * np.pi / 180 # angle in radians
mu = 38.86984154054163
c = np.sqrt(r1 ** 2 + …Run Code Online (Sandbox Code Playgroud) 在下面的Python中,我有五个函数包含在func我必须集成的数组中.代码调用使用f2py以下代码生成的外部Fortran模块:
import numpy as np
from numpy import cos, sin , exp
from trapzdv import trapzdv
def func(x):
return np.array([x**2, x**3, cos(x), sin(x), exp(x)])
if __name__ == '__main__':
xs = np.linspace(0.,20.,100)
ans = trapzdv(func,xs,5)
print 'from Fortran:', ans
print 'exact:', np.array([20**3/3., 20**4/4., sin(20.), -cos(20.), exp(20.)])
Run Code Online (Sandbox Code Playgroud)
Fortran例程是:
subroutine trapzdv(f,xs,nf,nxs,result)
integer :: I
double precision :: x1,x2
integer, intent(in) :: nf, nxs
double precision, dimension(nf) :: fx1,fx2
double precision, intent(in), dimension(nxs) :: xs
double precision, intent(out), dimension(nf) :: …Run Code Online (Sandbox Code Playgroud) 在这个问题阐述了如何访问lower和upper给定矩阵的triagular部分,说:
m = np.matrix([[11, 12, 13],
[21, 22, 23],
[31, 32, 33]])
Run Code Online (Sandbox Code Playgroud)
在这里,我需要在一维数组中转换矩阵,可以这样做:
indices = np.triu_indices_from(m)
a = np.asarray( m[indices] )[-1]
#array([11, 12, 13, 22, 23, 33])
Run Code Online (Sandbox Code Playgroud)
在进行大量计算后a,更改其值,它将用于填充对称的2D数组:
new = np.zeros(m.shape)
for i,j in enumerate(zip(*indices)):
new[j]=a[i]
new[j[1],j[0]]=a[i]
Run Code Online (Sandbox Code Playgroud)
返回:
array([[ 11., 12., 13.],
[ 12., 22., 23.],
[ 13., 23., 33.]])
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来实现这一目标?更具体地说,避免Python循环重建2D数组?
我对numpy.dot产品有些怀疑.
我定义了一个矩阵6x6,如:
C=np.zeros((6,6))
C[0,0], C[1,1], C[2,2] = 129.5, 129.5, 129.5
C[3,3], C[4,4], C[5,5] = 25, 25, 25
C[0,1], C[0,2] = 82, 82
C[1,0], C[1,2] = 82, 82
C[2,0], C[2,1] = 82, 82
Run Code Online (Sandbox Code Playgroud)
然后我通过使用多维数组以4级张量重新制作它
def long2short(m, n):
"""
Given two indices m and n of the stiffness tensor the function
return i the index of the Voigt matrix
i = long2short(m,n)
"""
if m == n:
i = m
elif (m == 1 and n == 2) or (m == 2 …Run Code Online (Sandbox Code Playgroud) 我经常在命令行中使用Numpy,并且始终要记住应用一些重复设置,例如设置输出格式:
np.set_printoptions(threshold=np.NaN, precision=3, suppress=True, linewidth=180)
Run Code Online (Sandbox Code Playgroud)
是否有一个针对新Python Shell或在导入过程中可以自动执行的全局Numpy配置文件?如果没有,是否有一种优雅的方法来达到这种效果?
python configuration numpy environment-variables configuration-files
我正在使用 numpy linalg 例程 lstsq 来求解方程组。我的 A 矩阵的大小为 (11046, 504),而我的 B 矩阵的大小为 (11046, 1),确定的秩为 249,因此解决的 x 数组的大约一半并不是特别有用。我想使用奇异值的 s 数组将与奇异值对应的参数的求解结果归零,但似乎 s 数组是按统计显着性递减的顺序排序的。有没有办法可以找出我的哪个 x 对应于每个奇异值 s?
我试图使用MatPlotLib简单地在Python的曲线下填充区域.
这是我的SSCCE:
import json
import pprint
import numpy as np
import matplotlib.pyplot as plt
y = [0,0,0,0,0,0,0,0,0,0,0,863,969,978,957,764,767,1009,1895,980,791]
x = np.arange(len(y))
fig2, ax2 = plt.subplots()
ax2.fill(x, y)
plt.savefig('picForWeb.png')
plt.show()
Run Code Online (Sandbox Code Playgroud)
附图显示了产生的输出.
有谁知道为什么Python没有填充x轴和曲线之间的整个区域?
我已经完成了Google和StackOverflow搜索,但找不到类似的例子.直觉上它似乎应该填满曲线下的整个区域.
我正在尝试使用numbapro编写一个简单的矩阵向量乘法:
from numbapro import cuda
from numba import *
import numpy as np
import math
from timeit import default_timer as time
n = 100
@cuda.jit('void(float32[:,:], float32[:], float32[:])')
def cu_matrix_vector(A, b, c):
y, x = cuda.grid(2)
if y < n:
c[y] = 0.0
if x < n and y < n:
for i in range(n):
c[y] += A[y, i] * b[i]
A = np.array(np.random.random((n, n)), dtype=np.float32)
B = np.array(np.random.random((n, 1)), dtype=np.float32)
C = np.empty_like(B)
s = time()
dA = cuda.to_device(A)
dB …Run Code Online (Sandbox Code Playgroud) 所以我有:
t = [0.0, 3.0, 5.0, 7.2, 10.0, 13.0, 15.0, 20.0, 25.0, 30.0, 35.0]
U = [12.5, 10.0, 7.6, 6.0, 4.4, 3.1, 2.5, 1.5, 1.0, 0.5, 0.3]
U_0 = 12.5
y = []
for number in U:
y.append(math.log(number/U_0, math.e))
(m, b) = np.polyfit(t, y, 1)
yp = np.polyval([m, b], t)
plt.plot(t, yp)
plt.show()
Run Code Online (Sandbox Code Playgroud)
因此,通过这样做,我得到线性回归拟合m=-0.1071和b=0.0347.
如何获得m值的偏差或误差?
我想要 m = -0.1071*(1+ plus/minus error)
m是k,b在y = kx + n时是n
我有一个3D数组,我需要在最后一个轴上"挤"它,这样我得到一个2D数组.我需要以下面的方式做到这一点.对于前两个维度的索引的每个值,我知道应从中获取值的第三维的索引值.
例如,我知道,如果i1 == 2和i2 == 7再i3 == 11.这意味着out[2,7] = inp[2,7,11].从前两个维度到第三个维度的映射在另一个2D阵列中给出.换句话说,我有一个数组,其中2,7我的位置11作为一个值.
所以,我的问题是如何组合这两个数组(3D和2D)来获得输出数组(2D).