简体   繁体   中英

Is it possible plot using ordered dictionary or list, OrderedDict doesn't work

I want to plot temperature decreasing in time. The calculation is correct, my problem is that dictionary can NOT be in order. I can sorted list in time. keys=time(1year,5year, 10year etc), values=temperature. I want sort by keys

drawtemperature={}
for i, tau in enumerate(years):
    for well in producers :
        Temperature=[]
            if len(reached) == 0:
                Temperature.append(T0)
            else: 
                 sumQ=well.flow/nbcontours*len(reached)
                 Tm=((well.flow-sumQ)*T0+sumQ*Ti)/well.flow
                 Temperature.append(Tm)   

           drawtemperature[tau]=Temperature

dc=[drawtemperature[k] for k in sorted(drawtemperature)]

for k in dc:
    pylab.plot(drawtemperature.keys(), drawtemperature.values())
pylab.show()

It drawing in wrong way, because of not ordering dictionary. The figure should be a piston like. I couldn't post images, because I need at least 10 reputation(:

I know that I can't order dictionary, I've alreday tried OrderedDict from collection, but it doesn't work. Any idea?

Something like this? I assume you are trying to sort the dictionary by value

pylab.plot(*zip(*sorted(dc.items(), key=lambda x:x[1])))

To switch the axes use this

pylab.plot(*reversed(zip(*sorted(dc.items(), key=lambda x:x[1]))))

To sort by key, just leave the key=... out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM