2

I've got java code to call a python script

Java

private void visualizeData() {
    try{
        Runtime.getRuntime().exec(“python pyScripts/visualize.py”)
    } catch (IOException e){
        e.printStackTrace();
    }
}

And this is my code for the visualize.py:

visualize.py

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pylot as plt

f = open(“ecoli_data_transformed.txt”,”r”)

fig = plt.figure()
ax = fig.add_subplot(111, projection=‘3d’)

for line in f:
    (a, b, c) = line.split(“\t”)
    a = float(a)
    b = float(b)
    c = float(c)    

    ax.scatter(a,b,c)

ax.setxlabel(‘PCA1’)
ax.setylabel(‘PCA2’)
ax.setzlabel(‘PCA3’)

plt.show()

But it doesn't plot the data.

If I call the test.py script from the java code (test.py is in the same directory as visualize.py), it works:

test.py

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pylot as plt
import numpy as np

np.random.seed(19680801)

def randrange(n, min, vmcx):
    return (vmax - vein)*np.random.rand(n) + vmin

fig = plt.figure()
ax = fig.add_subplot(111, projection=‘3d’)

n = 100

for c, m, slow, high in [(‘r’, ‘o’, -50, -25), (‘b’, ‘^’, -30, -5)]:
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zlow, zhigh)
    ax.scatter(xs, ys, zs, c=c, marker=m)

ax.set_xlabel(‘X Label’)
ax.set_ylabel(‘Y Label’)
ax.set_zlabel(‘Z Label’)

plt.show()

What could be the problem?

*NOTE: Calling the visualize.py script from console by 'python visualize.py' works totally fine.

5
  • 2
    please paste the code in the question instead of attaching links of images. Commented Jan 4, 2018 at 14:11
  • I tried but it didn't format well. Sorry, I'm a newbie. Commented Jan 4, 2018 at 14:12
  • To format as code, you should substitute your tabs for spaces in your editor, paste the code here, select it and use the code format tool available (which basically adds 4 spaces per line of code) Commented Jan 4, 2018 at 14:29
  • And what happens? Any exceptions? Commented Jan 4, 2018 at 15:01
  • No, nothing. The code finishes, but the script's graph is never plotted. Not the case for test.py, everything works fine there. Commented Jan 4, 2018 at 15:25

1 Answer 1

1

The relative path that you are passing to the open() command in your python script is probably the problem.

Your Java program is located in a different directory than the one your Python program is located in. When it starts the python script, the current path is still the path to the Java program.

This means that Python is unable to find the relative path to the file you are trying to open, ecoli_data_transformed.txt.

A work around would be to include the complete path to your .txt file:

f = open(“C:\\path\\to\\your\\file\\ecoli_data_transformed.txt”,”r”)

A better solution would be to determine it programatically:

import os

file_path = os.path.dirname(__file__)
f = open(file_path + "\\ecoli_data_transformed.txt”,”r”)
Sign up to request clarification or add additional context in comments.

1 Comment

OH MY GOD YES, THANK YOU! <3

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.