我正在同一页面上绘制三个子图.我想在所有子图中绘制一条horiZontal线.以下是我的代码和结果图:(您可以注意到我可以在其中一个图上获得水平线,但不是全部)
gs1 = gridspec.GridSpec(8, 2)
gs1.update(left=0.12, right=.94, wspace=0.12)
ax1 = plt.subplot(gs1[0:2, :])
ax2 = plt.subplot(gs1[3:5, :], sharey=ax1)
ax3 = plt.subplot(gs1[6:8, :], sharey=ax1)
ax1.scatter(theta_cord, density, c = 'r', marker= '1')
ax2.scatter(phi_cord, density, c = 'r', marker= '1')
ax3.scatter(r_cord, density, c = 'r', marker= '1')
ax1.set_xlabel('Theta (radians)')
ax1.set_ylabel('Galaxy count')
ax2.set_xlabel('Phi (radians)')
ax2.set_ylabel('Galaxy count')
ax3.set_xlabel('Distance (Mpc)')
ax3.set_ylabel('Galaxy count')
plt.ylim((0,0.004))
loc = plticker.MultipleLocator(base=0.001)
ax1.yaxis.set_major_locator(loc)
plt.axhline(y=0.002, xmin=0, xmax=1, hold=None)
plt.show()
Run Code Online (Sandbox Code Playgroud)
这会生成以下内容:

同样,我希望我在最后一个子图上绘制的线也出现在前两个子图上.我怎么做?
我想从一个大约一百万个条目的numpy数组中删除一些条目.
这段代码可以做到,但需要很长时间:
a = np.array([1,45,23,23,1234,3432,-1232,-34,233])
for element in a:
if element<(-100) or element>100:
some delete command.
Run Code Online (Sandbox Code Playgroud)
我可以用其他方式做到这一点吗?
当绘制为直方图时,我有高斯形式的数据.我想在直方图上绘制一条高斯曲线,看看数据有多好.我正在使用matplotlib的pyplot.另外,我不想标准化直方图.我可以做标准拟合,但我正在寻找一个非标准化的拟合.这里有人知道怎么做吗?
谢谢!阿比纳夫库马尔
我想绘制带有点和误差线的直方图。我不需要条形图或阶梯直方图。这可能吗?谷歌没有帮助我,我希望你能。它也不应该被标准化。谢谢!
我有一份清单.它非常大.它有超过100万条目.我想计算每个字符串的频率.它将数字存储为1到1000之间的字符串.我使用了以下内容,但它会持续运行数小时:
d = {b:a.count(b) for b in a}
n, m = d.keys(), d.values()
print n, m
Run Code Online (Sandbox Code Playgroud) 我有6个列表,分别存储两组位置的x,y,z坐标(每组3个列表)。我想计算两个集合中每个点之间的距离。我已经编写了自己的距离函数,但是它很慢。我的清单之一有大约一百万个条目。我试过了cdist,但是它产生一个距离矩阵,我不明白它的意思。是否有另一个内置函数可以做到这一点?
如何缩短以下代码?这是一个简单的代码,可以查看角度值并确保它们不会超出范围.问题在于,虽然这可以完成这项工作,但我真的想要一些更加pythonic,更容易使用并且编写起来不那么麻烦的东西.
theta_min_lim = 0.0
theta_max_lim = 180.0
if theta_min<theta_min_lim:
new_theta_min = theta_max_lim-abs(theta_min)
theta_row_index_along_new = np.array(np.where(sph_pos_count[:,1]>=new_theta_min)).flatten()
theta_row_index_along = np.concatenate((theta_row_index_along_new, theta_row_index_along))
if theta_max>theta_max_lim:
new_theta_max = theta_max-theta_max_lim
theta_row_index_along_new = np.array(np.where(sph_pos_count[:,1]<=new_theta_max)).flatten()
theta_row_index_along = np.concatenate((theta_row_index_along_new, theta_row_index_along))
if phi_min<phi_min_lim:
new_phi_min = phi_max_lim-abs(phi_min)
phi_row_index_along_new = np.array(np.where(sph_pos_count[:,2]>=new_phi_min)).flatten()
phi_row_index_along = np.concatenate((phi_row_index_along_new, phi_row_index_along))
if phi_max>phi_max_lim:
new_phi_max = phi_max-phi_max_lim
phi_row_index_along_new = np.array(np.where(sph_pos_count[:,2]<=new_phi_max)).flatten()
phi_row_index_along = np.concatenate((phi_row_index_along_new, phi_row_index_along))
#theta_row_index_along = np.concatenate(theta_row_index_along_new, theta_row_index_along)
row_index_along = np.intersect1d(theta_row_index_along,phi_row_index_along)
sph_pos_count_along = sph_pos_count[row_index_along]
#GIving range for theta and phi in the direction along the velocity
theta_min_opp = vtheta_opp …Run Code Online (Sandbox Code Playgroud) 我有一个包含以下数据的文本文件:
5298 10036 4 360 8
6128 11947 2 385 7
9472 18930 0 233 4
5056 9790 1 293 6
Run Code Online (Sandbox Code Playgroud)
我使用以下代码读取此文件:
file1 = open("test.txt","r")
lines = file1.readlines()
BF=[map(float, line.split()) for line in lines]
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
could not convert string to float: ÿþ5
Run Code Online (Sandbox Code Playgroud)
为什么我看到这个错误?
更新:
print lines
Run Code Online (Sandbox Code Playgroud)
说明:
['\xff\xfe5\x002\x009\x008\x00\t\x001\x000\x000\x003\x006\x00\t\x004\x00\t\x003\x006\x000\x00\t\x008\x00\r\x00\n', '\x006\x001\x002\x008\x00\t\x001\x001\x009\x004\x007\x00\t\x002\x00\t\x003\x008\x005\x00\t\x007\x00\r\x00\n', '\x009\x004\x007\x002\x00\t\x001\x008\x009\x003\x000\x00\t\x000\x00\t\x002\x003\x003\x00\t\x004\x00\r\x00\n', '\x005\x000\x005\x006\x00\t\x009\x007\x009\x000\x00\t\x001\x00\t\x002\x009\x003\x00\t\x006\x00\r\x00\n', '\x001\x005\x000\x006\x004\x00\t\x003\x000\x001\x006\x000\x00\t\x001\x00\t\x003\x001\x002\x00\t\x008\x00']
Run Code Online (Sandbox Code Playgroud) 我有以下嵌套循环。但在时间上效率低下。所以使用发电机会好得多。你知道怎么做吗?
x_sph[:] = [r*sin_t*cos_p for cos_p in cos_phi for sin_t in sin_theta for r in p]
Run Code Online (Sandbox Code Playgroud)
似乎你们中的一些人认为(查看评论)在这种情况下使用生成器没有帮助。我的印象是使用生成器将避免将变量分配给内存,从而节省内存和时间。我错了吗?
我有一个数字列表,如:
a = [2,4,5,12]
Run Code Online (Sandbox Code Playgroud)
我想通过从任何大于10的元素中减去10来更改列表.
我可以做到以下几点:
i = 0
for ax in a:
if ax>10:
ax = ax-10
a[i] = ax
i = i+1
Run Code Online (Sandbox Code Playgroud)
但这不是一种"pythonic"编码方式.如果我使用Fortran,那么我会很好用这个循环,但Python比这些结构化循环更好.
我可以用另一种方式吗?
您好,我有数据集(麦克斯韦和高斯),我用它们绘制直方图。我使用 scipy.stats.chisquare 拟合数据,但默认情况下,自由度为 0。如果我理解正确,这是不可能的,对吗?
我有一个 C 代码,其中定义了一些使用命令进行预处理的变量#define。
但是,我认为有一些方法可以在 float 和 double 数据类型之间切换,例如:
#define 1.0f // float data type
Run Code Online (Sandbox Code Playgroud)
或者
#define 1.0 // double data type
Run Code Online (Sandbox Code Playgroud)
有没有办法使用它来设置长双精度数据类型的变量?
python ×11
list ×3
matplotlib ×3
numpy ×3
histogram ×2
arrays ×1
c ×1
count ×1
data-fitting ×1
distance ×1
file-io ×1
frequency ×1
function ×1
gaussian ×1
generator ×1
iteration ×1
line-plot ×1
long-double ×1
loops ×1
nested-loops ×1
point ×1
python-2.7 ×1
readlines ×1
replace ×1
scatter-plot ×1
scipy ×1
statistics ×1
string ×1
subplot ×1