简体   繁体   English

我如何 plot 一个 Python 字典键与其对应的字典值?

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

I am new to Python and I have the following dictionary:我是 Python 的新手,我有以下字典:

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.我想做的是将每个键值条目视为给定坐标和 plot (例如使用matplotlib )y轴上的字典键和x轴上的字典值。

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.您可以分别通过其.keys().values()方法访问字典的键和值。

from matplotlib import pyplot

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 的嵌套字典中用其对应的 Boolean 值替换字符串值? - How can I replace a string value with its corresponding Boolean value in a nested dictionary in Python? 用一个键值和没有对应的值在python中初始化一个字典 - Initializing a dictionary in python with a key value and no corresponding values 密钥对应python字典中的最大值 - key corresponding to maximum value in python dictionary 如何在Python的列表中保存与字典的每个值相对应的键? - How to save key corresponding to every value of a dictionary in a list in Python? 如何通过具有相应键的字典值更改列中的值? Python - How I can change value in column by a value of dictionary with corresponding key ? Python 如何 plot 列出 python 中字典键的值 - How to plot list if values with respect to its key of a dictionary in python Python tkinter combobox select 字典键如何打印对应值? - Python tkinter combobox, how can I select dictionary key and print corresponding value? Python:如何从字典键中随机选择一个值? - Python: How do I randomly select a value from a dictionary key? 如何在Python中汇总字典并按键值排序 - How do I Sum Dictionary in Python and Sort by Key Value 如何在 Python 中按键检索字典值? - How do I retrieve a dictionary value by key in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM