vendredi 1 novembre 2019

How do I use if statement with numpy array? [duplicate]

I'm trying to plot two subplots as follows:

First code:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

data_csv = pd.read_csv('DATA_TCC.csv')
print(list(data_csv))#to get csv file headings

loc = data_csv['LOCAL'][:]
date = data_csv['DATA'][:]
sal = data_csv['S'][:]
at = data_csv['AT'][:]


ax1 = plt.subplot2grid((2,1), (0,0), rowspan=1, colspan=1)
ax2 = plt.subplot2grid((2,1), (1,0), rowspan=1, colspan=1)

plt.sca(ax1)
if loc == 'Porto do Recife' and date == '5/14/14':
    plt.plot(sal, at, marker='.', color='k', label='CE - Nov/14', 
linestyle='none')

plt.sca(ax2)
if loc == 'Porto do Recife' and date == '11/6/14':
    plt.plot(sal, at, marker='.', color='k', label='CE - Nov/14', 
linestyle='none')

plt.tight_layout()
plt.show()

Second code:

data_csv = pd.read_csv('DATA_TCC.csv')
data = pd.DataFrame.to_numpy(data_csv)#to convert pandas series into 
np arrays

print(list(data_csv))#to get csv file headings

loc = data[:,0]
date = data[:,3]
sal = data[:,10]
at = data[:,21]


ax1 = plt.subplot2grid((2,1), (0,0), rowspan=1, colspan=1)
ax2 = plt.subplot2grid((2,1), (1,0), rowspan=1, colspan=1)

plt.sca(ax1)
if loc == 'Porto do Recife' and date == '5/14/14':
    plt.plot(sal, at, marker='.', color='k', label='CE - Nov/14', 
linestyle='none')

plt.sca(ax2)
if loc == 'Porto do Recife' and date == '11/6/14':
    plt.plot(sal, at, marker='.', color='k', label='CE - Nov/14', 
linestyle='none')

plt.tight_layout()
plt.show()

After getting this error for the first code:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I tried to convert my cdv file to numpy arrays with the seccond code, and got this error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Clearly I'm missing something about if statements. How could I make both codes run nicely?

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire