This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Use Pyecharts for data visualization

You can also download the Pyecharts library package from the PyCharm software. Query the version after the download is successful

The import pyecharts print (pyecharts. __version__) 1.2.Copy the code

Pyecharts Chinese official website

Pyecharts can be viewed at pyecharts.org/#/zh-cn/int… .

The general method of use

The add() method is used to add data to the chart and set various configuration items.

Show_config () is used to print all the configuration items for the output chart

The render() method will generate a render. HTML file in the root directory by default, with support for the path parameter, setting the location of the file, such as render(r”e:my_first_chart.html”), and opening the file in the browser.

Note that * the default encoding type is UTF-8, which is fine in Python3, which has much better Support for Chinese. However, in Python2, encoding processing is a headache, and I haven’t found a perfect solution for the time being. At present, I can only do secondary coding by myself through the text editor. I use Visual Studio Code, first open it with Gbk Code, and then save it again with utf-8. If open with browser so, won’t appear Chinese garbled code problem.

The basic use

  • Chart_name = Type() Initializes a specific Type diagram.
  • Add () Adds data and configuration items.
  • Render () generates the.html file.

Use examples to solve real problems

1. Line chart and step chart of postage rate change in the United States from 1995 to 2009;

The data are as follows: Year: [” 1995 “, “1996”, “1997”, “1998”, “1999”, “2000”, “2001”, “2002”, “2003”, “2004”, “2005”, “2006”, “2007”, “2008”, “2009”] postage: [0.32, 0.32, 0.32, 0.32, 0.33, 0.33, 0.34, 0.37, 0.37, 0.37, 0.37, 0.39, 0.41, 0.42, 0.44] line chart code is as follows:

from pyecharts.charts import Line year= ["1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009"] postage = [0.32, 0.32, 0.32, 0.32, 0.33, 0.33, 0.34, 0.37, 0.37, 0.37, Tooltip_opts = opts.tooltipopts (is_show=False), xaxis_opts=opts.AxisOpts(type_="category"), yaxis_opts=opts.AxisOpts( type_="value", axistick_opts=opts.AxisTickOpts(is_show=True), splitline_opts=opts.SplitLineOpts(is_show=True), ), ) .add_xaxis(xaxis_data=year) .add_yaxis( series_name="", y_axis=postage, symbol="emptyCircle", is_symbol_show=True, label_opts=opts.LabelOpts(is_show=False), ) .render("basic_line_chart.html") )Copy the code

A basic_line_chart.html page will be generated in the same directory, and when opened, the results of this code will be displayed. (This is not shown, the same as below) Step diagram code is as follows:

from pyecharts.charts import Line year = ["1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009"] postage = [0.32, 0.32, 0.32, 0.32, 0.33, 0.33, 0.34, 0.37, 0.37, 0.37, 0.37, 0.39, 0.41, 0.42, 0.44] c = (Line () add_xaxis (xaxis_data = year) add_yaxis (" the United States in 1995-2009, postage, "y_axis = postage, Title_opts = opts.titleopts (title=" line_step ")).render("line_step ")).set_global_opts(title_opts= opts.titleopts (title=" line_step ")).render("line_step ")Copy the code

A line_step-html page will be generated in the same directory. When opened, the page will display the result of this code:

2. Stacked bar chart and polar coordinate system-stacked bar chart (Nightingale rose chart) of the top three winners of the Hot Dog Eating Competition from 2000 to 2010; Data file: hot-dog-places.csv Hot-dog-places.csv reads:

2000200 1200 2200 3200 4200 5200 6200 7200 8200 9201 25,50,50.5 0, 44.5, 53.5, 49,54,66,59,68,54 24,31,26,30.5, 38,37,52,63,59,64.5, 43 and 22,23.5, 25.5, 29.5, 32,32,37,49,42,55,37

The code of the stack bar chart is as follows:

From Pyecharts.charts import Bar import CSV filename="hot-dog places.csv" data_x=[] # Open file loop with open(filename) as f: reader = csv.reader(f) for data_row in reader: Y1 =data_x[1] y2=data_x[2] y3=data_x[3] c = (Bar().add_xaxis(x)) Add_yaxis (" 1 ", y1, stack = "walks"). The add_yaxis (" second ", y2, stack = "walks"). The add_yaxis (" third place ", y3, Stack ="stack1")# Set_series_opts (label_opts=opts.LabelOpts(is_show=False)) Title_opts = opts.titLEOPts (title=" bar-stack Bar chart ")).render("bar_stack0.html")).set_global_opts(title_opts= opts.titLEOPts (title=" bar-stack Bar chart ")).render("bar_stack0.html"))Copy the code

This will generate a webpage in the same directory as bar_stack0.html, and open the webpage to display the result of this code:

The code for the polar coordinate – stacked bar chart (Nightingale Rose) is as follows:

From Pyecharts.charts import Polar import CSV filename="hot-dog places.csv" data_x=[] # Open file loop with open(filename) as  f: reader = csv.reader(f) for data_row in reader: Y1 =data_x[1] y2=data_x[2] y3=data_x[3] c = (Polar() .add_schema(angleaxis_opts=opts.AngleAxisOpts(data=x, type_="category")) .add("A", y1, type_="bar", stack="stack0") .add("B", y2, type_="bar", stack="stack0") .add("C", y3, type_="bar", Title_opts = opts.titLEOPts (titLE_opts = opts.titleopts (title=" pole coordinate - stack column chart ")) .render(" Polar coordinates - Stacked bar Chart (Nightingale Rose chart).html"))Copy the code

Open the web page to display the operation result of this code:

The code for the polar coordinate – stack bar chart is the same as above, but the code following c needs to be changed to the following:

Polar() .add_schema( radiusaxis_opts=opts.RadiusAxisOpts(data=x, type_="category"), angleaxis_opts=opts.AngleAxisOpts(is_clockwise=True, max_=200), ) .add("A", y1, type_="bar", stack="stack1") .add("B", y2, type_="bar", stack="stack1") .add("C", y3, type_="bar", Title_opts = opts.titLEOPts (title=" pole coordinate - stack chart ").set_global_opts(titLE_opts = opTS.titLEOPts (title=" pole coordinate - stack chart ")) .set_series_opts(label_opts= opts.labelopts (is_show=True)).render(" polar coordinate - stack column.html "))Copy the code

Open the web page to display the operation result of this code:

3. Draw pie charts and ring charts for the voting results of the field that users are interested in on a website; Data file: vote_result.csv Vote_result.csv

Fields of interest, vote Finance,172 Health care,136 Marketing,135 Retail,101 Manufacturing,80 Justice,68 Engineering and Science,50 Insurance,29 Others,41

The pie chart code is as follows:

From Pyecharts.charts import Pie import CSV filename="vote_result.csv" data_x=[] encoding='UTF-8') as f: reader = csv.reader(f) for data_row in reader: data_x.append(data_row) b=[] c=[] for index,values in enumerate(data_x): if(index>0): B. append(values[0]) c. Append (values[1]) x=data_x[0] d = (Pie().add("", [list(z) for z in zip(b, C)], center = "35%", "50%"],). Set_global_opts (title_opts = opts. TitleOpts (title = "vote pie"), legend_opts=opts.LegendOpts(pos_left="15%"), ) .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) .render("pie_position.html") )Copy the code

Open the web page to display the operation result of this code:

The code of the ring diagram is as follows:

From Pyecharts.charts import Pie import CSV filename="vote_result.csv" data_x=[] encoding='UTF-8') as f: reader = csv.reader(f) for data_row in reader: data_x.append(data_row) b=[] c=[] for index,values in enumerate(data_x): if(index>0): b.append(values[0]) c.append(values[1]) d = ( Pie() .add( "", [list(z) for z in zip(b, c)], radius=["40%", "75%"], Title_opts = opts.titleopts (title=" ring "), legend_opts= opts.legendopts (Orient ="vertical"), legend_opts= opts.legendopts (Orient ="vertical"), legend_opts= opts.legendopts (Orient ="vertical"), legend_opts= opts.legendopts (Orient ="vertical") pos_top="15%", pos_left="2%"), ) .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}").render(" {c}");Copy the code

Open the web page to display the operation result of this code:

4. A stacked bar chart of poll results for Obama’s political initiatives; The data file: approval_rash.csv inside the approval_rash.csv file says:

Political initiatives, for, against, don’t speak out racial issues,52,38,10 education,49,40,11 terrorism,48,45,7 energy policy,47,42,11 foreign affairs,44,48,8 environment,43,51,6 religious policy,41,53,6 taxes,41,54,5 Health care policy,40,57,3 economics,38,59,3 employment policy,36,57,7 trade policy,31,64,5 immigration,29,62,9

The code of the stack bar chart is as follows:

From Pyecharts.charts import Bar import CSV filename=" approval_rt.csv "data_x=[] # open file loop with open(filename,'r', encoding='UTF-8') as f: reader = csv.reader(f) for data_row in reader: B =[] c=[] d=[] e=[] for index,values in enumerate(data_x): if(index>0): b.append(values[0]) c.append(values[1]) d.append(values[2]) e.append(values[3]) elif(index==0): x.append(values) print(b) c = ( Bar() .add_xaxis(b) .add_yaxis(x[0][1], c, stack="stack1") .add_yaxis(x[0][2], d, Add_yaxis (x[0][3], e, stack="stack1") Set_series_opts (label_opts=opts.LabelOpts(is_show=False)) .set_global_opts(title_opts= opts.titleopts (title=" bar-stack Bar chart ")).render(" Political action poll results.html "))Copy the code

Open the web page to display the operation result of this code:

I am white and white I, a love to share knowledge of the program yuan ❤️

If you have no experience in programming, you can read this blog and find that you don’t know or want to learn Python, you can directly leave a message or private me.