[英]How to make a pandas dataframe for-loop (for a stock market API)
[英]How can make subplots of columns in Pandas dataframe in one window inside of for-loop
*請幫助它,這非常重要:為什么不能通過在for循環內使用HeatMap獲得Pandas數據框的柱狀圖子圖?
我試圖在迭代期間在for循環內的pandas數據框中創建列的子圖,因為我在每個窗口中繪制每個480個值的結果,以使所有3個子圖在一個窗口中並排屬於A,B,C。 我在這里只找到一個答案,恐怕不是我的情況! @ euri10通過使用flat回答。
我的腳本如下:
# Import and call the needed libraries
import numpy as np
import pandas as pd
import os
import seaborn as sns
import matplotlib.pyplot as plt
'''
Take a list and create the formatted matrix
'''
def mkdf(ListOf480Numbers):
normalMatrix = np.array_split(ListOf480Numbers,8) #Take a list and create 8 array (Sections)
fixMatrix = []
for i in range(8):
lines = np.array_split(normalMatrix[i],6) #Split each section in lines (each line contains 10 cells from 0-9)
newMatrix = [0,0,0,0,0,0] #Empty array to contain reordered lines
for j in (1,3,5):
newMatrix[j] = lines[j] #lines 1,3,5 remain equal
for j in (0,2,4):
newMatrix[j] = lines[j][::-1] #lines 2,4,6 are inverted
fixMatrix.append(newMatrix) #After last update of format of table inverted (bottom-up zig-zag)
return fixMatrix
'''
Print the matrix with the required format
'''
def print_df(fixMatrix):
values = []
for i in range(6):
values.append([*fixMatrix[4][i], *fixMatrix[7][i]]) #lines form section 6 and 7 are side by side
for i in range(6):
values.append([*fixMatrix[5][i], *fixMatrix[6][i]]) #lines form section 4 and 5 are side by side
for i in range(6):
values.append([*fixMatrix[1][i], *fixMatrix[2][i]]) #lines form section 2 and 3 are side by side
for i in range(6):
values.append([*fixMatrix[0][i], *fixMatrix[3][i]]) #lines form section 0 and 1 are side by side
df = pd.DataFrame(values)
return (df)
'''
Normalizing Formula
'''
def normalize(value, min_value, max_value, min_norm, max_norm):
new_value = ((max_norm - min_norm)*((value - min_value)/(max_value - min_value))) + min_norm
return new_value
'''
Split data in three different lists A, B and C
'''
dft = pd.read_csv('D:\me4.TXT', header=None)
id_set = dft[dft.index % 4 == 0].astype('int').values
A = dft[dft.index % 4 == 1].values
B = dft[dft.index % 4 == 2].values
C = dft[dft.index % 4 == 3].values
data = {'A': A[:,0], 'B': B[:,0], 'C': C[:,0]}
#df contains all the data
df = pd.DataFrame(data, columns=['A','B','C'], index = id_set[:,0])
'''
Data generation phase
'''
#next iteration create all plots, change the number of cycles
cycles = int(len(df)/480)
print(cycles)
for i in df:
try:
os.mkdir(i)
except:
pass
min_val = df[i].min()
min_nor = -1
max_val = df[i].max()
max_nor = 1
for cycle in range(1): #iterate thriugh all cycles range(1) by ====> range(int(len(df)/480))
count = '{:04}'.format(cycle)
j = cycle * 480
ordered_data = mkdf(df.iloc[j:j+480][i])
csv = print_df(ordered_data)
#Print .csv files contains matrix of each parameters by name of cycles respectively
csv.to_csv(f'{i}/{i}{count}.csv', header=None, index=None)
if 'C' in i:
min_nor = -40
max_nor = 150
#Applying normalization for C between [-40,+150]
new_value3 = normalize(df['C'].iloc[j:j+480][i].values, min_val, max_val, -40, 150)
n_cbar_kws = {"ticks":[-40,150,-20,0,25,50,75,100,125]}
df3 = print_df(mkdf(new_value3))
else:
#Applying normalizayion for A,B between [-1,+1]
new_value1 = normalize(df['A'].iloc[j:j+480][i].values, min_val, max_val, -1, 1)
new_value2 = normalize(df['B'].iloc[j:j+480][i].values, min_val, max_val, -1, 1)
n_cbar_kws = {"ticks":[-1.0,-0.75,-0.50,-0.25,0.00,0.25,0.50,0.75,1.0]}
df1 = print_df(mkdf(new_value1))
df2 = print_df(mkdf(new_value2))
#Plotting parameters by using HeatMap
plt.figure()
sns.heatmap(df, vmin=min_nor, vmax=max_nor, cmap ='coolwarm', cbar_kws=n_cbar_kws)
plt.title(i, fontsize=12, color='black', loc='left', style='italic')
plt.axis('off')
#Print .PNG images contains HeatMap plots of each parameters by name of cycles respectively
plt.savefig(f'{i}/{i}{count}.png')
#plotting all columns ['A','B','C'] in-one-window side by side
fig, axes = plt.subplots(nrows=1, ncols=3 , figsize=(20,10))
plt.subplot(131)
sns.heatmap(df1, vmin=-1, vmax=1, cmap ="coolwarm", linewidths=.75 , linecolor='black', cbar=True , cbar_kws={"ticks":[-1.0,-0.75,-0.5,-0.25,0.00,0.25,0.5,0.75,1.0]})
fig.axes[-1].set_ylabel('[MPa]', size=20) #cbar_kws={'label': 'Celsius'}
plt.title('A', fontsize=12, color='black', loc='left', style='italic')
plt.axis('off')
plt.subplot(132)
sns.heatmap(df2, vmin=-1, vmax=1, cmap ="coolwarm", cbar=True , cbar_kws={"ticks":[-1.0,-0.75,-0.5,-0.25,0.00,0.25,0.5,0.75,1.0]})
fig.axes[-1].set_ylabel('[Mpa]', size=20) #cbar_kws={'label': 'Celsius'}
#sns.despine(left=True)
plt.title('B', fontsize=12, color='black', loc='left', style='italic')
plt.axis('off')
plt.subplot(133)
sns.heatmap(df3, vmin=-40, vmax=150, cmap ="coolwarm" , cbar=True , cbar_kws={"ticks":[-40,150,-20,0,25,50,75,100,125]})
fig.axes[-1].set_ylabel('[°C]', size=20) #cbar_kws={'label': 'Celsius'}
#sns.despine(left=True)
plt.title('C', fontsize=12, color='black', loc='left', style='italic')
plt.axis('off')
plt.suptitle(f'Analysis of data in cycle Nr.: {count}', color='yellow', backgroundcolor='black', fontsize=48, fontweight='bold')
plt.subplots_adjust(top=0.7, bottom=0.3, left=0.05, right=0.95, hspace=0.2, wspace=0.2)
#plt.subplot_tool()
plt.savefig(f'{i}/{i}{i}{count}.png')
plt.show()
到目前為止,由於無法在每個周期內輸出正確的輸出,因此無法獲得正確的輸出,例如,以不同的間隔打印3次。 它打印'A'
左然后再次將打印'A'
下的名稱'B'
和'C'
在中間和右側在一個窗口中。 再次它打印'B'
3次而不是一次,放在中間,最后它打印'C'
3次而不是一次,然后放在右邊,而在中間和左邊!
目標是在每個 for 循環中 (每個480個值乘480個值)在一個窗口中捕獲所有3列A,B和C的子圖!
第一個循環:0000 -----> A,B,C的子圖---->將其存儲為0000.png
第二個循環:A,B,C的0001 ----->子圖---->將其存儲為0001.png ...
問題是在for循環內使用df ,它傳遞A或B或C的值3次,而應該傳遞它的值分別屬於每一列一次,因此我在此處提供了輸出不成功的圖片,以便您可以准確地看到問題很明顯
我想要的輸出如下:
我公司還為3次提供的數據集的示例文本文件: 數據集
因此,在查看了您的代碼和要求之后,我想我知道問題出在哪里。 您的for
循環順序錯誤。 您需要每個循環一個新的圖形,其中包含每個“ A”,“ B”和“ C”作為子圖。
這意味着您的外循環應遍歷整個循環,然后您的內循環應遍歷i
,而循環的縮進和順序使您嘗試繪制第一個循環中已經存在的所有'A','B','C'
子圖i
( i='A'
, cycle=1
),而不是在第一個循環的第一個循環之后,所有i
( i='A','B','C'
, cycle=1
)。
這也是為什么您遇到未定義df3的問題(如對此答案的評論中所述)。 df3的定義在if塊中進行檢查'C' in i
在您的第一個循環中不檢查'C' in i
,因此不滿足此條件,因此未定義df3,但是您仍在嘗試繪制它!
同樣,您再次遇到NaN / inf值與其他問題相同的問題。
重新運行for
循環和縮進並清理NaN / inf值將獲得以下代碼:
#...
#df contains all the data
df = pd.DataFrame(data, columns=['A','B','C'], index = id_set[:,0])
df = df.replace(np.inf, np.nan)
df = df.fillna(0)
'''
Data generation phase
'''
#next iteration create all plots, change the number of cycles
cycles = int(len(df)/480)
print(cycles)
for cycle in range(cycles): #iterate thriugh all cycles range(1) by ====> range(int(len(df)/480))
count = '{:04}'.format(cycle)
j = cycle * 480
for i in df:
try:
os.mkdir(i)
except:
pass
min_val = df[i].min()
min_nor = -1
max_val = df[i].max()
max_nor = 1
ordered_data = mkdf(df.iloc[j:j+480][i])
csv = print_df(ordered_data)
#Print .csv files contains matrix of each parameters by name of cycles respectively
csv.to_csv(f'{i}/{i}{count}.csv', header=None, index=None)
if 'C' in i:
min_nor = -40
max_nor = 150
#Applying normalization for C between [-40,+150]
new_value3 = normalize(df['C'].iloc[j:j+480], min_val, max_val, -40, 150)
n_cbar_kws = {"ticks":[-40,150,-20,0,25,50,75,100,125]}
df3 = print_df(mkdf(new_value3))
else:
#Applying normalizayion for A,B between [-1,+1]
new_value1 = normalize(df['A'].iloc[j:j+480], min_val, max_val, -1, 1)
new_value2 = normalize(df['B'].iloc[j:j+480], min_val, max_val, -1, 1)
n_cbar_kws = {"ticks":[-1.0,-0.75,-0.50,-0.25,0.00,0.25,0.50,0.75,1.0]}
df1 = print_df(mkdf(new_value1))
df2 = print_df(mkdf(new_value2))
# #Plotting parameters by using HeatMap
# plt.figure()
# sns.heatmap(df, vmin=min_nor, vmax=max_nor, cmap ='coolwarm', cbar_kws=n_cbar_kws)
# plt.title(i, fontsize=12, color='black', loc='left', style='italic')
# plt.axis('off')
# #Print .PNG images contains HeatMap plots of each parameters by name of cycles respectively
# plt.savefig(f'{i}/{i}{count}.png')
#plotting all columns ['A','B','C'] in-one-window side by side
fig, axes = plt.subplots(nrows=1, ncols=3 , figsize=(20,10))
plt.subplot(131)
sns.heatmap(df1, vmin=-1, vmax=1, cmap ="coolwarm", linewidths=.75 , linecolor='black', cbar=True , cbar_kws={"ticks":[-1.0,-0.75,-0.5,-0.25,0.00,0.25,0.5,0.75,1.0]})
fig.axes[-1].set_ylabel('[MPa]', size=20) #cbar_kws={'label': 'Celsius'}
plt.title('A', fontsize=12, color='black', loc='left', style='italic')
plt.axis('off')
plt.subplot(132)
sns.heatmap(df2, vmin=-1, vmax=1, cmap ="coolwarm", cbar=True , cbar_kws={"ticks":[-1.0,-0.75,-0.5,-0.25,0.00,0.25,0.5,0.75,1.0]})
fig.axes[-1].set_ylabel('[Mpa]', size=20) #cbar_kws={'label': 'Celsius'}
#sns.despine(left=True)
plt.title('B', fontsize=12, color='black', loc='left', style='italic')
plt.axis('off')
plt.subplot(133)
sns.heatmap(df3, vmin=-40, vmax=150, cmap ="coolwarm" , cbar=True , cbar_kws={"ticks":[-40,150,-20,0,25,50,75,100,125]})
fig.axes[-1].set_ylabel('[°C]', size=20) #cbar_kws={'label': 'Celsius'}
#sns.despine(left=True)
plt.title('C', fontsize=12, color='black', loc='left', style='italic')
plt.axis('off')
plt.suptitle(f'Analysis of data in cycle Nr.: {count}', color='yellow', backgroundcolor='black', fontsize=48, fontweight='bold')
plt.subplots_adjust(top=0.7, bottom=0.3, left=0.05, right=0.95, hspace=0.2, wspace=0.2)
#plt.subplot_tool()
plt.savefig(f'{i}/{i}{i}{count}.png')
plt.show()
這將為您提供以下三個圖像以及三個單獨的數字以及您提供的數據:
一般來說,您的代碼非常混亂。 我明白了,如果您是編程的新手,並且只想分析數據,那么您可以做任何有效的事情,不管它是否漂亮。
但是,我認為凌亂的代碼意味着您無法正確查看腳本的底層邏輯,這就是解決此問題的方法。
我建議如果再次遇到類似問題,請在所有循環中寫出一些“偽代碼”,並嘗試考慮每個循環中要完成的工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.