Home > other >  Setting condition on a column values while plotting other columns
Setting condition on a column values while plotting other columns

Time:12-08

I have a question about plotting from a text data file that contains three columns (20000 rows). I would like to plot column 2 and 3 (or Histogram of column 2). However, I would like my plot to be for only a range of datas from column one from 100-250 values.

Note: One way maybe by sorting the data accoriding to column one, which I dont know how.

The sample of data is

174.2227   0.1624629285511385E 03  -0.6292327918805374E 02
 96.5364   0.9382981565234142E 02  -0.2269888520085278E 02
170.4995   0.1255471456652923E 03  -0.1153603193263530E 03
 70.3605   0.5622579821326531E 02  -0.4229968593987883E 02
 70.3641   0.1705414793985607E 02  -0.6826609764576108E 02
245.6546   0.1009630870343540E 03  -0.2239478772161106E 03
247.0803   0.2428952541481390E 03  -0.4528334882548071E 02
240.4885   0.1898105937624483E 03  -0.1476708453344265E 03
190.4206   0.2201049326187159E 01  -0.1904078537576801E 03
 58.0858   0.2315296872737939E 02  -0.5327192955482575E 02
263.4021   0.2480699465562589E 03  -0.8855483744759709E 02
 52.9697   0.1776581942067039E 02  -0.4990154780891378E 02
135.9583   0.1774572342000289E 02  -0.1347952056648868E 03
 79.8317   0.5762263417747670E 02  -0.5525152449053701E 02
155.5004   0.1506111928119825E 03  -0.3868642911295389E 02

I have tried the following code


import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np


file1 = "data1.txt"

a1 = np.loadtxt(file1,usecols=[0])
b1 = np.loadtxt(file1,usecols=[1])
c1 = np.loadtxt(file1,usecols=[2])

while 100 < a1 < 200:

   plt.plot(b1,c1,'k.')

plt.show()

CodePudding user response:

Use pandas to read your file and filtering.

import pandas as pd
df = pd.read_csv("your_file.tsv", sep='\t', names=['col1', 'col2', 'col3'])
df_filtered = df[df['col1'].between(100, 250)]
df_filtered.plot( x='col1', y='col2', kind='hist')

CodePudding user response:

your while condition kept throwing errors. I would use pandas (like @cucurbit said)

import pandas as pd
import matplotlib.pyplot as plt

x = [[174.2227,   0.1624629285511385E 03,  -0.6292327918805374E 02],
[ 96.5364,   0.9382981565234142E 02,  -0.2269888520085278E 02],
[170.4995,   0.1255471456652923E 03,  -0.1153603193263530E 03],
[ 70.3605,   0.5622579821326531E 02,  -0.4229968593987883E 02],
[ 70.3641,   0.1705414793985607E 02,  -0.6826609764576108E 02],
[245.6546,   0.1009630870343540E 03,  -0.2239478772161106E 03],
[247.0803,   0.2428952541481390E 03,  -0.4528334882548071E 02],
[240.4885,   0.1898105937624483E 03,  -0.1476708453344265E 03],
[190.4206,   0.2201049326187159E 01,  -0.1904078537576801E 03],
[ 58.0858,   0.2315296872737939E 02,  -0.5327192955482575E 02],
[263.4021,   0.2480699465562589E 03,  -0.8855483744759709E 02],
[ 52.9697,   0.1776581942067039E 02,  -0.4990154780891378E 02],
[135.9583,   0.1774572342000289E 02,  -0.1347952056648868E 03],
[ 79.8317,   0.5762263417747670E 02,  -0.5525152449053701E 02],
[155.5004,   0.1506111928119825E 03,  -0.3868642911295389E 02],]

df = pd.DataFrame(x)
df.columns = ['a1','b1','c1']
tmp = df.loc[df.a1.apply(lambda x: x > 100 and x < 200)]
plt.hist(tmp)
plt.savefig('tmp')

enter image description here

  • Related