Data visualization - draw a simple line chart with python

insert image description here

foreword

We have already learned the basic grammar and object-oriented of python, then we will learn the excellence of python programming language - the line chart of data visualization.

insert image description here

JSON

When it comes to data visualization, we need to know what JSON is.

  • JSON is a lightweight data interaction format that can organize and encapsulate data in the format specified by JSON
  • json is essentially a string with a specific format

The main functions of JSON are:

  1. Data serialization and transmission: JSON can convert complex data structures (such as objects and arrays) into string form for transmission over the network. It is cross-platform compatible and can be parsed and generated in different programming languages.

  2. Human-readable data format: JSON uses a concise text format that is easy to read and understand. It uses key/value pairs to represent data, and curly braces ({}) to represent objects and square brackets ([]) to represent arrays.

  3. Object representation: JSON supports object representation, which can represent complex data structures in the form of key/value pairs. Keys are strings and values ​​can be strings, numbers, booleans, objects, arrays, or null.

  4. Array representation: JSON supports array representation, which can contain multiple values ​​in square brackets, separated by commas. Arrays can be nested, allowing the storage of multi-level data structures.

  5. Data exchange and storage: JSON is widely used for data exchange and storage. It is a common data format for many APIs and services, making it easy to transfer data from one application to another.

  6. Cross-language support: JSON can be parsed and generated in different programming languages, so data can be easily shared and processed on different platforms.
    insert image description here

This is convenient because the JSON data type uses curly braces { }for objects and square brackets for arrays, which behave the same way in python as dictionaries and lists.[ ]

Conversion of python data and JSON data To convert python data and JSON data, we need to use the methods and methods in the
python module .jsondumpsloads

  • json.dumps(data) convert python data to JSON data
  • json.loads(data) convert JSON data to python data
import json

data = [{
    
    'name':'张三','age':18},{
    
    'name':'李四','age':20}]
json_str = json.dumps(data)
print(type(json_str))
result = json.loads(json_str)
print(type(result))

insert image description here
Since JSON is a string with a specific format, its type is strthe type.

Use the pyecharts module to draw a line chart

Download pyecharts module

We need to download the pyecharts module first.

Download the module cmdusing the command in .pip install pyechartspyecharts
insert image description here

And pyecharts also provides an official website https://pyecharts.org/#/en-us/ so we can learn about relevant knowledge on the official website.
insert image description here

On the pyecharts-gallery website, we can see many drawn data visualization graphics with reference codes. https://gallery.pyecharts.org/#/README_EN

insert image description here

Use the pyecharts module to draw a simple line chart

We need to use the method under the module pyechartsunder the package .chartsLine

from pyecharts.charts import Line

# 得到折线图对象
line = Line()

# 添加横坐标数据
line.add_xaxis(["中国","美国","日本"])

# 添加纵坐标数据
line.add_yaxis("GDP",[30,20,10])

# 生成图标
line.render()

But after we run the code, a file is generated render.html.
insert image description here
insert image description here

Add configuration options

Although the line graph is drawn in this way, we can see that the graph is relatively monotonous, so we can have some configuration options.

Two configuration options commonly used by pyecharts:

  • global configuration options
  • Series Configuration Options

Then our article mainly adds global configuration options.

Use set_global_optsfor global configuration. We can see what configurable global options are available on the official website.
insert image description here

These are the global options we often use.

insert image description here
These configuration methods are all in pyecharts.optionsthe module, we need to import the methods under the corresponding module.

from pyecharts.options import TitleOpts,LegendOpts,ToolboxOpts,VisualMapOpts

We can also use CTRL + Pto view the parameter list
insert image description here

from pyecharts.charts import Line
from pyecharts.options import TitleOpts,LegendOpts,ToolboxOpts,VisualMapOpts

# 得到折线图对象
line = Line()

# 添加横坐标数据
line.add_xaxis(["中国","美国","日本"])

# 添加纵坐标数据
line.add_yaxis("GDP",[30,20,10])

# 添加全局配置选项
line.set_global_opts(title_opts=TitleOpts(title="GDP展示",pos_left="center",pos_bottom="1%"),  # 配置标题
                     legend_opts=LegendOpts(is_show=True),  # 配置图例,默认是展示的
                     toolbox_opts=ToolboxOpts(is_show=True),  # 配置工具箱
                     visualmap_opts=VisualMapOpts(is_show=True))  # 配置视觉映射

# 生成图标
line.render()

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/m0_73888323/article/details/131788973