5

I've three columns in a text file, the first 2 columns represent the x-axis and y-axis positions, and I am trying to plot the third column at x and y-axis. The third column consists of 0-5 numbers.

I've tried the following code, but it doesn't give me the required result.

import numpy as np
import matplotlib.pyplot as plt

x, y, z = np.loadtxt('test.txt', delimiter=' ', unpack=True)
plt.plot(x, '.', y, '.', z)

plt.xlabel('Distance in meters')
plt.ylabel('Distance in meters')
plt.title('distribution of values')
plt.legend()
plt.show()

Here is the sample txt file.

2411.02 3310.21 3
-1246.65 -3098.39 4
166.298 6042.84 0
----
-51.7214 -2374.09 5
7285.08 661.326 0
-1390.51 4438.9 2
-2741.8 466.014 5
0 0 0

Figure shows the actual plot I got from the below code. However, the 3rd value should be plotted in a circular form based on the x and y-axis positions.

This is the output figure:

enter image description here

Solved with help of these resources: enter link description here and enter link description here

Code:

x, y, z = np.loadtxt('test.txt', delimiter=' ', unpack=True)    
plt.scatter(x, y, c=z)
plt.show()

Solved

a4arshad
  • 606
  • 1
  • 7
  • 18
  • 1
    you can do this with plt.scatter, https://matplotlib.org/api/_as_gen/matplotlib.pyplot.scatter.html , see an example at https://rrighart.github.io/City/#q18 under section 18. – Ruthger Righart Apr 04 '19 at 07:30
  • Possible duplicate of [pyplot scatter plot marker size](https://stackoverflow.com/questions/14827650/pyplot-scatter-plot-marker-size) – Georgy Apr 04 '19 at 08:01
  • Thanks for pointing out the problem nature, now the plot seems scattered in a circular fashion. But, the size of point gets smaller as it goes far away from the center. But still need help in defining the color for each point contained in z. because z has 0-5 values (suppose these are the rewards I get for a particular node at position x, y). I should have 6 different colors for the z values. – a4arshad Apr 04 '19 at 09:06
  • Do you mean this [Setting different color for each series in scatter plot on matplotlib](https://stackoverflow.com/questions/12236566/setting-different-color-for-each-series-in-scatter-plot-on-matplotlib)? – Georgy Apr 04 '19 at 11:52
  • It worked! Thanks :) – a4arshad Apr 04 '19 at 13:47

0 Answers0