Search code examples
pythonnumpymatplotlibnonetypemap-function

Replacing Nones in a python array with zeroes


I've just joined two arrays of unequal length together with the command:

allorders = map(None,todayorders, lastyearorders)

where "none" is given where today orders fails to have a value (as the todayorders array is not as long).

However, when I try to pass the allorders array into a matplotlib bar chart:

 p10= plt.bar(ind, allorders[9],   width, color='#0000DD', bottom=allorders[8])

..I get the following error:

TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'

So, is there a way for matplotlib to accept none datatypes? if not, how do I replace the 'Nones' with zeroes in my allorders array?

If you can, as I am a Python newbie (coming over from the R community), please provide detailed code from start to finish that I can use/test.


Solution

  • Use a list comprehension:

    allorders = [i if i[0] is not None else (0, i[1]) for i in allorders]