2

Hi I have a data file with multiple column and I want to add a third axis with a colorbar. What I want to do is plot 1st column as y and 5th column as x axis also want to add 4th column with color on data points but I'm getting following error:

ValueError: to_rgba: Invalid rgba arg "124.12834"
to_rgb: Invalid rgb arg "124.12834"
gray (string) must be in range 0-1

my data file is as follows:

10.0    -1000.0 1300.0  2800.0  124.12834       426.450106      207.905003
10.0    -1000.0 1200.0  2000.0  127.856047      426.794984      230.104389
10.0    -500.0  1300.0  2800.0  123.074804      426.255793      205.883523
10.0    -500.0  1200.0  2000.0  127.10481       426.453441      228.650902
10.0    0.0     1300.0  2800.0  122.240729      425.953329      204.735877
10.0    0.0     1200.0  2000.0  126.435714      426.086112      227.646258
10.0    500.0   1200.0  2000.0  125.835211      425.704534      226.969429

and the code that im using is:

import numpy as np
import matplotlib.pyplot as plt

with open("test_data1.dat") as f:
    data = f.read()

data = data.split('\n')

x = [row.split('\t')[5] for row in data]
y = [row.split('\t')[1] for row in data]
z = [row.split('\t')[4] for row in data]          

cm = plt.cm.get_cmap('RdYlBu')
fig, ax = plt.subplots()
sc = ax.scatter(x, y, c=z, vmin=10, vmax=15,s=35.0,cmap=cm)
fig.colorbar(sc)
plt.show()

Note that it doesn't copy data with protecting all properties thus you can take following data this is the data string in the code:

['10.0\t-1000.0\t1300.0\t2800.0\t124.12834\t426.450106\t207.905003',
 '10.0\t-1000.0\t1200.0\t2000.0\t127.856047\t426.794984\t230.104389',
 '10.0\t-500.0\t1300.0\t2800.0\t123.074804\t426.255793\t205.883523',
 '10.0\t-500.0\t1200.0\t2000.0\t127.10481\t426.453441\t228.650902',
 '10.0\t0.0\t1300.0\t2800.0\t122.240729\t425.953329\t204.735877',
 '10.0\t0.0\t1200.0\t2000.0\t126.435714\t426.086112\t227.646258',
 '10.0\t500.0\t1200.0\t2000.0\t125.835211\t425.704534\t226.969429']

Thanks

2 Answers 2

3

I delete the '\t' in all split(), because my text editor changed the '\t' to space automatically.

string.split(s[, sep[, maxsplit]): If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed).

And it can show the diagram correctly, Here is the code:

import numpy as np
import matplotlib.pyplot as plt

data = open("test_data1.dat").read().split('\n')

x = [row.split()[5] for row in data]
y = [row.split()[1] for row in data]
z = [row.split()[4] for row in data]          

cm = plt.cm.get_cmap('RdYlBu')
fig, ax = plt.subplots()
sc = ax.scatter(x, y, c=z, vmin=10, vmax=15,s=35.0,cmap=cm)
fig.colorbar(sc)
plt.show()

Here is the diagram: diagram

That error didn't occur to me, may be you didn't split any row correctly(print it to check), or you can try to reinstall matplotlib.

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

Comments

2

You just get the values as strings not as floats. You have to use:

x = [float(row.split('\t')[5]) for row in data]
y = [float(row.split('\t')[1]) for row in data]
z = [float(row.split('\t')[4]) for row in data]

Also [1] gets you the second row, as indices start with 0 in python. Therefore maybe you get not the columns you want.

If you want to have the points coloured differently you have to adjust the range vmin=10, vmax=15, to fit your values, or leave it blank, then matplotlib automatically adjusts them.

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.