我只是需要帮助,不使用python在matplotlib的饼图上按照标题绘制'零'值。
我尝试过使用Not plotting 'zero' in matplotlib or change zero to None [Python]中来自不同帖子的解决方案,但我一直无法将float NaN转换为整数错误。
来自其他帖子的解决方案:
values = [3, 5, 0, 3, 5, 1, 4, 0, 9]
def zero_to_nan(values):
"""Replace every 0 with 'nan' and return a copy."""
return [float('nan') if x==0 else x for x in values]
print(zero_to_nan(values))
这是我的代码的简化版本:
#For plotting and visualization
from IPython.display import display
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
#Possible solution - however i get error cannot convert float NaN to integer
error
sample = [innercitybypass,others,kingsfordcount] ##E.g.[0,1,2]
def zero_to_nan(sample):
return [float('nan') if x==0 else x for x in sample]
labels = ['InnerCityBypass','OtherRds','KingsfordRd']
###sizes = [innercitybypass,others,kingsfordcount]
sizes = zero_to_nan(sample)
#Set different colors
colors = ['green','red','blue']
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%',
startangle=140)
plt.axis('equal')
plt.show()
任何帮助将不胜感激,谢谢!
答案 0 :(得分:0)
使用numpy.nan(确保导入numpy)
import numpy as np
def zero_to_nan(sample):
return [np.nan if x==0 else x for x in sample]