将数字添加到该数组的特定数据后如何获取数组?

How to get array after adding a number to specific data of that array?

import numpy as np 
import xlrd
import xlwt

wb = xlrd.open_workbook('Scatter plot.xlsx')
workbook = xlwt.Workbook() 

sheet = workbook.add_sheet("Sheet1")

sh1 = wb.sheet_by_name('T180')
sh2=wb.sheet_by_name("T181")

x= np.array([sh1.col_values(1, start_rowx=51, end_rowx=315)])
y= np.array([sh1.col_values(2, start_rowx=51, end_rowx=315)])

x1= np.array([sh2.col_values(1, start_rowx=50, end_rowx=298)])
y1= np.array([sh2.col_values(2, start_rowx=50, end_rowx=298)])

condition = [(x1<=1000) & (x1>=0) ]
condition1 = [(y1<=1000) & (y1>=0) ]

x_prime=x1[condition]-150
y_prime= y[condition1]+20

plt.plot(x,y, "ro", label="T180")
plt.plot(x_prime,y_prime,"gs")
plt.show()

我想从 x1 数组中小于 1000 的值中减去 150,最后我需要所有值(减去+剩余)。但是通过这段代码,我只得到了小于 1000 的值。但我需要两者(小于 1000+ 大于 1000)。但大于 1000 的值将保持不变。我怎么会这样。如您所见,x1 数组中有 248 个元素,因此减法后我将需要 248 个元素,如 x_prime。与 y 相同。预先感谢您的友好合作。

您可以使用组合布尔数组的 numpy.place to modify arrays where a logic expression holds. For complex logic expressions on the array there are the logic functions

例如:

A = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
np.place(A, np.logical_and(A > 1, A <= 8), A-10)

将从 A 的每个元素中减去 10,即 > 1<= 8。在此之后 A 将是

array([ 1, -9, -8, -7, -6, -5, -4, -3,  9, 10])
import numpy as np 
#random initialization
x1=np.random.randint(1,high=3000, size=10)

x_prime=x1.tolist()

for i in range(len(x_prime)):
    if(x_prime[i]<=1000 and x_prime[i]>=0): 
        x_prime[i]=x_prime[i]-150

x_prime=np.asarray(x_prime)  

答案:

x1
Out[151]: array([2285, 2243, 1716,  632, 2489, 2837, 2324, 2154,  562, 2508])

x_prime
Out[152]: array([2285, 2243, 1716,  482, 2489, 2837, 2324, 2154,  412, 2508])

这是一个Pandas解决方案:

import matplotlib
import matplotlib.pyplot as plt
import pandas as pd

matplotlib.style.use('ggplot')

fn = r'/path/to/ExcelFile.xlsx'
sheetname = 'T181'
df = pd.read_excel(fn, sheetname=sheetname, skiprows=47, parse_cols='B:C').dropna(how='any')

# customize X-values
df.ix[df.eval('0 <= GrvX <= 1000'), 'GrvX'] -= 150
df.ix[df.eval('2500 < GrvX <= 3000'), 'GrvX'] += 50
df.ix[df.eval('3000 < GrvX'), 'GrvX'] += 30

# customize Y-values
df.ix[df.eval('0 <= GrvY <= 1000'), 'GrvX'] += 20

df.plot.scatter(x='GrvX', y='GrvY', marker='s', s=30, label=sheetname, figsize=(14,12))

plt.show()