Hi, I’m 👉

This issue mainly uses Pyecharts to show the changes of bitcoin stock price in recent years, mainly using the timeline function, hoping to help you.

First look at the results:

1. Import modules

import datetime
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Line,Timeline
from pyecharts.commons.utils import JsCode
Copy the code

Upgrade pyecharts package: Pyecharts ==1.9.0 is required for visualization. If you already have pyecharts installed, you will need to upgrade. If you have not installed Pyecharts before, you will install PIP as the latest version.

2. Pandas data processing

2.1 Reading Data

df = pd.read_csv('btc.csv')
df
Copy the code

Results:

2.2 Interception of serial time data

The data time can be adjusted as required (2019-09-01 as an example) :

df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')
df = df[(df['date'] >=pd.to_datetime('20190901'))]
Copy the code

Results:

3. Pyecharts dynamic diagram drawing

3.1 Setting the coordinate data style in the abscissa

Line.add_xaxis(list(df['date'])[0:i]) Line.add_yaxis( series_name="", y_axis=list(df['closep'])[0:i], is_smooth=True,is_symbol_show=False, linestyle_opts={ 'normal': { 'width': 3, 'shadowColor': Rgba (0, 0, 0, 0.5), 'shadowBlur': 5, 'shadowOffsetY': 10, 'shadowOffsetX': 10, 'curve': 0.5, 'color': JsCode(color_js0) } }, itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_js0)), markpoint_opts=opts.MarkPointOpts( data=[ opts.MarkPointItem( name="high_price", value=f'{coordy_high}$',coord=[coordx,coordy_high],symbol=symbol, symbol_size=30, itemstyle_opts=opts.ItemStyleOpts(color='#FF8C00',border_color="#FFD700") ) ], label_opts=opts.LabelOpts(font_size=18,color="#EB1934",font_weight="bold",position="right") ), )Copy the code

Here, the main attention is paid to linestyle and markpoint Settings.

3.2 Setting the Global Style

Line.set_global_opts(title_opts= opts.titleopts (title_opts= opts.titleopts (title=f" BTC daily closing price ({title_date})", pos_top="10%", pos_left="center", title_textstyle_opts=opts.TextStyleOpts(color="#0000FF", font_family='STKaiti', font_size=20), ), xaxis_opts=opts.AxisOpts( type_="time", max_=x_max, boundary_gap=False, axislabel_opts=opts.LabelOpts(color="#EB1934"), axisline_opts=opts.AxisLineOpts( is_show=True, linestyle_opts=opts.LineStyleOpts(width=2, color="#EB1934") ), axistick_opts=opts.AxisTickOpts( is_show=True, linestyle_opts=opts.LineStyleOpts(color="#EB1934"), ), ), yaxis_opts=opts.AxisOpts( type_="value", max_=y_max, position="left", axislabel_opts=opts.LabelOpts(color="#EB1934"), axisline_opts=opts.AxisLineOpts( is_show=True, linestyle_opts=opts.LineStyleOpts(width=2, color="#EB1934") ), axistick_opts=opts.AxisTickOpts( is_show=True, linestyle_opts=opts.LineStyleOpts(color="#EB1934"), ), splitline_opts=opts.SplitLineOpts( is_show=False, linestyle_opts=opts.LineStyleOpts(color="#EB1934") ), ), )Copy the code

Effect:

3.3 Adding a Background Image

graphic_opts=[ opts.GraphicImage( graphic_item=opts.GraphicItem( id_="logo", z=-10, bounding="raw", origin=[50, 100] ), Graphic_imagestyle_opts = opts. GraphicImageStyleOpts (image = "coin. JPG" width = 900, height = 600, opacity = 0.2,),),Copy the code

Effect:


END

The above is this issue for you to sort out all the content, hurry up to practice it, like friends can like, collect, you can also leave a message in the comment area to communicate with each other, I hope you can like.