简体   繁体   English

Python - 如何使用字典正确绘制折线图?

[英]Python - How can I plot a line graph properly with a dictionary?

I am trying to plot a line graph to show the trends of each key of a dictionary in Jupyter Notebook with Python.我试图用 Python 绘制一个折线图来显示 Jupyter Notebook 中字典每个键的趋势。 This is what I have in the k_rmse_values variable as shown below:这是我在 k_rmse_values 变量中的内容,如下所示:

k_rmse_values = {'bore': {1: 8423.759328233446, 3: 6501.928933614838, 5: 6807.187615513473, 7: 6900.29659028346, 9: 7134.8868708101645}, 'city-mpg': {1: 4265.365592771621, 3: 3865.0178306330113, 5: 3720.409335758634, 7: 3819.183283405616, 9: 4219.677972675927}, 'compression-rate': {1: 7016.906657495168, 3: 7319.354017489066, 5: 6301.624922763969, 7: 6133.006310754547, 9: 6417.253959732598}, 'curb-weight': {1: 3950.9888180049306, 3: 4201.343428000144, 5: 4047.052502155118, 7: 3842.0974736649846, 9: 3943.9478256384205}, 'engine-size': {1: 2853.7338453331627, 3: 2793.6254775629623, 5: 3123.320055069605, 7: 2941.73029681235, 9: 2931.996240628853}, 'height': {1: 6330.178232877807, 3: 7049.500497198366, 5: 6869.570862695864, 7: 6738.641089739572, 9: 6344.062937760911}, 'highway-mpg': {1: 4826.0580187146525, 3: 3510.253629329685, 5: 3379.2250123364083, 7: 4044.271135312068, 9: 4462.027046251678}, 'horsepower': {1: 3623.6389886411143, 3: 4294.825669466819, 5: 4778.254807521257, 7: 4730.5387015 k_rmse_values = { '孔':{1:8423.759328233446,3:6501.928933614838,5:6807.187615513473,7:6900.29659028346,9:7134.8868708101645}, '城市MPG':{1:4265.365592771621,3:3865.0178306330113,5:3720.409335758634,7: 3819.183283405616,9:4219.677972675927},'压缩率:{1:7016.906657495168,3:7319.354017489066,5:6301.624922763969,7:6133.006310754547,9:6417.253959732598}, '路边 - 重':{1:3950.9888180049306,3:4201.343428000144, 5:4047.052502155118,7:3842.0974736649846,9:3943.9478256384205}, '发动机尺寸':{1:2853.7338453331627,3:2793.6254775629623,5:3123.320055069605,7:2941.73029681235,9:2931.996240628853}, '高度':{1:6330.178232877807, 3:7049.500497198366,5:6869.570862695864,7:6738.641089739572,9:6344.062937760911}, '高速公路-MPG':{1:4826.0580187146525,3:3510.253629329685,5:3379.2250123364083,7:4044.271135312068,9:4462.027046251678}, '马力':{ 1: 3623.6389886411143, 3: 4294.825669466819, 5: 4778.254807521257, 7: 4730.5387015 14935, 9: 4662.8601512508885}, 'length': {1: 4952.798701744297, 3: 5403.624431188139, 5: 5500.731909846179, 7: 5103.4515274528885, 9: 4471.077661709427}, 'normalized-losses': {1: 9604.929081466453, 3: 7494.820436511842, 5: 6391.912634697067, 7: 6699.853883298577, 9: 6861.6389834002875}, 'peak-rpm': {1: 8041.2366213164005, 3: 7502.080095843049, 5: 6521.863037752326, 7: 6869.602542315512, 9: 6884.533017667794}, 'stroke': {1: 10330.231237489314, 3: 8947.585146097614, 5: 6973.912792744113, 7: 7266.333478250421, 9: 7026.017456146411}, 'wheel-base': {1: 2797.4144312203725, 3: 3392.8627620671928, 5: 4238.25624378706, 7: 4456.687059524217, 9: 4426.032222634904}, 'width': {1: 2849.2691940215127, 3: 4076.59327053035, 5: 3979.9751617315405, 7: 3845.3326184519606, 9: 3687.926625900343}} 14935,9:4662.8601512508885}, '长度':{1:4952.798701744297,3:5403.624431188139,5:5500.731909846179,7:5103.4515274528885,9:4471.077661709427}, '归一化后损失':{1:9604.929081466453,3:7494.820436511842,5: 6391.912634697067,7:6699.853883298577,9:6861.6389834002875}, '峰RPM':{1:8041.2366213164005,3:7502.080095843049,5:6521.863037752326,7:6869.602542315512,9:6884.533017667794}, '抚摸':{1:10330.231237489314,3: 8947.585146097614,5:6973.912792744113,7:7266.333478250421,9:7026.017456146411}, '轴距':{1:2797.4144312203725,3:3392.8627620671928,5:4238.25624378706,7:4456.687059524217,9:4426.032222634904}, '宽度':{1: 2849.2691940215127, 3: 4076.59327053035, 5: 3979.9751617315405, 7: 3845.3326184519606, 9: 36623}2}0

When I used this code to plot当我使用此代码进行绘图时

for k,v in k_rmse_values.items():
x = list(v.keys())
y = list(v.values())

plt.plot(x,y)
plt.xlabel('k value')
plt.ylabel('RMSE')

and it doesn't plot from 1 to 9 in order;它没有按顺序从 1 到 9 绘制; it gives this graph它给出了这个图在此处输入图片说明

it plots in this k-value order 1, 3, 9 , 5, 7它以 k 值顺序 1, 3, 9 , 5, 7 绘制

I have spent hours on this problem and still can't figure out a way to do it.我已经在这个问题上花费了几个小时,但仍然无法找到解决方法。 Your help with this would be greatly appreciated.您对此的帮助将不胜感激。

One solution is to sort the keys and get the matching values:一种解决方案是对键进行排序并获取匹配的值:

for k,v in k_rmse_values.items():
  xs = list(v.keys()).sort()
  ys = [v[x] for x in xs]

# Note I renamed these arrays so following uses should be changed accordingly

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

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