1
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()

I want to subtract 150 from the values less than 1000 of x1 array and finally I need all values (subtracted+remaining). But with this code I got only the values that are less than 1000. But I need both (less than 1000+ greater than 1000). But greater than 1000 values will be unchanged. How can I will do this. As you can see there 248 elements in x1 array so after subtraction I will need 248 element as x_prime. Same as for y. Thanks in advance for your kind co-operation.

4
  • can you upload your Excel file somewhere and post here a link? It would help us to code and test solutions... Commented Sep 22, 2016 at 8:29
  • I cannot upload it, I did not find anyway to upload this file. What can I do? Thanks for your kind concern.@MaxU Commented Sep 22, 2016 at 8:34
  • google.com/… Commented Sep 22, 2016 at 8:35
  • filedropper.com/scatterplot @MaxU Commented Sep 22, 2016 at 8:37

3 Answers 3

1

Here is a Pandas solution:

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()

enter image description here

Sign up to request clarification or add additional context in comments.

11 Comments

Thank you bro. It shows error in fn. What to do? @MaxU
@QuaziNizam, sorry, fn - is a filename (including a path) - it's got lost. I've corrected my answer...
Thanks. One more problem, I want figure like this filedropper.com/figure2_1 , but according to your code I found like this: filedropper.com/figure3 How to solve?@MaxU
Thanks. Its working now. If I want more condition like -150 for <1000, and add 50 for values >2500,add 30 for >3000 of x1 (same as y1) what can I do? @MaxU
@QuaziNizam, you can do it similar to df.ix[df.eval('0 <= GrvX <= 1000'), 'GrvX'] -= 150...
|
1

You can use numpy.place to modify arrays where a logic expression holds. For complex logic expressions on the array there are the logic functions that combines boolean arrays.

E.g.:

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)

will subtract 10 from every element of A that is > 1 and <= 8. After this A will be

array([ 1, -9, -8, -7, -6, -5, -4, -3,  9, 10])

5 Comments

"ValueError: object too deep for desired array" it shows this kind of error for the following code: z=np.place(x1, np.logical_and(x1 > 1, x1 <= 1000), x1-150) print z @eistaa
@QuaziNizam I think the error comes because your x1 is multidimensional. When you construct x, y, x1, and y1 you have [ ] brackets around the col_values expression, this makes a list containing a slice, while you only want the slice. Try removing those brackets.
Thanks. Its working now. If I want more condition like -150 for <1000, and add 50 for values >25000,add 30 for >3000 of x1 (same as y1) what can I do?
@QuaziNizam use multiple place statements
Sorry I cannot understand. Would you please just show me the code as example. Thanks for your kind help. @eistaa
0
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)  

Answer:

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])

2 Comments

Thanks, but it shows same result for x1 and x_prime. @ Sayali Sonawane
you will get same results, if you won't convert it into list first. This is why, I have converted it into list first. Then perform operations and convert back to array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.