Overview of Pyecharts
Pyechart is a Python third-party library for generating Echarts charts (Echarts is an open source visual chart library based on Javascript).
1.1 Pyecharts features
According to the official documentation, Pyecharts has the following features:
- Simple API design, silky smooth use, support chain call
- Contains 30+ common charts, you can find everything
- Support mainstream Notebook environment, Jupyter Notebook and JupyterLab
- Easy to integrate into Flask, Django, and other mainstream Web frameworks
- Highly flexible configuration items can be easily matched to create beautiful charts
- Detailed documentation and examples to help developers get started faster
- Up to 400+ map files and native Baidu map provide strong support for geographic data visualization
Pyecharts is available at pyecharts.org/#/
1.2 Introduction to Pyecharts
1.2.1 installation Pyecharts
Install Pyecharts via PIP
$ pip install pyecharts
After installing Pyecharts, you can run the following command to check whether Pyecharts is installed successfully
$ pip list
Copy the code
1.2.2 Introduction to Pyecharts
from pyecharts.charts import Bar
Version v1 began to support chained calls
bar = (
Bar()
.add_xaxis(['shirt'.'sweaters'.'pants'])
.add_yaxis(Merchants' A '[10.20.30])
.add_yaxis(Merchants' B '[30.20.10]))# render generates a local HTML file with the default current directory and the name of the file called render.html
# You can also pass in arguments such as bar.render('mycharts.html')
bar.render()
Copy the code
Running the above program will generate a render. HTML in the current.py file directory. Open render.
Pyecharts configuration items
Pyecharts consists of global configuration items and series configuration items. You can customize the contents displayed in the Charts charts by using these configuration items.
2.1 Global Configuration Items
The global configuration can passset_global_opts
Method, as shown in the following figure. You can customize the title, legend, prompt box, toolbox and other contents of the Charts chart by using global configuration items.
2.1.1 Use of global Configuration Items
The use of global configuration items is generally the same, so let’s take TitleOpts title Settings as an example to explain how to use these Settings. First, you can learn about the Settings supported by TitleOpts:
class TitleOpts(
# main heading text, use \n newline supported.
title: Optional[str] = None.# main title jump URL link
title_link: Optional[str] = None.# Main title jump link way
The default value is blank
# Optional arguments: 'self', 'blank'
# 'self' current window opens; 'blank' opens a new window
title_target: Optional[str] = None.# subtitle text, use \n newline supported.
subtitle: Optional[str] = None.# subtitle jump URL link
subtitle_link: Optional[str] = None.# subtitle jump link way
The default value is blank
# Optional arguments: 'self', 'blank'
# 'self' current window opens; 'blank' opens a new window
subtitle_target: Optional[str] = None.The distance between the title component and the left side of the container.
The # left value can be a specific pixel value like 20, it can be a percentage like '20%' relative to the height and width of the container,
# can also be 'left', 'center', 'right'.
# If the value of left is 'left', 'center', 'right', the component will be automatically aligned according to the corresponding position.
pos_left: Optional[str] = None.# Title The distance between the component and the right side of the container.
The value of # right can be a specific pixel value like 20, or a percentage of the height and width of the container like '20%'.
pos_right: Optional[str] = None.The distance between the title component and the top side of the container.
The value of # top can be a specific pixel value like 20, it can be a percentage like '20%' relative to the height and width of the container,
# can also be 'top', 'middle', 'bottom'.
# If the value of top is 'top', 'middle', 'bottom', the component is automatically aligned according to the corresponding position.
pos_top: Optional[str] = None.# Title The distance between the component and the bottom of the container.
The value of # bottom can be a specific pixel value like 20, or a percentage like '20%' relative to the height and width of the container.
pos_bottom: Optional[str] = None.# title margin, unit: px, default margin in each direction is 5, accept array set to top, right, bottom, left margin respectively.
# // Set the inner margin to 5
# padding: 5
# // Set the top and bottom margins to 5 and the left and right margins to 10
# padding: [5, 10]
# // Set the inner margins in each of the four directions
# padding: [
# 5, / /
# 10, / / right
# 5, / /
# 10, / / to the left
#]
padding: Union[Sequence, Numeric] = 5.# Space between main and subheadings.
item_gap: Numeric = 10.# main header font style configuration item, see 'series_options.TextStyleOpts'
title_textstyle_opts: Union[TextStyleOpts, dict.None] = None.# subtitle font style configuration item, see 'series_options.TextStyleOpts'
subtitle_textstyle_opts: Union[TextStyleOpts, dict.None] = None.)
Copy the code
As mentioned above, global configuration items are set by set_global_opts. In other words, when set_global_opts is called, title_OPts can be added into its input parameter to customize some titles for the Charts chart. Set_global_opts set_global_opts
def set_global_opts(
self,
title_opts: types.Title = opts.TitleOpts(),
legend_opts: types.Legend = opts.LegendOpts(),
tooltip_opts: types.Tooltip = None,
toolbox_opts: types.Toolbox = None,
brush_opts: types.Brush = None,
xaxis_opts: types.Axis = None,
yaxis_opts: types.Axis = None,
visualmap_opts: types.VisualMap = None,
datazoom_opts: types.DataZoom = None,
graphic_opts: types.Graphic = None,
axispointer_opts: types.AxisPointer = None.) :
Copy the code
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
Bar()
.add_xaxis(['shirt'.'sweaters'.'pants'])
.add_yaxis(Merchants' A '[10.20.30])
.add_yaxis(Merchants' B '[30.20.10])
.set_global_opts(
title_opts=opts.TitleOpts(
title='This is title'
)
)
)
bar.render()
Copy the code
2.1.2 Overview of Global Configuration Items
- When calling set_global_opts, you can add global configuration items. For the supported global configuration items, refer to the set_global_opts source code.
- You can use the official documentation or the source code to find out what these global configuration items support, as well as the source code.
- Note the data type of the input. Not all global configuration entries have input types of STR.
2.2 Series Configuration Items
Series configuration can be set using set_series_opts. You can configure text and primitives using series configuration items.
2.2.1 Use of series configuration items
ItemStyleOpts is an ItemStyleOpts primitive setting item. The ItemStyleOpts primitive setting item is an ItemStyleOpts primitive setting item.
class ItemStyleOpts(
# Color of the graph.
# color can be RGB, such as' RGB (128, 128, 128)', if you want to add alpha channel opacity,
# can be RGBA, such as' RGBA (128, 128, 128, 0.5)', or hexadecimal, such as '# CCC '.
Color support for gradient and texture fill in addition to solid color
#
# Linear gradient, the first four parameters are x0, y0, x2, y2, ranging from 0 to 1, equivalent to the percentage in the graph bounding box,
# If globalCoord is' true ', the four values are absolute pixel positions
# color: {
# type: 'linear',
# x: 0,
# y: 0,
# x2: 0,
# y2: 1,
# colorStops: [{
# offset: 0, color: 'red' // 0
#}, {
# offset: 1, color: 'blue' // 100% of the color
#}].
# global: false // Default is false
#}
#
The first three parameters are center x, y and radius respectively. The values are the same as linear gradient
# color: {
# type: 'radial',
# x: 0.5,
# y: 0.5,
# r: 0.5.
# colorStops: [{
# offset: 0, color: 'red' // 0
#}, {
# offset: 1, color: 'blue' // 100% of the color
#}].
# global: false // Default is false
#}
#
# Texture fill
# color: {
# image: imageDom, // support for HTMLImageElement, HTMLCanvasElement, path string is not supported
# repeat: 'repeat' // Can be 'repeat x', 'repeat y', 'no-repeat'
#}
color: Optional[str] = None.# The color of the shadow line graph.
color0: Optional[str] = None.The stroke color of the graph. The supported color format is the same as color, and the callback function is not supported.
border_color: Optional[str] = None.The stroke color of the shadow line graph.
border_color0: Optional[str] = None.Stroke width, default no stroke.
border_width: Optional[Numeric] = None.'2011-2015' > > >
border_type: Optional[str] = None.# Graphic transparency. The number ranges from 0 to 1. If the number is 0, the graph is not drawn.
opacity: Optional[Numeric] = None.# Region color.
area_color: Optional[str] = None.)
Copy the code
The specific usage is as follows:
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
Bar()
.add_xaxis(['shirt'.'sweaters'.'pants'])
.add_yaxis(Merchants' A '[10.20.30])
.add_yaxis(Merchants' B '[30.20.10])
.set_series_opts(
itemstyle_opts=opts.ItemStyleOpts(
color='#0066FF'
)
)
)
bar.render()
Copy the code
It can be seen from the charts chart above that the color of the charts chart has changed, and we can also find a problem that the color of each series is the same. Such charts may not meet our expected results. Since it is a series configuration item, we can focus onadd_yaxis
As you can see, the method also has oneitemstyle_opts
Therefore, you can also set the parameters as follows:
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
Bar()
.add_xaxis(['shirt'.'sweaters'.'pants'])
.add_yaxis(Merchants' A '[10.20.30], itemstyle_opts=opts.ItemStyleOpts(color='#5edd55'))
.add_yaxis(Merchants' B '[30.20.10], itemstyle_opts=opts.ItemStyleOpts(color='#33c0eb'))
)
bar.render()
Copy the code
2.2.2 Summary of Series Configuration Items
Series configuration items are used in the same way as global configuration items:
- When calling set_series_opts, you can append corresponding global configuration items. For the supported global configuration items, refer to the set_series_opts source code.
- You can use the official documentation or the source code to find out what these global configuration items support, as well as the source code.
- Note the data type of the input. Not all global configuration entries have input types of STR.
- Series configuration items can be added to the Charts series.
Summary of Pyecharts
For generating charts, there are no more than two steps: 1) Obtain the data, 2) generate the charts of the data, and Pyecharts focuses on the second step. After understanding pyecharts’ global configuration items and series of configuration items, we can say that we have basically mastered the use of Pyecharts. Other types of Pyecharts charts are no longer demonstrated and are fully explained in the official documentation.