此代码使我能够绘制“3d”数组 [X,Y,Z] 的颜色图(它们是 3 个简单的 np.array 元素)。但是我无法在颜色条图例的右侧添加垂直书写标签。
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure("Color MAP 2D+")
contour = plt.tricontourf(X, Y, Z, 100, cmap="bwr")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Color MAP 2D+")
#Legend
def fmt(x, pos):
a, b = '{:.2e}'.format(x).split('e')
b = int(b)
return r'${} \times 10^{{{}}}$'.format(a, b)
import matplotlib.ticker as ticker
plt.colorbar(contour, format=ticker.FuncFormatter(fmt))
plt.show()
Run Code Online (Sandbox Code Playgroud)
不能从谷歌得到一个简单的答案是很烦人的……有人可以帮我吗?
我想通过椭圆函数拟合二维数组:(x / a)² + (y / b)² = 1 ----> (因此得到 a 和 b)
然后,能够在我的图表上重新绘制它。我在互联网上找到了很多例子,但没有一个有这个简单的笛卡尔方程。我可能搜索得很糟糕!我认为这个问题的基本解决方案可以帮助很多人。
下面是一个数据示例:
可悲的是,我不能把这些值......所以让我们假设我有一个 X,Y 数组来定义每个点的坐标。
当我加载500Mo大的JSON文件时,Python(和spyder)返回MemoryError.
但我的电脑有一个32Go内存,当我试图加载时,spyder显示的"内存"从15%到19%!看来我应该有更多的空间......
我没想到的东西?
我有一些 x 和 y 数据,我想用它们生成带有颜色渐变(bwr 或其他)的 3D 直方图。
我编写了一个脚本,绘制了 x 和 y 脓肿的有趣值,介于 -2 和 2 之间:
import numpy as np
import numpy.random
import matplotlib.pyplot as plt
# To generate some test data
x = np.random.randn(500)
y = np.random.randn(500)
XY = np.stack((x,y),axis=-1)
def selection(XY, limitXY=[[-2,+2],[-2,+2]]):
XY_select = []
for elt in XY:
if elt[0] > limitXY[0][0] and elt[0] < limitXY[0][1] and elt[1] > limitXY[1][0] and elt[1] < limitXY[1][1]:
XY_select.append(elt)
return np.array(XY_select)
XY_select = selection(XY, limitXY=[[-2,+2],[-2,+2]])
heatmap, xedges, yedges = np.histogram2d(XY_select[:,0], …Run Code Online (Sandbox Code Playgroud) 我需要将不同形状的矩阵,M和N与有限大小的MxN相乘.
我想一个例子会更清楚:
A(形状:4x4)=
0 3 0 0
0 0 4 0
0 0 0 3
0 0 0 0
Run Code Online (Sandbox Code Playgroud)
B(形状:7x7)=
3 0 0 0 0 0 0
0 2 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 -1 0 0
0 0 0 0 0 -2 0
0 0 0 0 0 0 -3
Run Code Online (Sandbox Code Playgroud)
结果,我想要一个形状矩阵(4*7 x 4*7),这意味着(28 x 28)如下:
0 3*B 0 0
0 0 …Run Code Online (Sandbox Code Playgroud) 我想找到列表中一个元素周围的两个元素。我建议这个代码(它有效),但我很确定有一种方法可以以更简单的方式做到这一点。
a = [4,5,6,8,9,15,16,18,54,60]
b = 24
i = 0
while i<len(a):
if b > a[i-1] and b < a[i+1]:
result = [a[i-1],a[i]]
i = i+1
else:
i = i+1
Run Code Online (Sandbox Code Playgroud)
它返回给我:
>>> result
[18, 54]
Run Code Online (Sandbox Code Playgroud)
你知道python的函数吗?