Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Put zero for x axis values
#1
Hello,
I have this dict:

od = OrderedDict([(0, 1), (1, 1), (3, 1), (7, 1), (36, 1)])
And I am plotting this array with the following line:
plt.bar(range(len(od)), list(od.values()), align='center')
plt.xticks(range(len(od)), list(od.keys()),rotation='vertical')
IN this example for x axis, I have 0,1,3,7,36. However I don't have 2 between 1 and 3 and I don't have any other numbers between 7 and 36. So I want to update my dict like that:
od=([(0, 1), (1, 1),(2,0), (3, 1), (7, 1), (8,0),(9,0),(10,0),(11,0),...(36, 1)])
How can I do that?

I tried this way:
finalarray=np.zeros(60) # or sth larger than max (input dict keys)
for k,v in  od: 
    print(k,v)
    finalarray[k]=v
And I got this error:
Error:
TypeError: cannot unpack non-iterable int object
Reply
#2
I think what you wanted was (line 1):
od = OrderedDict([(0, 1), (1, 1), (3, 1), (7, 1), (36, 1)])
and assuming this code is in same script:
finalarray=np.zeros(60) # or sth larger than max (input dict keys)
for k,v in  od: 
    print(k,v)
    finalarray[k]=v
Reply
#3
(Jul-29-2021, 01:59 AM)Larz60+ Wrote: I think what you wanted was (line 1):
od = OrderedDict([(0, 1), (1, 1), (3, 1), (7, 1), (36, 1)])
and assuming this code is in same script:
finalarray=np.zeros(60) # or sth larger than max (input dict keys)
for k,v in  od: 
    print(k,v)
    finalarray[k]=v

Thanks for corrections but I still could not solve my problem
Reply
#4
okay, problem has been solved:
for k,v in  list(od.items()): 
      print(k,v)
      finalarray[k]=v
Reply
#5
I guess there is a better solution for matplotlib to fill the gaps.

A function which looks for the gaps and fill them:
def continious_keys(mapping, fillvalue=0):
    last = None
    
    for key, value in mapping.items():
        
        if last is None:
            yield key, value
            last = key
        else:
            while last + 1 < key:
                last += 1
                yield last, fillvalue
                
            yield key, value
            last = key
It's a generator, so you have to consume it. For example you can use a OrderedDict again.
from collections import OrderedDict


od = OrderedDict([(0, 1), (1, 1), (3, 1), (7, 1), (36, 1)])
generator = continious_keys(od)
new_od = OrderedDict(generator) # <- consumes the generator, creates a new OrderedDict
To make it better visible, I used as fillvalue=math.nan, which is not a number.
Example output from repl:
Output:
OrderedDict([(0, 1), (1, 1), (2, nan), (3, 1), (4, nan), (5, nan), (6, nan), (7, 1), (8, nan), (9, nan), (10, nan), (11, nan), (12, nan), (13, nan), (14, nan), (15, nan), (16, nan), (17, nan), (18, nan), (19, nan), (20, nan), (21, nan), (22, nan), (23, nan), (24, nan), (25, nan), (26, nan), (27, nan), (28, nan), (29, nan), (30, nan), (31, nan), (32, nan), (33, nan), (34, nan), (35, nan), (36, 1)])
quest likes this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 554 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Difference Between Figure Axis and Sub Plot Axis in MatplotLib JoeDainton123 2 813 Aug-21-2020, 10:17 PM
Last Post: JoeDainton123
  Sort y axis by descening values matplotlib mrsenorchuck 0 1,871 Dec-08-2019, 08:13 PM
Last Post: mrsenorchuck

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020