循环绘制成Dict Python MATPLOTLIB [英] Loop to graph into a Dict Python MATPLOTLIB

查看:59
本文介绍了循环绘制成Dict Python MATPLOTLIB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里想要做的是使一个循环创建多个字典图.

我循环执行单变量滚动窗口回归,并将每个变量的COEFS和R_SQ存储在DICT中.

DICT itsel获得了23个子DICT,每个子DICT包含我从2008年到2020年的一系列系数和r平方(4580个观测点).

它是这样的(用于可视化):

  data1BE10USD参数-const&efr_sqBE30CAD参数-const&efr_sqSWAP1YUSD参数-const&efr_sq 

接着,我的字典由23个"sub dicts"组成.像这样.

因此,我想做的是循环创建所有这些子区域的图形.详细地说,我想绘制出r_sq和coef随时间的变化情况.

这是我的一个简单图形(一个变量)的代码,我真的很喜欢这个结果:

  #Plotfig1 = rres.plot_recursive_coefficient(变量= ['BE10USD'],figsize =(14,6))#标签名称plt.xlabel('日期')plt.ylabel('R.平方和系数')#添加r2r_sq.plot()#添加图例导入matplotlib.patches作为mpatchesorange_patch = mpatches.Patch(颜色='橙色​​',标签='r2')blue_patch = mpatches.Patch(颜色='蓝色',标签='系数')plt.legend(handles = [orange_patch,blue_patch]) 

是否有办法将此图循环到所有字典中,并为dict键赋予图的标题?

我是python的新手,因此您可以提供的任何帮助都将非常有用.谢谢!

解决方案

我无法从提供的脚本中完全了解您的工作区和变量,但是原则上很容易遍历python中的所有可迭代对象并使用 matplotlib命令以在循环内调用图.这是一个示例:

  • color_list 是十六进制字符串的列表,指示着色器图
  • mydict 是您的父词典(或任何子词典,进行相应的访问)
  • 我认为您的数据在 mydict [key]

 #在一个绘图中递归绘图无花果,ax = plt.subplots(1,1,figsize =(12,8))对于密钥,使用zip(mydict.keys(),color_list)中的颜色:ax.plot(mydict [key],label = key,ls ='-',color = color) 

 #在多个子图中进行递归绘制(单列)无花果,ax = plt.subplots(5,1,figsize =(14,12))用于i,key,enumerate中的颜色(zip(mydict.keys(),color_list)):ax [i,1] .plot(mydict [key],label = key,ls ='-',color = color) 

在循环中处理各个子图设置.然后,您可以在循环外部完成父图属性(例如图例或轴限制)的确定.当然,如果您需要在多个轴上划分子图(例如在网格中),那么事情会变得更加棘手,然后您可能想使用模创建一个函数以从单个索引 i 到网格 nrow,ncol .

What i'm trying to do here is to make a loop to create multiple graphs of dicts.

I looped to execute univariate rolling window regressions and I stored the COEFS and the R_SQ of each variable in a DICT.

the DICT itsel got 23 sub DICT each containing my a series of coefficients and rsquareds from 2008 to 2020 (4580 obs.)

It goes like this (for visualisation):

data1
   BE10USD
     params - const & coef 
     r_sq
   BE30CAD
     params - const & coef 
     r_sq
   SWAP1YUSD
     params - const & coef 
     r_sq

And on, my dict is composed of 23 "sub dicts" like this.

SO, what i'd like to do, is loop to create graph of all these subdiscts. In detail, I'd like to graph the evolution of the r_sq and the coef in time.

Here was my code for a simple graph (of one variable), I really liked the results of this one:

#Plot
fig1 = rres.plot_recursive_coefficient(variables=['BE10USD'], figsize=(14,6))

#Label names
plt.xlabel('Date')
plt.ylabel('R.squared & Coefficient')

#Add r2
r_sq.plot()

#Add legend
import matplotlib.patches as mpatches
orange_patch = mpatches.Patch(color='orange', label='r2')
blue_patch = mpatches.Patch(color='blue', label='Coefficient')
plt.legend(handles=[orange_patch,blue_patch])

Would there be a way to loop this graph into all the dict, giving the dict Key the title of the graph?

I'm new to python so any help you can provide would be super helpfull. Thank you!

解决方案

I don't fully understand your workspace and variables from your provided scripts, but in principle it's simple to loop through any iterable in python and use matplotlib commands to call plots within the loop. Here is an example:

  • color_list is a list of hex strings, indicating colorper plot
  • mydict is your parent dictionary (or any subdictionary, access accordingly)
  • I assume your data is in mydict[key]

# plotting recursively in one plot
fig, ax = plt.subplots(1,1,figsize=(12,8))

for key, color in zip(mydict.keys(), color_list):
    ax.plot(mydict[key], label=key, ls='--', color=color)

# plotting recursively in multiple subplots (single column)
fig, ax = plt.subplots(5,1,figsize=(14,12))

for i,key,color in enumerate(zip(mydict.keys(), color_list)):
    ax[i,1].plot(mydict[key], label=key, ls='--', color=color)

Treat individual subplot settings within your loop. You can then finalize parent plot properties (such as the legend or axes limits) outside the loop. Of course, things get more tricky if you need to divide subplots across multiple axes (i.e. in a grid) and you may then want to create a function using the modulo % to get from a single index i to a grid nrow, ncol.

这篇关于循环绘制成Dict Python MATPLOTLIB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆