1

I am able to make graph an X Y graph when extracting data from a text file, but I just need help highlighting one point on the function.

I have this list of data where the first column is the y values and the second is the x values. I would like to just highlight the value (0.718, 1.42676) when plotting this data from a text file.

1.3822;0.2
1.43985;0.3
1.45821;0.4
1.45764;0.5
1.4469;0.6
1.43022;0.7
1.42676;0.718
1.4101;0.8
1.38796;0.9
1.3647;1.0

import matplotlib.pyplot as plt

data_file = open('new3b.txt','r')
lines = data_file.readlines()
data_file.close()
kinf_list = []
den_list = []
for i in range(len(lines)):
    lines[i] = lines[i].strip('\n')
    line_list = lines[i].split(';')
    kinf_list.append(float(line_list[0]))
    den_list.append(float(line_list[1]))

X = den_list
Y = kinf_list

plt.plot(X,Y)
plt.xlabel('Density (g/cc)')
plt.ylabel('K-INF')
plt.title('Multiplication factor as a function of Density')
plt.plot(X[1:], Y[1:], 'ro')
plt.plot(X[0], Y[0], 'g*')
plt.grid()
plt.savefig('kinfVdenB2.png')
plt.show()

The code above works perfect, the only problem is that it does not highlight the desired point on the function.

1
  • The other answer is more generalize-able as it isn't dependent on the order of the data points. Commented Sep 10, 2019 at 19:09

2 Answers 2

0

It looks like you used the method from this answer: using Matplotlib how to highlight one point in the final plot. In this case you can just break things up in a similar manner:

plt.plot(X,Y)
plt.xlabel('Density (g/cc)')
plt.ylabel('K-INF')
plt.title('Multiplication factor as a function of Density')
plt.plot(X[0:6], Y[0:6], 'ro')
plt.plot(X[6], Y[6], 'g*')
plt.plot(X[7:], Y[7:], 'ro')
plt.grid()
plt.savefig('kinfVdenB2.png')

Hope this helps.

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

Comments

0

It's not highlighting the right point because you're giving it the wrong positions in:

plt.plot(X[1:], Y[1:], 'ro')
plt.plot(X[0], Y[0], 'g*')

A more foolproof way is to do this:

import matplotlib.pyplot as plt
import numpy as np

data_file = open('new3b.txt','r')
lines = data_file.readlines()
data_file.close()
kinf_list = []
den_list = []
for i in range(len(lines)):
    lines[i] = lines[i].strip('\n')
    line_list = lines[i].split(';')
    kinf_list.append(float(line_list[0]))
    den_list.append(float(line_list[1]))

X = np.array(den_list)
Y = np.array(kinf_list)

plt.plot(X,Y)
plt.xlabel('Density (g/cc)')
plt.ylabel('K-INF')
plt.title('Multiplication factor as a function of Density')

HighlitghtX,HighlitghtY = 0.718, 1.42676
NotX = X[np.where(X != HighlitghtX)]
NotY = Y[np.where(Y != HighlitghtY)]
YesX = X[np.where(X == HighlitghtX)]
YesY = Y[np.where(Y == HighlitghtY)]

plt.scatter(NotX, NotY, color='r',marker='o',s=50)
plt.scatter(YesX, YesY, color='g',marker='*',s=50)
plt.grid()
plt.savefig('kinfVdenB2.png')
plt.show()

enter image description here

If you want to highlight multiple, you could do this:

plt.plot(X,Y)
plt.xlabel('Density (g/cc)')
plt.ylabel('K-INF')
plt.title('Multiplication factor as a function of Density')


HighlitghtX,HighlitghtY = np.array([0.718,0.8]), np.array([1.42676,1.4101])
NotX = X[np.isin(X,HighlitghtX)]
NotY = Y[np.isin(Y,HighlitghtY)]
YesX = X[np.invert(np.isin(X,HighlitghtX))]
YesY = Y[np.invert(np.isin(Y,HighlitghtY))]

print(NotX,YesX)

plt.scatter(NotX, NotY, color='r',marker='o',s=50)
plt.scatter(YesX, YesY, color='g',marker='*',s=50)
plt.grid()
plt.savefig('kinfVdenB2.png')
plt.show()

enter image description here

Comments

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.