Matplotlib plotting nan values



我试图在matplotlib中绘制条形图,但不知何故它绘制了nan值。

import pandas as pd
import matplotlib.pyplot as plt
import math 

engine_data= pd.read_excel('edb-emissions-databank_v28c_web.xlsx',
sheet_name = 'nvPM Emissions')
manufacturers = engine_data.value_counts('Manufacturer').index.values

def Average(engine_type, column, manufacturer):

averages = []
for manufacturer in manufacturers:  
average = engine_data[(engine_data.iloc[:,2]== manufacturer) & (engine_data.iloc[:,5] == engine_type)][column].mean()
averages.append(average)

return averages

TF_particles = Average('TF','nvPM LTO Total Mass (mg)', manufacturers)
MTF_particles = Average('MTF','nvPM LTO Total Mass (mg)', manufacturers) 

results_TF = list(zip(manufacturers, TF_particles))
results_MTF = list(zip(manufacturers, MTF_particles))

for result in results_TF:
if math.isnan(result[1]) == True:
results_TF.remove(result)

for result in results_MTF:
if math.isnan(result[1]) == True:
results_MTF.remove(result)

fig ,ax = plt.subplots(1,2,figsize = (150,50))
for result in results_TF:
ax[0].bar(result[0], result[1], color = 'red')
for result in results_MTF:
ax[1].bar(result[0], result[1], color = 'blue')
ax[0].tick_params(axis='x', labelsize=70)
ax[0].tick_params(axis='y', labelsize=70)
ax[1].tick_params(axis='x', labelsize=30)
ax[1].tick_params(axis='y', labelsize=30)
plt.show()

我试图删除nan值,但是当我设法删除一些时,仍然有一些仍然存在。我正在绘制发动机制造商与其在LTO循环中的平均排放量之间的关系。我有两个图表,因为我把两种发动机类型分开了:TF =涡扇发动机和MTF =混合涡扇发动机。nan是由一些制造商没有任何TF引擎或相反的事实引起的,所以当我执行zip时,我得到一些nan值。

这是我得到的

您正在迭代results_TFresults_MTF,同时试图从两个列表中删除项目,因此它最终会跳过而不是捕获所有的nan值。创建新列表可能更容易,而不是修改它们,然后将其添加到你的条形图中。

# new lists
results_TF_filtered = [(m, p) for (m, p) in results_TF if pd.notnull(p)]
results_MTF_filtered = [(m, p) for (m, p) in results_MTF if pd.notnull(p)]
fig ,ax = plt.subplots(1,2,figsize = (150,50))
# plotting bar chart
ax[0].bar([m for (m, _) in results_TF_filtered], [p for (_, p) in results_TF_filtered], color = 'red')
ax[1].bar([m for (m, _) in results_MTF_filtered], [p for (_, p) in results_MTF_filtered], color = 'blue')

更新代码:

import pandas as pd
import matplotlib.pyplot as plt

engine_data= pd.read_excel('edb-emissions-databank_v28c_web.xlsx',
sheet_name = 'nvPM Emissions')
manufacturers = engine_data.value_counts('Manufacturer').index.values

def Average(engine_type, column, manufacturer):

averages = []
for manufacturer in manufacturers:  
average = engine_data[(engine_data.iloc[:,2]== manufacturer) & (engine_data.iloc[:,5] == engine_type)][column].mean()
averages.append(average)

return averages

TF_particles = Average('TF','nvPM LTO Total Mass (mg)', manufacturers)
MTF_particles = Average('MTF','nvPM LTO Total Mass (mg)', manufacturers) 

results_TF = list(zip(manufacturers, TF_particles))
results_MTF = list(zip(manufacturers, MTF_particles))
# new lists
results_TF_filtered = [(m, p) for (m, p) in results_TF if pd.notnull(p)]
results_MTF_filtered = [(m, p) for (m, p) in results_MTF if pd.notnull(p)]
fig ,ax = plt.subplots(1,2,figsize = (150,50))
# plotting bar chart
ax[0].bar([m for (m, _) in results_TF_filtered], [p for (_, p) in results_TF_filtered], color = 'red')
ax[1].bar([m for (m, _) in results_MTF_filtered], [p for (_, p) in results_MTF_filtered], color = 'blue')
ax[0].tick_params(axis='x', labelsize=70)
ax[0].tick_params(axis='y', labelsize=70)
ax[1].tick_params(axis='x', labelsize=30)
ax[1].tick_params(axis='y', labelsize=30)
plt.show()

相关内容

  • 没有找到相关文章

最新更新