This series is a compilation of notes for the introductory book "Python Programming: From Getting Started to Practice", which belongs to the primary content. The title sequence follows the title of the book.
From this article, I will use three articles to introduce how to use Python for data visualization.
1 Introduction
Starting from this article, we will use three articles to initially introduce how to use Python for data visualization operations. The content of this article includes:
- Draw a simple line chart;
- random walk
- Use the
Pygal
simulation to roll the dice.
Before the official start, two expansion packs need to be installed: matplotlib
and pygal
. The way to install third-party libraries in Python has been introduced in the previous project, and will not be repeated here.
2. Draw a simple line chart
2.1 Simple Line Chart
First we draw a simple line chart and save the code to a mpl_squares.py
file:
import matplotlib.pyplot as plt
# 输入数据, x轴
input_values = [1, 2, 3, 4, 5]
# 输出数据, y轴
squares = [1, 4, 9, 16, 25]
# linewidth表示线条的粗细
plt.plot(input_values, squares, linewidth=5)
# 设置图标标题,并给坐标轴加上标签
plt.title("Square Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)
# 设置刻度标记的大小
plt.tick_params(axis="both", labelsize=14)
plt.show()
matplotlib.pyplot.plot()
The function can only pass in one squares
parameter, which represents y
the value of the axis. In this case, the x
one-to-one correspondence will start from the 0 point of the axis. Sometimes that's easy, but in this case the icon won't be correct, so we pass in the input_values
list and map it to the squares
list one-to-one.
The code from lines 10 to 15 can be omitted, these codes just make the information of the chart more complete. The final result is as follows:
2.2 Generate a scatter plot
We use matplotlib.pyplot
the scatter()
function in to generate the scatter plot, saving the code to a scatter_squares.py
file:
import matplotlib.pyplot as plt
x_values = list(range(1, 1001))
y_values = [x ** 2 for x in x_values]
# s表示点的大小,edgecolor表示点的轮廓的颜色,c表示数据点的颜色(可以使用RGB颜色)
# plt.scatter(x_values, y_values, s=4, edgecolor="none", c="red")
# 使用渐变色, 给c赋值了一个y值列表,并使用参数cmap告诉pylot使用哪个颜色来映射
plt.scatter(x_values, y_values, s=40, edgecolor="none", c=y_values, cmap=plt.cm.Blues)
# 设置图表标题并给坐标轴加上标签
plt.title("Square Number", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)
# 设置刻度的大小
plt.tick_params(axis="both", which="major", labelsize=14)
# 每个坐标轴的取值范围
plt.axis([0, 1100, 0, 1100000])
# 第一个参数是路径名,第二个参数指定将图表多余的空白区域裁减掉。
plt.savefig("squares.png", bbox_inches="tight")
plt.show()
We used a list comprehension to generate the y-axis data, and used a gradient color to draw the image, which matplotlib.pyplot.cm.Blues
is a matplotlib
built-in gradient color, which c
corresponds to each value of . pyplot
The passed axis()
function to set the value range of each axis. Finally save the image locally. The resulting image is as follows:
3. Random Walk
A random walk means that each walk is completely random, with no clear direction, and the outcome is determined by a series of random decisions. Random walks have practical uses in nature, physics, biology, chemistry, and economics.
Use Python to generate random walk data and matplotlib
plot this data using . First create RandomWalk
the class and save the code to a random_walk.py
file:
from random import choice
class RandomWalk:
"""一个生成随机漫步数据的类"""
def __init__(self, num_points=5000):
"""初始化随机漫步的属性"""
self.num_points = num_points
# 所有随机漫步都始于(0, 0),这两个列表用于存储随机漫步数据
self.x_values = [0]
self.y_values = [0]
def fill_walk(self):
"""计算随机漫步包含的所有点"""
# 不断漫步,直到列表到达指定的长度
while len(self.x_values) < self.num_points:
# 决定前进方向以及沿这个方向前进的距离
# 通过choice从给定值中随机选取
x_direction = choice([1, -1]) # 正向还是负向
x_distance = choice([0, 1, 2, 3, 4]) # 移动的距离
x_step = x_direction * x_distance
y_direction = choice([1, -1])
y_distance = choice([0, 1, 2, 3, 4])
y_step = y_direction * y_distance
# 拒绝原地踏步
if x_step == 0 and y_step == 0:
continue
# 计算下一个点的坐标
next_x = self.x_values[-1] + x_step
next_y = self.y_values[-1] + y_step
self.x_values.append(next_x)
self.y_values.append(next_y)
The following code is used to generate a random walk image, the code is saved to a rw_visual.py
file:
import matplotlib.pyplot as plt
from random_walk import RandomWalk
while True:
rw = RandomWalk(50000)
rw.fill_walk()
# 设置绘图窗口的尺寸
plt.figure(figsize=(10, 6))
# 绘制随机漫步的图像
point_number = list(range(rw.num_points))
plt.scatter(rw.x_values, rw.y_values, s=1, c=point_number,
edgecolors="none", cmap=plt.cm.Blues)
# 突出起点和终点
plt.scatter(0, 0, c="green", edgecolors="none", s=100)
plt.scatter(rw.x_values[-1], rw.y_values[-1], c="red", edgecolors="none",
s=100)
# 隐藏坐标轴
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show()
if input("Make another walk?(y/n)") == "n":
break
The program draws the random walk graph multiple times through a loop class; the size of the image is set by pyplot
the figure()
function figsize
in inches; the path of the image is drawn through the gradient color, the color is from light to dark, and we set the starting point (green) and the ending point ( red) is highlighted; finally the axes are hidden. The final image is as follows (the effect is different for each run):
4. Using Pygal to simulate rolling dice
First we need to create a dice class Dice
, save it into dice.py
:
from random import randint
class Dice:
"""表示一个骰子类"""
def __init__(self, num_sides=6):
"""骰子默认为6面"""
self.num_sides = num_sides
def roll(self):
"""返回一个位于1和骰子面数之间的随机值"""
return randint(1, self.num_sides)
You can set the number of sides of the dice yourself. The following is a simulation of throwing two dice 50,000 times and counting the distribution of the sum of the two dice points. Finally, a vector file is generated .svg
, which can be opened in the browser. The code is as follows:
import pygal
from dice import Dice
dice_1 = Dice()
dice_2 = Dice(10)
# 掷骰子多次,并将结果存储在一个列表中
results = []
for roll_num in range(50000):
results.append(dice_1.roll() + dice_2.roll())
# 分析结果
frequences = []
# 能够模拟掷任何双骰子的情况,不管这些骰子有多少面
max_result = dice_1.num_sides + dice_2.num_sides
for value in range(2, max_result + 1):
# 统计每个结果的频数
frequences.append(results.count(value))
# 对结果进行可视化
# 创建条形图
hist = pygal.Bar()
hist.title = "Result of rolling a D6 and a D10 50000 times."
# 创建x轴上的刻度
hist.x_labels = [str(value) for value in range(2, max_result + 1)]
hist.x_title = "Result"
hist.y_title = "Frequency of Result"
# 给这组数据起个名字,并加到图表中
hist.add("D6 + D10", frequences)
# 将图像渲染为svg文件,矢量图
hist.render_to_file("dice_visual.svg")
Note that frequences
the data in are hist.x_labels
corresponding to . Here is the final result:
Pygal
Make this graph interactive: If you mouse over any data bar in the graph, you'll see its specific data.
5. Summary
This article mainly describes:
- how to generate the dataset and how to visualize it;
- How to
matplotlib
create a simple chart using; - If a scatter plot is used to explore the random walk process;
- How to use
Pygal
Create a histogram and how to use the histogram to explore the results of rolling two dice with different sides at the same time.