画K线图

#以tushare数据为例,画K线图
import tushare as ts
import datetime
from matplotlib.pylab import date2num

import matplotlib as mpl
import tushare as ts
import matplotlib.pyplot as plt
import mpl_finance as mpf
%matplotlib inline


wdyx = ts.get_k_data('510500','2020-06-20')
wdyx.info()

# 对tushare获取到的数据转换成candlestick_ohlc()方法可读取的格式
    
def date_to_num(dates):
    num_time = []
    for date in dates:
        date_time = datetime.datetime.strptime(date,'%Y-%m-%d')
        num_date = date2num(date_time)
        num_time.append(num_date)
    return num_time

# dataframe转换为二维数组
mat_wdyx = wdyx.as_matrix()
num_time = date_to_num(mat_wdyx[:,0])
mat_wdyx[:,0] = num_time

# fig, ax = plt.subplots(figsize=(15,5))
# fig.subplots_adjust(bottom=0.5)
# mpf.candlestick_ochl(ax, mat_wdyx, width=0.6, colorup='r', colordown='g', alpha=1.0)
#plt.grid(True)
# 设置日期刻度旋转的角度 
# plt.xticks(rotation=30)
# plt.title('510500')
# plt.xlabel('Date')
# plt.ylabel('Price')
# ax.xaxis_date ()

fig, (ax1, ax2) = plt.subplots(2, sharex=True, figsize=(15,8))
mpf.candlestick_ochl(ax1, mat_wdyx, width=1.0, colorup = 'g', colordown = 'r')
ax1.set_title('510500')
ax1.set_ylabel('Price')
ax1.grid(True)
ax1.xaxis_date()
plt.bar(mat_wdyx[:,0]-0.25, mat_wdyx[:,5], width= 0.5)
ax2.set_ylabel('Volume')
ax2.grid(True)

 

K线图,你可以使用PHP的图形处理库,比如GD或ImageMagick。以下是一个简单的示例: ```php // 创建一个空白布 $image = imagecreatetruecolor(800, 600); // 定义K线图的数据 $k_data = array( array('open'=>100, 'close'=>120, 'high'=>130, 'low'=>90), array('open'=>120, 'close'=>90, 'high'=>125, 'low'=>85), array('open'=>90, 'close'=>110, 'high'=>115, 'low'=>80), // ... ); // 设置笔颜色 $black = imagecolorallocate($image, 0, 0, 0); $red = imagecolorallocate($image, 255, 0, 0); $green = imagecolorallocate($image, 0, 255, 0); // 计算最高价和最低价 $min_price = $k_data[0]['low']; $max_price = $k_data[0]['high']; foreach ($k_data as $k) { if ($k['low'] < $min_price) { $min_price = $k['low']; } if ($k['high'] > $max_price) { $max_price = $k['high']; } } // 计算K线图的高度和宽度 $k_height = 500; $k_width = 800; $k_top_margin = 50; $k_bottom_margin = 50; $k_left_margin = 50; $k_right_margin = 50; // 网格线 $grid_color = imagecolorallocate($image, 200, 200, 200); for ($i = 1; $i <= 4; $i++) { $y = $k_top_margin + $i * ($k_height / 4); imageline($image, $k_left_margin, $y, $k_left_margin + $k_width, $y, $grid_color); } // Y轴标签 $font_size = 12; $label_color = $black; $label_interval = ($max_price - $min_price) / 4; for ($i = 0; $i <= 4; $i++) { $price = $min_price + $i * $label_interval; $y = $k_top_margin + $k_height - $i * ($k_height / 4); $label = sprintf("%.2f", $price); $label_width = imagefontwidth($font_size) * strlen($label); $label_height = imagefontheight($font_size); imagestring($image, $font_size, $k_left_margin - $label_width - 5, $y - $label_height / 2, $label, $label_color); } // K线 $k_color = $green; $k_width = 10; $k_gap = 5; foreach ($k_data as $i => $k) { $x = $k_left_margin + ($k_width + $k_gap) * $i; $y_high = $k_top_margin + ($max_price - $k['high']) / ($max_price - $min_price) * $k_height; $y_low = $k_top_margin + ($max_price - $k['low']) / ($max_price - $min_price) * $k_height; if ($k['open'] > $k['close']) { // 阴线 $y_open = $k_top_margin + ($max_price - $k['open']) / ($max_price - $min_price) * $k_height; $y_close = $k_top_margin + ($max_price - $k['close']) / ($max_price - $min_price) * $k_height; imagefilledrectangle($image, $x, $y_open, $x + $k_width, $y_close, $k_color); imageline($image, $x + $k_width / 2, $y_high, $x + $k_width / 2, $y_open, $k_color); imageline($image, $x + $k_width / 2, $y_close, $x + $k_width / 2, $y_low, $k_color); } else { // 阳线 $y_open = $k_top_margin + ($max_price - $k['open']) / ($max_price - $min_price) * $k_height; $y_close = $k_top_margin + ($max_price - $k['close']) / ($max_price - $min_price) * $k_height; imagefilledrectangle($image, $x, $y_close, $x + $k_width, $y_open, $k_color); imageline($image, $x + $k_width / 2, $y_high, $x + $k_width / 2, $y_close, $k_color); imageline($image, $x + $k_width / 2, $y_open, $x + $k_width / 2, $y_low, $k_color); } } // 输出图像 header('Content-Type: image/png'); imagepng($image); imagedestroy($image); ``` 这个示例只是一个基础的K线图,你可以根据自己的需求进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值