python - 使用一个命令在 python matplotlib 中进行 DRY 绘图
问题描述
import pandas as pd
import matplotlib.pyplot as plt
> importing csv files
january = pd.read_csv('divident_month/january.csv')
april = pd.read_csv('divident_month/april.csv')
july = pd.read_csv('divident_month/july.csv')
october = pd.read_csv('divident_month/october.csv')
> substracting column 'Open' to column close
jangain = january['Open']-january['Close']
aprgain = april['Open']-april['Close']
julgain = july['Open']-july['Close']
octgain = october['Open']-october['Close']
>plotting
medium=[jangain, aprgain, julgain, octgain]
plt.plot(medium)
plt.show()
## jan = plt.plot(jangain, label='january')
## apr =plt.plot(aprgain, label='april')
## jul =plt.plot(julgain, label='july')
## oct =plt.plot(octgain, label='october')
## plt.legend()
如何在不重复自己的情况下将多个项目绘制到图表中,就像我在## 中所做的那样。我有多个具有不同月份重复代码的文件(它们在 div 月份之前、div 月份和 div 月份之后分组在不同的文件中)。
我已经尝试将它们分组到一个列表(中)中并将列表传递到plt.plot(medium)
,但这似乎不起作用。
我还给出了地块名称(例如 Jan、apr...),因为我将它们导入到不同的文件中进行 q1、q2、q3、q4 分析(以防信息混淆)
这是我试图用 python 做金融 btw
解决方案
你可以这样做:
import os
import pandas as pd, collections as co, matplotlib.pyplot as plt
the_dir = 'divident_month/'
months = co.OrderedDict() # use if order of csv files is important
# iterate over files in directory
for a_file in sorted(os.listdir(the_dir)):
if os.path.splitext(a_file)[-1] == '.csv':
# add DataFrame entries to dictionary
months[a_file.rstrip('.csv')] = pd.read_csv(os.path.join(the_dir,a_file))
# perform gain calculation (k: dictionary `k`ey, v: dictionary `v`alue)
for k,v in months.items():
months[k] = v['Open'] - v['Close']
# get an axes handle from matplotlib so you can re-use it
fig,ax = plt.subplots()
# plot without manual repetition of plot command
for k,v in months.items():
ax = v.plot(ax=ax,label=k)
# add title and show plot
plt.gcf().suptitle('Gain')
plt.show()
推荐阅读
- c++ - 更新返回 0 即使表中没有这个 id
- chisel - firrtl.Driver 已被弃用 - 但我们应该改用什么?
- python-2.7 - Paramiko 在某些 llinux 设备上挂在 stdout.readlines() 上,但在其他设备上工作正常
- asp.net-mvc - 如何制作 linq lambda 表达式并动态设置要在表达式中使用的表?
- java - Java中if条件中的编译错误
- javascript - 通过以编程方式更改的输入触发需要事件的 onChange
- r - 从 pmap 转换为 pmap_dfr purrr 时使用列表名称作为行名称
- typescript - 具有一个且只有一个属性的对象的类型
- javascript - 如何修复文件javascript中不存在名称“ViewBag”
- sql-server - 为什么“sys.sql_expression_dependencies”对 Azure 数据仓库中的存储过程没有任何参考?