如何解决为什么Matplotlib图形不响应Python 3.8.2
这是我的代码
import pandas as pd
from datetime import datetime
import numpy as np
import time
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import time
import sys
import pandas as pd
from mpl_finance import candlestick_ohlc
try:
sys.modules['mpl_finance'] = __import__('six')
sys.modules['mpl_finance'] = __import__('joblib')
import hdbscan
except ImportError:
import mpl_finance
# - Initialise the graph and settings
fig = plt.figure(figsize=(8,5))
ax = plt.subplot2grid((1,1),(0,0))
plt.ion()
# - Show the blank graph
fig.show()
#fig.canvas.draw()
#Date time format for timeseries data. It has milliseconds
format_string = '%Y-%m-%d %H:%M:%S.%m'
DATA_GENERATION_DELAY = 5 # seconds
CHART_CANDLE_LENGTH = "1Min" #1 min as a string
#Initialize the panda dataframe with price as column. Price is in datetime format
df = pd.DataFrame(columns=['price'],dtype=float,index=pd.to_datetime([]))
def convert_ticks_to_ohlc2(df,df_column,timeframe):
data_ohlc = df[df_column].resample(timeframe).ohlc()
#print(data_ohlc)
return data_ohlc
#Function to generate some random price value with a timestamp
def generateRandomData():
now = datetime.now()
ts = now.strftime(format_string)
value = np.random.randint(0,100)
# append the generated value to the end of the data frame
df.loc[pd.Timestamp('now')] = pd.Series([value],['price'])
#Repeat the loop to generate data and update the chart simultaneously
while True:
generateRandomData()
candlestick_ohlc2 = convert_ticks_to_ohlc2(df,"price",CHART_CANDLE_LENGTH) # The chart will
display 1 minute candle bars
#print("Candlestick data is ",candlestick_ohlc)
all_candles = len(candlestick_ohlc2)
print(all_candles)
for candle in range(all_candles):
candles_to_show = candlestick_ohlc2[0:candle]
ohlc_counter = range(len(candles_to_show["open"]))
ohlc = []
for ohlc_item in ohlc_counter:
append_me = ohlc_counter[ohlc_item],\
candles_to_show["open"][ohlc_item],\
candles_to_show["high"][ohlc_item],\
candles_to_show["low"][ohlc_item],\
candles_to_show["close"][ohlc_item]
ohlc.append(append_me)
ax.clear() # - Clear the chart
candlestick_ohlc(ax,ohlc,width=0.4,colorup='#075105',colordown='#AF141A')
for label in ax.xaxis.get_ticklabels():
label.set_rotation(45)
ax.xaxis.set_major_locator(mticker.MaxNLocator(10))
ax.grid(True)
plt.grid(False)
plt.xlabel('Candle count')
plt.ylabel('Price')
plt.title('Candlestick chart simulation')
plt.subplots_adjust(left=0.09,bottom=0.20,right=0.94,top=0.90,wspace=0.2,hspace=0)
fig.canvas.draw() # - Draw on the chart
# Adding a delay
time.sleep(DATA_GENERATION_DELAY) # Delays for 5 seconds
我听说要更改时间。但是您认为这不起作用吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。