Some graphics visualizations are based on imported CSV files
Experimental environment:
-
windows 10
-
VScode
-
Python (3.7.4)
-
Pyecharts (1.7.1)
Preparations:
Since some graphics are drawn using an imported CSV file, here is how to import a CSV file
# Method 1:
# import packages
import csv
Filename.csv is used here
filedata = "Filename.csv"
Create an empty list
data_x = []
# Open file
with open(filedata) as h:
Read file data
reader = csv.reader(h)
Read data in a row loop
for data_row in reader:
Form a list of data to append
data_x.append(data_row)
Read the first line of the list and assign it to x
x = data_x[0]
Convert the elements in the data list to single-precision floating-point type and assign the value to y
y = [float(i) for i in data_x[1]]
Copy the code
Most of the CSV files we need to visualize are in Chinese, but the encoding mode of the Chinese files is not the ASCII code that the computer can recognize. In this case, using data directly will cause an error. Therefore, we need to specify how to encode the CSV file. While method one can be troublesome to specify, here is method two:
# Method 2:
# import packages
import pandas as pd
Read the file and specify how to encode the file
filedata = pd.read_csv("Filename.csv", encoding = 'utf-8')
Read all the data in the first column, convert it to a list and assign it to k
k = vote.iloc[:, 0].tolist()
Copy the code
Case: Draw the line chart and ladder chart of postage changes in the United States from 1995 to 2009;
Data:
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]
The line chart:
Ideas:
- Import the library for drawing polylines
- Write data
- visualization
- beautify
Steps:
Import the library package
import pyecharts.options as opts
from pyecharts.charts import Line
Write data
years = ["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()
# global configuration
.set_global_opts()
# x
.add_xaxis(xaxis_data=years)
# y
.add_yaxis(
series_name="".The name of the collection to which the data belongs
y_axis=Postage, # Y-axis data
)
Output as an HTML file
.render("line_chart.html"))Copy the code
# global configuration
.set_global_opts(
# titles
title_opts=opts.TitleOpts(
title="Ladder chart of Postage Rate Changes in the United States, 1995-2009", subtitle="Li Houwen"),
# Open the prompt box
tooltip_opts=opts.TooltipOpts(is_show=True),
Axis types
xaxis_opts=opts.AxisOpts(type_="category"), # category axis
yaxis_opts=opts.AxisOpts(
type_="value".# number line
# Display the axis scale
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),),)# y
.add_yaxis(
series_name="".The name of the collection to which the data belongs
y_axis=Postage, # Y-axis data
is_symbol_show=True.# Hover value display with mouse
label_opts=opts.LabelOpts(is_show=False), # the Y-axis value is not displayed at the point
is_step=False.# Close the ladder diagram
)
Copy the code
Import the library package
import pyecharts.options as opts
from pyecharts.charts import Line
Write data
years = ["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()
# global configuration
.set_global_opts(
# set the title
title_opts=opts.TitleOpts(
title="Ladder chart of Postage Rate Changes in the United States, 1995-2009", subtitle="Li Houwen"),
# Open the prompt box
tooltip_opts=opts.TooltipOpts(is_show=True),
Axis types
xaxis_opts=opts.AxisOpts(type_="category"), # category axis
yaxis_opts=opts.AxisOpts(
type_="value".# number line
# Display the axis scale
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),),)# x
.add_xaxis(xaxis_data=years)
# y
.add_yaxis(
series_name="".The name of the collection to which the data belongs
y_axis=Postage, # Y-axis data
is_symbol_show=True.# Hover value display with mouse
label_opts=opts.LabelOpts(is_show=False), # the Y-axis value is not displayed at the point
is_step=False.# Close the ladder diagram
)
Output as an HTML file
.render("line_chart.html"))Copy the code
Effect:
Ladder diagram:
Ideas:
On the basis of the original line chart, open the ladder chart
Steps:
# y
.add_yaxis(
is_step=True.# Open the ladder diagram
)
Copy the code
Import the library package
import pyecharts.options as opts
from pyecharts.charts import Line
Write data
years = ["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()
# global configuration
.set_global_opts(
# set the title
title_opts=opts.TitleOpts(
title="Ladder chart of Postage Rate Changes in the United States, 1995-2009", subtitle="Li Houwen"),
# Open the prompt box
tooltip_opts=opts.TooltipOpts(is_show=True),
Axis types
xaxis_opts=opts.AxisOpts(type_="category"), # category axis
yaxis_opts=opts.AxisOpts(
type_="value".# number line
# Display the axis scale
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),),)# x
.add_xaxis(xaxis_data=years)
# y
.add_yaxis(
series_name="".The name of the collection to which the data belongs
y_axis=Postage, # Y-axis data
is_symbol_show=True.# Hover value display with mouse
label_opts=opts.LabelOpts(is_show=False), # the Y-axis value is not displayed at the point
is_step=True.# Open the ladder diagram
)
Output as an HTML file
.render("line_chart.html"))Copy the code
Effect:
Case: Stacked bar chart, polar coordinate system – Stacked bar chart (Nightingale rose Chart) of the top three winners in hot Dog Eating Competition from 2000 to 2010
Data file: hot-dog-places.csv
Stack bar diagram:
Ideas:
- Import the library for drawing bar charts
- Write data
- visualization
- beautify
Steps:
# import libraries
import pyecharts.options as opts
from pyecharts.charts import Bar
import csv
# read file
hot_dog_places = "hot-dog-places.csv"
data_x = []
with open(hot_dog_places) as h: # Open file
reader = csv.reader(h) Read the data in the file
for data_row in reader: Read data in a row loop
data_x.append(data_row) Form a list of data to append
x = data_x[0]
y1 = [float(i) for i in data_x[1]] Convert the elements in the data list to single-precision floating-point type and assign the value to y1
y2 = [float(i) for i in data_x[2]]
y3 = [float(i) for i in data_x[3]]
(
Bar()
# titles
.set_global_opts(title_opts=opts.TitleOpts(title="Top three finishes in the Hot dog Eating contest.", subtitle="Li Houwen"))
# x
.add_xaxis(x)
# Y-axis data is stackable with 50% spacing between each column
.add_yaxis("Number one", y1, stack="stack1", category_gap="50%")
.add_yaxis("Second place", y2, stack="stack1", category_gap="50%")
.add_yaxis("Third place", y3, stack="stack1", category_gap="50%")
.render("Bar.html"))Copy the code
# import libraries
from pyecharts.globals import ThemeType
(
# Change the theme to LIGHT
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
)
Copy the code
# import libraries
import pyecharts.options as opts
from pyecharts.charts import Bar
import csv
from pyecharts.globals import ThemeType
# read file
hot_dog_places = "hot-dog-places.csv"
data_x = []
with open(hot_dog_places) as h: # Open file
reader = csv.reader(h) Read the data in the file
for data_row in reader: Read data in a row loop
data_x.append(data_row) Form a list of data to append
x = data_x[0]
y1 = [float(i) for i in data_x[1]] Convert the elements in the data list to single-precision floating-point type and assign the value to y1
y2 = [float(i) for i in data_x[2]]
y3 = [float(i) for i in data_x[3]]
(
# Change the theme to LIGHT
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
# titles
.set_global_opts(title_opts=opts.TitleOpts(title="Top three finishes in the Hot dog Eating contest.", subtitle="Li Houwen"))
# x
.add_xaxis(x)
# Y-axis data is stackable with 50% spacing between each column
.add_yaxis("Number one", y1, stack="stack1", category_gap="50%")
.add_yaxis("Second place", y2, stack="stack1", category_gap="50%")
.add_yaxis("Third place", y3, stack="stack1", category_gap="50%")
.render("Bar.html"))Copy the code
Effect:
Nightingale roses:
Ideas:
A Nightingale rose is also called a polar coordinate system – a stacked histogram
This can be done literally by placing the stacked bar graph in polar coordinates
Steps:
# import libraries
import pyecharts.options as opts
from pyecharts.charts import Polar
import csv
# read file
hot_dog_places = "hot-dog-places.csv"
data_x = []
with open(hot_dog_places) as h: # Open file
reader = csv.reader(h) Read the data in the file
for data_row in reader: Read data in a row loop
data_x.append(data_row) Form a list of data to append
x = data_x[0]
y1 = [float(i) for i in data_x[1]] Convert the elements in the data list to single-precision floating-point type and assign the value to y1
y2 = [float(i) for i in data_x[2]]
y3 = [float(i) for i in data_x[3]]
(
Polar()
.add_schema()
# titles
.set_global_opts(title_opts=opts.TitleOpts(title="hot-dog-places", subtitle="Li Houwen"))
# display prompt
.set_series_opts(label_opts=opts.LabelOpts(is_show=True))
The data type is stacked column
.add("Number one", y1, stack="stack2", type_="bar")
.add("Second place", y2, stack="stack2", type_="bar")
.add("Third place", y3, stack="stack2", type_="bar")
.render("Polar.html"))Copy the code
(
Polar()
.add_schema(
# Polar coordinate system radial axis configuration
radiusaxis_opts=opts.RadiusAxisOpts(
data=x, type_="category"), Set the axis type
angleaxis_opts=opts.AngleAxisOpts(is_clockwise=True, max_=200)))Copy the code
# import libraries
import pyecharts.options as opts
from pyecharts.charts import Polar
import csv
# read file
hot_dog_places = "hot-dog-places.csv"
data_x = []
with open(hot_dog_places) as h: # Open file
reader = csv.reader(h) Read the data in the file
for data_row in reader: Read data in a row loop
data_x.append(data_row) Form a list of data to append
x = data_x[0]
y1 = [float(i) for i in data_x[1]] Convert the elements in the data list to single-precision floating-point type and assign the value to y1
y2 = [float(i) for i in data_x[2]]
y3 = [float(i) for i in data_x[3]]
(
Polar()
.add_schema(
# Polar coordinate system radial axis configuration
radiusaxis_opts=opts.RadiusAxisOpts(
data=x, type_="category"), Set the axis type
angleaxis_opts=opts.AngleAxisOpts(is_clockwise=True, max_=200),# titles
.set_global_opts(title_opts=opts.TitleOpts(title="hot-dog-places", subtitle="Li Houwen"))
# display prompt
.set_series_opts(label_opts=opts.LabelOpts(is_show=True))
The data type is stacked column
.add("Number one", y1, stack="stack2", type_="bar")
.add("Second place", y2, stack="stack2", type_="bar")
.add("Third place", y3, stack="stack2", type_="bar")
.render("Polar.html"))Copy the code
Effect:
Case: a website users interested in the voting results of the field to draw a pie chart, ring chart
Data file: vote_result.csv
The pie chart:
Ideas:
- Import the library for drawing pie charts
- Write data
- visualization
- beautify
Steps:
# import libraries
from pyecharts import options as opts
from pyecharts.charts import Pie
import pandas as pd
Select 'utF-8' from 'utF-8'
vote = pd.read_csv("vote_result.csv", encoding = 'utf-8')
Read data and assign values
k = vote.iloc[:, 0].tolist() Read all of the first column and convert it to a list
v = vote.iloc[:, 1].tolist()
(
Pie()
# titles
.set_global_opts(
title_opts=opts.TitleOpts(title="Users are interested", subtitle="Li Houwen"))# Hover the mouse over each pie to display the data format
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
# list to dictionary
.add("", [list(z) for z in zip(k, v)])
.render("Pie.html"))Copy the code
Effect:
Circular diagram:
Ideas:
The circle graph is cut in the middle of the pie, so consider reducing the internal and external radius of the original pie
Steps:
# import libraries
from pyecharts import options as opts
from pyecharts.charts import Pie
import pandas as pd
Select 'utF-8' from 'utF-8'
vote = pd.read_csv("vote_result.csv", encoding = 'utf-8')
Read data and assign values
k = vote.iloc[:, 0].tolist() Read all of the first column and convert it to a list
v = vote.iloc[:, 1].tolist()
(
Pie()
# titles
.set_global_opts(
title_opts=opts.TitleOpts(title="Users are interested", subtitle="Li Houwen"))# Hover the mouse over each pie to display the data format
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
# List to dictionary, narrow inside and outside radius
.add("", [list(z) for z in zip(k, v)],
radius=["50%"."70%"],)
.render("Pie.html"))Copy the code
Effect:
Case study: A stacked bar chart of polling results on Obama’s political initiatives
Data file: approval_rate.csv
Stack column Figure 2:
The CSV file uses the encoding format ‘UTF-8 ‘, so method two is used to import
Ideas:
- Import the library for drawing columns
- Write data
- visualization
- beautify
Steps:
# import libraries
import pyecharts.options as opts
from pyecharts.charts import Bar
import pandas as pd
Select 'utF-8' from 'utF-8'
approval = pd.read_csv("approval_rate.csv", encoding='utf-8')
Read data and assign values
x = approval.iloc[:, 0].tolist() Read all of the first column and convert it to a list
y1 = approval.iloc[:, 1].tolist()
y2 = approval.iloc[:, 2].tolist()
y3 = approval.iloc[:, 3].tolist()
(
Bar()
# titles
.set_global_opts(title_opts=opts.TitleOpts(title="Obama's Political initiatives poll Results.", subtitle="Li Houwen"))
# x
.add_xaxis(x)
# Y axis data is stackable, with 50% space between each column. Y axis values are not displayed on the graph
.add_yaxis("Support", y1, stack="stack1", category_gap="50%", label_opts=opts.LabelOpts(is_show=False),)
.add_yaxis("Against", y2, stack="stack1", category_gap="50%", label_opts=opts.LabelOpts(is_show=False),)
.add_yaxis("No opinion.", y3, stack="stack1", category_gap="50%", label_opts=opts.LabelOpts(is_show=False),)
.render("Bar2.html"))Copy the code
# import libraries
from pyecharts.globals import ThemeType
(
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
)
Copy the code
# import libraries
import pyecharts.options as opts
from pyecharts.charts import Bar
from pyecharts.globals import ThemeType
import pandas as pd
Select 'utF-8' from 'utF-8'
approval = pd.read_csv("approval_rate.csv", encoding='utf-8')
Read data and assign values
x = approval.iloc[:, 0].tolist() Read all of the first column and convert it to a list
y1 = approval.iloc[:, 1].tolist()
y2 = approval.iloc[:, 2].tolist()
y3 = approval.iloc[:, 3].tolist()
(
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
# titles
.set_global_opts(title_opts=opts.TitleOpts(title="Obama's Political initiatives poll Results.", subtitle="Li Houwen"))
# x
.add_xaxis(x)
# Y axis data is stackable, with 50% space between each column. Y axis values are not displayed on the graph
.add_yaxis("Support", y1, stack="stack1", category_gap="50%", label_opts=opts.LabelOpts(is_show=False),)
.add_yaxis("Against", y2, stack="stack1", category_gap="50%", label_opts=opts.LabelOpts(is_show=False),)
.add_yaxis("No opinion.", y3, stack="stack1", category_gap="50%", label_opts=opts.LabelOpts(is_show=False),)
.render("Bar2.html"))Copy the code