def industry_plot(self, dir_name, industry_info): industry_info.amount = industry_info.amount / 10000000000 total_amount = industry_info.amount.sum() amount_list = industry_info[0:10].amount.tolist() x = date.fromtimestamp(time.time()) fig = plt.figure() base_line = 0 for i in range(len(amount_list)): label_name = "%s:%s" % (industry_info.loc[i]['name'], 100 * amount_list[i] / total_amount) plt.bar(x, amount_list[i], width=0.1, color=self.COLORS[i], bottom=base_line, align='center', label=label_name) base_line += amount_list[i] plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y')) plt.gca().xaxis.set_major_locator(mdates.DayLocator()) plt.xlabel('x轴', fontproperties=get_chinese_font()) plt.ylabel('y轴', fontproperties=get_chinese_font()) plt.title('市值分布', fontproperties=get_chinese_font()) fig.autofmt_xdate() plt.legend(loc='upper right', prop=get_chinese_font()) plt.savefig('%s/industry.png' % dir_name, dpi=1000) plt.close(fig)
def multi_plot(self, x_dict, y_dict, ylabel, title, dir_name, filename): def _format_date(i, pos=None): if i < 0 or i > len(x) - 1: return '' return x[int(i)] xlabel = list(x_dict.keys())[0] x = list(x_dict.values())[0] xn = range(len(x)) fig = plt.figure() plt.title(title, fontproperties=get_chinese_font()) plt.xlabel(xlabel, fontproperties=get_chinese_font()) plt.ylabel(ylabel, fontproperties=get_chinese_font()) plt.gca().xaxis.set_major_locator(mticker.MultipleLocator(10)) plt.gca().xaxis.set_major_formatter( mticker.FuncFormatter(_format_date)) i = 0 for ylabel, y in y_dict.items(): i += 1 plt.plot(xn, y, label=ylabel) num = 0 for xi, yi in zip(xn, y): if num % 11 == 0 or num == len(x) - 1: plt.plot((xi, ), (yi, ), 'ro') plt.text(xi, yi, '%.2f' % yi, fontsize=5, rotation=60) num += 1 #plt.scatter(xn, y, color = self.COLORS[i], s = 5, marker = "o") fig.autofmt_xdate() plt.legend(prop=get_chinese_font()) plt.savefig('%s/%s.png' % (dir_name, filename), dpi=1000) plt.close(fig)
def animate(i): ax.clear() ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S')) ax.xaxis.set_major_locator(mdates.DayLocator()) ax.set_title('盯盘', fontproperties=get_chinese_font()) ax.set_xlabel('时间', fontproperties=get_chinese_font()) ax.set_ylabel('增长', fontproperties=get_chinese_font()) ax.set_ylim((-6, 6)) fig.autofmt_xdate() for name in name_list: pchange_list = list() price_list = cdata[cdata.name == name]['price'].tolist() pchange_list.append(0) for _index in range(1, len(price_list)): pchange_list.append( 10 * (price_list[_index] - price_list[_index - 1]) / price_list[0]) ax.plot(ctime_list[0:i], pchange_list[0:i], label=name, linewidth=1.5) if pchange_list[i - 1] > 1 or pchange_list[i - 1] < -1: ax.text(ctime_list[i - 1], pchange_list[i - 1], name, font_properties=get_chinese_font())
def plot_bullration(self, df, dir_name, file_name): def _format_date(i, pos=None): if i < 0 or i > len(date_list) - 1: return '' return date_list[int(i)] fig, ax = plt.subplots() date_list = df['date'].tolist() ratio_list = df['ratio'].tolist() xn = range(len(date_list)) ax.plot(xn, ratio_list) num = 0 for xi, yi in zip(xn, ratio_list): if num % 11 == 0 or num == len(ratio_list) - 1: plt.plot((xi, ), (yi, ), 'ro') plt.text(xi, yi, '%.2f' % yi) num += 1 plt.xlabel('时间', fontproperties=get_chinese_font()) plt.ylabel('牛股比例', fontproperties=get_chinese_font()) plt.title('牛熊股比', fontproperties=get_chinese_font()) plt.gca().xaxis.set_major_locator(mticker.MultipleLocator(19)) plt.gca().xaxis.set_major_formatter( mticker.FuncFormatter(_format_date)) fig.autofmt_xdate() fig.savefig('%s/%s.png' % (dir_name, file_name), dpi=1000) plt.close(fig)
def plot_pie(self, df, column, title, xtuple, dir_name, filename, ctype=None): def xfunc(pct, allvals): absolute = int(pct / 100. * np.sum(allvals)) return "{:.1f}%".format(pct) df = df[['name', 'code', column]] data = df[column].tolist() fig, ax = plt.subplots(figsize=(6, 4), subplot_kw=dict(aspect="equal")) ingredients = (df.name + ':' + df.code).tolist() ax.set_title(title, fontproperties=get_chinese_font()) if ctype is not None: wedges, texts, autotexts = ax.pie( data, center=(-100, 0), radius=1, labels=xtuple, autopct=lambda pct: xfunc(pct, data), textprops=dict(color="w", fontproperties=get_chinese_font()), labeldistance=1.1, pctdistance=1.1, startangle=90) ax.legend(wedges, ingredients, title='name', loc="lower left", bbox_to_anchor=(1, 0), prop=get_chinese_font(), fontsize='x-small') plt.setp(autotexts, size=8) plt.setp(texts, size=8, color='b') else: wedges, texts = ax.pie(data, center=(-100, 0), radius=1, labels=xtuple, textprops=dict( color="w", fontproperties=get_chinese_font()), labeldistance=1.1, pctdistance=1.1, startangle=90) ax.legend(wedges, ingredients, title='name', loc="lower left", bbox_to_anchor=(1, 0), prop=get_chinese_font(), fontsize='x-small') plt.setp(texts, size=8, color='b') fig.autofmt_xdate() plt.savefig('%s/%s.png' % (dir_name, filename), dpi=1000) plt.close(fig)
def static_plot(self, stock_info, limit_info, dir_name, file_name): limit_up_list = limit_info[(limit_info.pchange > 0) & (limit_info.prange != 0)].reset_index( drop=True).code.tolist() limit_down_list = limit_info[limit_info.pchange < 0].reset_index( drop=True).code.tolist() limit_list = limit_up_list + limit_down_list stock_info = stock_info[~stock_info.code.isin(limit_list)] changepercent_list = [9, 7, 5, 3, 1, 0, -1, -3, -5, -7, -9] num_list = list() name_list = list() num_list.append(len(limit_up_list)) name_list.append("涨停") c_length = len(changepercent_list) for index in range(c_length): pchange = changepercent_list[index] if 0 == index: num_list.append(len(stock_info[stock_info.pchange > pchange])) name_list.append(">%s" % pchange) elif c_length - 1 == index: num_list.append(len(stock_info[stock_info.pchange < pchange])) name_list.append("<%s" % pchange) else: p_max_change = changepercent_list[index - 1] num_list.append( len(stock_info[(stock_info.pchange > pchange) & (stock_info.pchange < p_max_change)])) name_list.append("%s-%s" % (pchange, p_max_change)) num_list.append(len(limit_down_list)) name_list.append("跌停") fig = plt.figure() for i in range(len(num_list)): plt.bar(i + 1, num_list[i], color=self.COLORS[i % len(self.COLORS)], width=0.3) plt.text(i + 1, 15 + num_list[i], num_list[i], ha='center', font_properties=get_chinese_font()) plt.xlabel('x轴', fontproperties=get_chinese_font()) plt.ylabel('y轴', fontproperties=get_chinese_font()) plt.title('涨跌分布', fontproperties=get_chinese_font()) plt.xticks(range(1, len(num_list) + 1), name_list, fontproperties=get_chinese_font()) fig.autofmt_xdate() plt.savefig('%s/%s.png' % (dir_name, file_name), dpi=1000) plt.close(fig)
def emotion_plot(self, df, dir_name, file_name): fig = plt.figure() x = df.date.tolist() xn = range(len(x)) y = df.score.tolist() plt.plot(xn, y) for xi, yi in zip(xn, y): plt.plot((xi, ), (yi, ), 'ro') plt.text(xi, yi, '%s' % yi) plt.scatter(xn, y, label='score', color='k', s=25, marker="o") plt.xticks(xn, x) plt.xlabel('时间', fontproperties=get_chinese_font()) plt.ylabel('分数', fontproperties=get_chinese_font()) plt.title('股市情绪', fontproperties=get_chinese_font()) fig.autofmt_xdate() plt.savefig('%s/%s.png' % (dir_name, file_name), dpi=1000)
def plot_ohlc(self, df, ylabel, flabel, dir_name, filename): date_tickers = df.date.tolist() def _format_date(x, pos = None): if x < 0 or x > len(date_tickers) - 1: return '' return date_tickers[int(x)] fig, ax = plt.subplots(figsize = (16, 10)) fig.subplots_adjust(bottom = 0.2) candlestick_ohlc(ax, df.values, width = 1.0, colorup = 'r', colordown = 'g') ax.xaxis.set_major_locator(mticker.MultipleLocator(3)) ax.xaxis.set_major_formatter(mticker.FuncFormatter(_format_date)) ax.set_ylabel(ylabel, fontproperties = get_chinese_font()) ax.set_title(flabel, fontproperties = get_chinese_font()) ax.yaxis.label.set_color("k") ax.grid(True, color = 'k', linestyle = '--') fig.autofmt_xdate() plt.savefig('%s/%s.png' % (dir_name, filename), dpi=1000)
def animate(i): cdate = dates[i] df = groups.get_group(cdate) init() print(cdate, len(df)) bull_stock_num = len(df[df.profit >= 0]) for code in df.code.tolist(): pday = df.loc[df.code == code, 'pday'] profit = df.loc[df.code == code, 'profit'] ax.scatter(pday, profit, color='black', s=1) ax.set_title("日期:%s 股票总数:%s 牛熊股比:%s" % (cdate, len(df), 100 * bull_stock_num / len(df)), fontproperties=get_chinese_font())