Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 绘制dict`{quot;label";:[…日期…],[…值…]}`的最佳方法是什么?_Python_List_Dataframe_Dictionary_Plot - Fatal编程技术网

Python 绘制dict`{quot;label";:[…日期…],[…值…]}`的最佳方法是什么?

Python 绘制dict`{quot;label";:[…日期…],[…值…]}`的最佳方法是什么?,python,list,dataframe,dictionary,plot,Python,List,Dataframe,Dictionary,Plot,我有一个dict,每个键包含两列:日期列表和值列表 data = {"Key_1" : [ [ 10-1-20, 11-1-20, ...], [ 1.0, 2.1, ... ], ], "Key_2" : [ [ 11-1-20, 12-1-20, ...], [ 3.2, 4.1, ... ], ], "Key_3" : [ [ 11-1-20, 13-1-20, ...], [ 5.342, 2.124, ... ], ], "Key_4"

我有一个dict,每个键包含两列:日期列表和值列表

data = {"Key_1" : [ [ 10-1-20, 11-1-20, ...], [ 1.0, 2.1, ... ], ],
        "Key_2" : [ [ 11-1-20, 12-1-20, ...], [ 3.2, 4.1, ... ], ],
        "Key_3" : [ [ 11-1-20, 13-1-20, ...], [ 5.342, 2.124, ... ], ],
        "Key_4" : [ [ 10-1-20, 12-1-20, ...], [ 2.123, 4.321, ... ], ]}
已排序日期(选中)。然而, 当我这么做的时候

for l in matrix:
    ax.plot(matrix[l][0], matrix[l][1], label=l)

绘制我的dict的最佳方法是什么?在我的例子中,为什么行返回日期?

使用
pandas.DataFrames的
dict
资料
data={“Key_1”:['10-1-20','11-1-20',[1.0,2.1]],
“键2”:['11-1-20','12-1-20',[3.2,4.1]],
“密钥_3”:['11-1-20','13-1-20',[5.342,2.124]],
“键4”:['10-1-20','12-1-20'],[2.123,4.321]}
数据帧记录
将熊猫作为pd导入
df_dict={key:pd.DataFrame({'dates':pd.to_datetime(data[key][0],dayfirst=True),
“数据”:数据中的键的数据[key][1]}。keys()}
对于df_dict.items()中的k,v:
打印(f'DataFrame{k}:\n{v}\n')
数据帧键_1:
日期数据
0 2020-10-01   1.0
1 2020-11-01   2.1
数据帧键2:
日期数据
0 2020-11-01   3.2
1 2020-12-01   4.1
数据帧键3:
日期数据
0 2020-11-01  5.342
1 2020-01-13  2.124
数据帧键4:
日期数据
0 2020-10-01  2.123
1 2020-12-01  4.321
绘图
df_dict
导入matplotlib.pylot作为plt
对于df_dict.items()中的k,v:
plt.plot(v['dates',v['data',label=k)
plt.xticks(旋转=45)
plt.grid(真)
plt.legend()
plt.show()

我猜您没有正确设置日期格式。示例数据中的“日期”格式无效。否则,您没有提供一个可复制的示例,显示所有步骤。@TrentonMcKinney,True。谢谢你的帮助。但即使在我按照你的建议做了之后,我的约会还是会变得混乱,我的图表看起来更糟糕。由于某种原因,在某些行中,月数与日数混淆,这正是我之前得到的。。。所以我猜这是一些日期转换问题?尝试将
pd.的
dayfirst
参数更改为_datetime
(例如
pd.到_datetime(data[key][0],dayfirst=True)
,或者您可以将
格式
参数指定为_datetime