Home > OS >  Python: graph from csv filtered by pandas shows no graph
Python: graph from csv filtered by pandas shows no graph

Time:12-27

I would like to create a graph on filtered data from a .csv file. A graph is created but without content.

Here is the code and the result:

# var 4 graph
xs = []
ys = []

name = "Anna"
gender = "F"
state = "CA"


# 4 reading csv file
import pandas as pd

# reading csv file
dataFrame = pd.read_csv("../Kursmaterialien/data/names.csv")
#print("DataFrame...\n",dataFrame)

# select rows containing text 
dataFrame = dataFrame[(dataFrame['Name'] == name)&dataFrame['State'].str.contains(state)&dataFrame['Gender'].str.contains(gender)]
#print("\nFetching rows with text ...\n",dataFrame)

print(dataFrame)

# append var with value
xs.append(list(dataFrame['Year']))
ys.append(list(dataFrame['Count']))
#xs.append(list(map(str,dataFrame['Year'])))
#ys.append(list(map(str,dataFrame['Count'])))


print(xs)
print(ys)

Result from print(xs) and print(ys) Result from print(xs) and print(ys)

%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(xs,ys)
plt.show()

Resulting plot: Resulting plot

I see that the variables start with two brackets, but don't know if that is the problem and how to fix it.

The graphic should look something like this : desired output

CodePudding user response:

You are correct about the two brackets, you have to extract the data from the inner bracket. This is done by setting the indice to 0 to get the first column (which is also the only one).

This should work:

XS=xs[0]
YS=ys[0]
plt.plot(XS,YS)
plt.show()

With your double brackets, the plt.plot is plotting each pairs of points as a different element. And plotting a point doesn't draw a line, as by default the makers are off. If you try to add markers to your plot, you will see the markers in different colours, but no lines.

plt.plot(xs,ys,'o') #round marker
  • Related