简体   繁体   中英

How do I plot a Python dictionary key against its corresponding dictionary value?

I am new to Python and I have the following dictionary:

results = {50: 18353.8393511688, 100: 18395.2151680032, 150: 18288.730020956387, 200: 18248.345889801505, 250: 18255.26922247291, 300: 18275.241922621914, 350: 18270.29183308043, 400: 18270.197974402367}

What I would like to do is to treat each key-value entry as given coordinates and plot (for example using matplotlib ) the dictionary keys on the y-axis and the dictionary values on the x-axis.

Does anyone know how this information can be extracted?

Thanks!

Marioanzas

You can access the keys and values of a dictionary via their .keys() and .values() methods, respectively.

from matplotlib import pyplot

fig, ax = pyplot.subplots()
ax.plot(results.keys(), results.values(), 'k-')

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