Article Directory
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.
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:
-
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.
-
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.
-
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.
-
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.
-
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.
-
Cross-language support: JSON can be parsed and generated in different programming languages, so data can be easily shared and processed on different platforms.
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 .json
dumps
loads
- 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))
Since JSON is a string with a specific format, its type is str
the type.
Use the pyecharts module to draw a line chart
Download pyecharts module
We need to download the pyecharts module first.
Download the module cmd
using the command in .pip install pyecharts
pyecharts
And pyecharts also provides an official website https://pyecharts.org/#/en-us/ so we can learn about relevant knowledge on the official website.
On the pyecharts-gallery website, we can see many drawn data visualization graphics with reference codes. https://gallery.pyecharts.org/#/README_EN
Use the pyecharts module to draw a simple line chart
We need to use the method under the module pyecharts
under the package .charts
Line
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
.
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_opts
for global configuration. We can see what configurable global options are available on the official website.
These are the global options we often use.
These configuration methods are all in pyecharts.options
the module, we need to import the methods under the corresponding module.
from pyecharts.options import TitleOpts,LegendOpts,ToolboxOpts,VisualMapOpts
We can also use CTRL + P
to view the parameter list
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()