Pyecharts is a powerful data visualization tool that combines Python with Echarts.


preface





The installation


[Python]

Plain text view
Copy the code

?
1
pip install pyecharts


Now let’s start using PyCharts officially. Here we directly use official data:



Histogram – Bar

[Python]

Plain text view
Copy the code

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
/
/
Import the bar chart
-
Bar
from
pyecharts
import
Bar
/
/
Set the name
columns
=
[
"Jan"
.
"Feb"
.
"Mar"
.
"Apr"
.
"May"
.
"Jun"
.
"Jul"
.
"Aug"
.
"Sep"
.
"Oct"
.
"Nov"
.
"Dec"
]
/
/
Set up the data
data1
=
[
2.0
.
4.9
.
7.0
.
23.2
.
25.6
.
76.7
.
135.6
.
162.2
.
32.6
.
20.0
.
6.4
.
3.3
]
data2
=
[
2.6
.
5.9
.
9.0
.
26.4
.
28.7
.
70.7
.
175.6
.
182.2
.
48.7
.
18.8
.
6.0
.
2.3
]
/
/
Sets the main title and subtitle of the bar chart
bar
=
Bar(
"Bar chart"
.
"Annual precipitation and evaporation."
)
/
/
Add bar chart data and configuration items
bar.add(
"Precipitation"
, columns, data1, mark_line
=
[
"average"
], mark_point
=
[
"max"
.
"min"
])
bar.add(
"Evaporation"
, columns, data2, mark_line
=
[
"average"
], mark_point
=
[
"max"
.
"min"
])
/
/
Generate a local file (default.html file)
bar.render()












The Pie chart – Pie


[Python]

Plain text view
Copy the code

?
01
02
03
04
05
06
07
08
09
10
/
/
Import Pie chart Pie
from
pyecharts
import
Pie
/
/
Set the main title and subtitle, center the title, and set the width to
900
pie
=
Pie(
"Pie chart"
.
"Annual precipitation and evaporation."
,title_pos
=
'center'
,width
=
900
)
/
/
Add data and set the coordinate position to [
25
.
50
], the colums option above is undisplayed
pie.add(
"Precipitation"
, columns, data1 ,center
=
[
25
.
50
],is_legend_show
=
False
)
/
/
Add data and set the coordinate position to [
75
.
50
], the colums option is undisplayed and the label label is displayed
pie.add(
"Evaporation"
, columns, data2 ,center
=
[
75
.
50
],is_legend_show
=
False
,is_label_show
=
True
)
/
/
Save the diagram
pie.render()







Box figure – Boxplot


[Python]

Plain text view
Copy the code

?
1
2
3
4
5
6
7
8
9
/
/
Import Boxplot
from
pyecharts
import
Boxplot
boxplot
=
Boxplot(
"Box diagram"
.
"Annual precipitation and evaporation."
)
x_axis
=
[
'Precipitation'
.
'Evaporation'
]
y_axis
=
[data1,data2]
/
/
The prepare_data method converts data to nested [
min
, Q1, median (
or
Q2), Q3,
max
]
yaxis
=
boxplot.prepare_data(y_axis)
boxplot.add(
"Weather Statistics"
, x_axis, _yaxis)
boxplot.render()







The Line chart – Line


[Python]

Plain text view
Copy the code

?
1
2
3
4
5
6
from
pyecharts
import
Line
line
=
Line(
"Line chart"
.
"Annual precipitation and evaporation."
)
/
/
Is_label_show is to set whether the above data is displayed
line.add(
"Precipitation"
, columns, data1, is_label_show
=
True
)
line.add(
"Evaporation"
, columns, data2, is_label_show
=
True
)
line.render()



















conclusion


  • Import the related chart package
  • Perform basic chart Settings to create chart objects
  • Data entry and chart setting using the add() method (print_echarts_options() can be used to print all configurable items)
  • Use the Render () method to save the chart











More technical information can be obtained from itheimaGZ