使用一个命令在 python matplotlib 绘图中 DRY
DRY in python matplotlib plotting using one single command
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 一起做金融,顺便说一句
你可以这样做:
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()
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 一起做金融,顺便说一句
你可以这样做:
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()