[英]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.