我想使用 matplotlib 绘制 n 个统一根,每个根都作为不同颜色的箭头。
它应该看起来像一个星形,箭头等距向外指向单位圆。
matplotlib 有一个绘制箭头的函数,但是有没有办法使用复数来做到这一点,或者我是否必须转换为真正的笛卡尔函数?
此外,是否存在一系列库存颜色,以便无论我希望显示多少根,它都会为我提供一系列不同的颜色?(而不是说七个几乎相同的红色阴影)
让v是行向量(1 x n矩阵)和M是一个n x m矩阵.我使用下面的代码来创建一个"加权向量"(我希望这些评论可以解释它应该做什么):
weighted_M = bsxfun(@times,v',M);
%creates a matrix with the i-th row of M being weighted (multiplied) by the i-th element of v
weighted_v = sum(weighted_M);
%sums the columns of weighted_M
Run Code Online (Sandbox Code Playgroud)
现在问题是:我必须对很多输入向量进行相同的计算v.因此,我想输入一个V包含矢量v作为行的矩阵,并输出一个包含加权矢量作为行的矩阵.有没有办法在不使用for循环的情况下执行此操作?
当我使用python的matplotlib绘制一个3d图时,我遇到了问题.使用以下python函数,我得到了这个数字:
在这里X,Y有孔栅格和Z和Z_的功能X和Y.C代表表面颜色.
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
def plot(X, Y, Z, Z_, C):
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(
X, Y, Z, rstride=1, cstride=1,
facecolors=cm.jet(C),
linewidth=0, antialiased=False, shade=False)
surf_ = ax.plot_surface(
X, Y, Z_, rstride=1, cstride=1,
facecolors=cm.jet(C),
linewidth=0, antialiased=False, shade=False)
ax.view_init(elev=7,azim=45)
plt.show()
Run Code Online (Sandbox Code Playgroud)
但现在我想水平切割这个数字,只剩下z在-1和2之间的部分.
用gnuplot绘制的我想要的是:
我曾尝试ax.set_zlim3d和ax.set_zlim,但他们都没有给我想要的数字.有人知道如何使用python做到这一点吗?
我是numpy的新手,我有一个2D数组的对象,我需要将它们分成一个较小的矩阵,然后计算每个bin中的对象数量来制作热图.我按照这个线程的答案来创建垃圾箱并对一个简单的数组进行计数,但我不知道如何将它扩展到2维.这是我到目前为止所拥有的:
data_matrix = numpy.ndarray((500,500),dtype=float)
# fill array with values.
bins = numpy.linspace(0,50,50)
digitized = numpy.digitize(data_matrix, bins)
binned_data = numpy.ndarray((50,50))
for i in range(0,len(bins)):
for j in range(0,len(bins)):
k = len(data_matrix[digitized == i:digitized == j]) # <-not does not work
binned_data[i:j] = k
Run Code Online (Sandbox Code Playgroud)
PS [digitized == i]数组上的表示法将返回二进制值数组.我无法在任何地方找到有关此符号的文档.一个链接将不胜感激.
I have a VTK file (unstructured grid) with points and cells.
I can import the file and read it into using the meshio python package.
If I type the command mesh.cells I see a dictionary called 'hexahedron' with an array made up of lists inside like this:
{'hexahedron': array([[ 0, 162, 185, ..., 163, 186, 23],
[162, 329, 351, ..., 330, 352, 186],
[329, 491, 514, ..., 492, 515, 352],
...,
[483, 583, 600, ..., 584, 601, 490],
[583, …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以matplotlib用箭头刻度绘制曲线.
就像是:
from pylab import *
y = linspace(0,10,0.01)
x = cos(y)
plot(x, y, '->')
Run Code Online (Sandbox Code Playgroud)
--->---->---->当x增加时,应该得到这样的曲线,当这个曲线---<----<----<减小时(当然也是y).编辑:此外,箭头应该在曲线的方向倾斜(例如,y = x函数为45度)
我正在阅读Matlab用户的优化工具箱指南.在第1-15页中,提供了一些用于创建索引变量的代码.这是代码:
%Combine variables into one vector
variables = {'I1','I2','HE1','HE2','LE1','LE2','C','BF1',...
'BF2','HPS','MPS','LPS','P1','P2','PP','EP'};
N = length(variables);
% create variables for indexing
for v = 1:N
eval([variables{v},' = ',num2str(v),';']); %?
end
Run Code Online (Sandbox Code Playgroud)
我知道"变量"类是单元格数组.但我无法理解"eval"的功能.要阅读以下代码,它似乎为变量中的元素创建索引,以便元素可以用作操作矩阵或向量的索引号.例如:
lb = zeros(size(variables));
lb([P1,P2,MPS,LPS]) = [2500,3000,271536,100623];
Run Code Online (Sandbox Code Playgroud)
我已经阅读了帮助文档,但仍然无法获得它.所以,任何人都可以更清楚地向我解释.
顺便说一句,用户指南建议避免这种"评估"功能.那么,还有其他方法可以实现上述功能吗?
谢谢大家
完整的计划
% Combine variables into one vector
variables = {'I1','I2','HE1','HE2','LE1','LE2','C','BF1',...
'BF2','HPS','MPS','LPS','P1','P2','PP','EP'};
N = length(variables);
% create variables for indexing
for v = 1:N
eval([variables{v},' = ',num2str(v),';']); %?
end
% Write bound constraints
lb = zeros(size(variables));
lb([P1,P2,MPS,LPS]) = ...
[2500,3000,271536,100623];
ub = Inf(size(variables));
ub([P1,P2,I1,I2,C,LE2]) …Run Code Online (Sandbox Code Playgroud) 我有带有colorbar的散点图,我将其另存为PNG图像。我需要该图具有一定的figsize,但要添加颜色条缩放原始图。
import pylab as plt
plt.figure(figsize=FIGSIZE)
plt.scatter(X, Y, c=Z, s=marker_size, norm=LogNorm(), vmin=VMIN, vmax=VMAX, cmap=CMAP,rasterized=True,lw=0,)
CB = plt.colorbar(ticks=TICKS, format=FORMAT)
Run Code Online (Sandbox Code Playgroud)
如何将原始图(按上述设置figsize)和颜色栏保存为两个单独的图像?
我有一个邻接矩阵A 和一个定义每个节点坐标的数组:
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
%matplotlib inline
Import adjacency matrix A[i,j]
A = np.matrix([[0, 1, 1, 0, 0, 1, 0],
[0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0]])
## Import node coordinates
xy = np.array([[0, …Run Code Online (Sandbox Code Playgroud) 鉴于下面的python Idle中的代码段片段,为什么sys.getrefcount(a)返回4?
在python idle中执行时,下面的代码段给出了
import sys
a = []
b = a
sys.getrefcount(a) # returns 3
a
sys.getrefcount(a) #returns 4
print(a)
sys.getrefcount(a) #returns 3
Run Code Online (Sandbox Code Playgroud)
谁能解释为什么引用计数增加到4?