Python 3.x 如何在同一图形上绘制不同的烛台图形?
我想绘制烛台图并保存我绘制的每个图。对于速度,我想在同一个图形上进行此操作。 到目前为止,我可以保存第一张图像,但其余的都是空白 我试过使用:Python 3.x 如何在同一图形上绘制不同的烛台图形?,python-3.x,matplotlib,Python 3.x,Matplotlib,我想绘制烛台图并保存我绘制的每个图。对于速度,我想在同一个图形上进行此操作。 到目前为止,我可以保存第一张图像,但其余的都是空白 我试过使用: pltclf() plt.cla() fig.clear() 我的代码: import pandas as pd import matplotlib.pyplot as plt from mpl_finance import candlestick2_ohlc from itertools import count colNames = ['Date'
pltclf()
plt.cla()
fig.clear()
我的代码:
import pandas as pd
import matplotlib.pyplot as plt
from mpl_finance import candlestick2_ohlc
from itertools import count
colNames = ['Date', 'Time', 'Open', 'High', 'Low', 'Close']
allData = pd.read_csv('file.csv', header=0, names=colNames, usecols=colNames,
parse_dates={'Datetime': ['Date', 'Time']},
infer_datetime_format=True)
fig, ax = plt.subplots()
for n in count(0, 10):
subset = allData[n:n+10]
plt.axis('off')
try:
candlestick2_ohlc(ax, subset['Open'], subset['High'], subset['Low'],
subset['Close'], width=0.6, colorup='g',
colordown='r', alpha=1)
except ValueError:
print("Graphing done.")
break
fig.savefig("{}.png".format(str(n)))
plt.show()
fig.clf()
使用的CSV文件(File.CSV):
我认为这个问题与烛台有关,它指向“ax”,但我不知道如何解决我的问题。以下是我要做的:
from mpl_finance import candlestick2_ohlc
from itertools import count
colNames = ['Date', 'Time', 'Open', 'High', 'Low', 'Close']
allData = pd.read_csv(StringIO(d), header=0, names=colNames, usecols=colNames,
parse_dates={'Datetime': ['Date', 'Time']},
infer_datetime_format=True)
fig, ax = plt.subplots()
for n in count(0, 10):
subset = allData[n:n+10]
plt.axis('off')
if len(subset)>0:
candlestick2_ohlc(ax, subset['Open'], subset['High'], subset['Low'],
subset['Close'], width=0.6, colorup='g',
colordown='r', alpha=1)
fig.savefig("{}.png".format(str(n)))
ax.clear()
else:
break
我想您想用
ax.clear()
替换fig.clf()
,也就是说,清除轴而不是图形。奇怪的是,这保留了当.show()时图形显示为空白的问题(不包括第一个图形)。但是,图形现在已正确保存,除了所有包含其轴的图形(不包括第一个图形)。
from mpl_finance import candlestick2_ohlc
from itertools import count
colNames = ['Date', 'Time', 'Open', 'High', 'Low', 'Close']
allData = pd.read_csv(StringIO(d), header=0, names=colNames, usecols=colNames,
parse_dates={'Datetime': ['Date', 'Time']},
infer_datetime_format=True)
fig, ax = plt.subplots()
for n in count(0, 10):
subset = allData[n:n+10]
plt.axis('off')
if len(subset)>0:
candlestick2_ohlc(ax, subset['Open'], subset['High'], subset['Low'],
subset['Close'], width=0.6, colorup='g',
colordown='r', alpha=1)
fig.savefig("{}.png".format(str(n)))
ax.clear()
else:
break