How to implement data visualization of Python's underlying technology

王林
Release: 2023-11-08 08:21:34
Original
743 people have browsed it

How to implement data visualization of Pythons underlying technology

In today's era of artificial intelligence and big data, data visualization has become a very important link in data analysis applications. Data visualization can help us understand the data more intuitively, discover patterns and anomalies in the data, and also help us convey our data analysis to others more clearly.

Python is one of the most widely used programming languages ​​and it performs very well in the fields of data analysis and data mining. Python provides a wealth of data visualization libraries, such as Matplotlib, Seaborn, Bokeh, etc. Among them, Matplotlib is one of the most famous data visualization libraries in Python. It provides extremely rich visualization functions. However, the official documentation is not very detailed on the underlying data visualization core technology of Matplotlib. Many developers may not understand Matplotlib. How the underlying technology is implemented. Therefore, this article will focus on how to use Python's underlying technology to achieve data visualization and provide specific code examples.

Matplotlib Implementation of underlying technology

Matplotlib is a widely used data visualization library in Python, and the underlying layer is based on pyplot.

We usually import the visualization library first, then create a graphics instance through the plot() function, and then create and display the graphics through a series of functions.

The following is a simple example showing how to use the Matplotlib library in Python to draw a coordinate curve with the x-axis as the horizontal axis and the y-axis as the vertical axis.

import matplotlib.pyplot as plt
import numpy as np

# 生成X轴的范围是(-π,π)内的等差数列
x = np.linspace(-np.pi,np.pi,256,endpoint=True)

# 计算cos(x)和sin(x)的值
C,S = np.cos(x), np.sin(x)

#创建画布和子图
fig,ax = plt.subplots()

# 画出cos(x)和sin(x)的曲线图
ax.plot(x,C,label='cos(x)')
ax.plot(x,S,label='sin(x)')

# 设置标题,x轴,y轴的名称
ax.set_title('Cos and Sin Function')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')

# 设置图例
ax.legend()

# 显示图形
plt.show()
Copy after login

With the above code, you can easily draw a coordinate curve with the x-axis as the horizontal axis and the y-axis as the vertical axis.

Matplotlib underlying technology implementation process

In the above code, we first generated the value range of the x-axis, and then calculated the values ​​of cos(x) and sin(x) . Next, we create a canvas and a subplot, and then use the plot() function to perform drawing operations. Finally, we set the title, x/y axis name and legend of the graph through some functions, and then call the show() function to display the canvas instance.

Among them, the matplotlib.pyplot sublibrary is the drawing module under the Matplotlib library. It provides various functions for drawing on NumPy arrays. The implementation of Matplotlib's underlying technology can be understood from two aspects, namely FigureCanvas and Renderer, which are the canvas and renderer objects in Matplotlib respectively.

FigureCanvas is an object-oriented graphics display class in Matplotlib. It is responsible for interacting with the drawing device and outputting the drawing results to the display screen. In the above example, we created a Figure, a canvas object, through plt.subplots(). All subsequent drawing operations are performed on this canvas.

Renderer is a renderer object in Matplotlib. It is responsible for drawing the lines, points, text, etc. of the drawing into images, that is, rendering them on the canvas. In the above example, we used the ax.plot() function to draw the curves of cos(x) and sin(x), and this function actually uses a renderer object to draw the graphics. In this process, the Axis X/Y Limiter is first called to determine the data range on each coordinate axis, then the Scaler is used to convert the original data into coordinates on the canvas, and finally the Renderer is used to implement the real drawing operation.

Implementation of Seaborn’s underlying technology

Seaborn is a higher-level drawing library based on Matplotlib. It provides a simpler and easier-to-use API while retaining the underlying drawing technology in Matplotlib. , it can be said that Seaborn is a supplement and enhancement of Matplotlib.

We take drawing a univariate histogram as an example to show specific code examples using the Seaborn library. This example will use the dataset "mpg" built into the Seaborn library.

import seaborn as sns

# 设置Seaborn图库的风格和背景颜色
sns.set(style='whitegrid', palette='pastel')

# 读取数据
mpg = sns.load_dataset("mpg")

# 绘制直方图,并设置额外参数
sns.distplot(mpg['mpg'], bins=20, kde=True, rug=True)

# 设置图形标题以及X轴,Y轴的标签
plt.title('Histogram of mpg ($mu=23.45, ; sigma=7.81$)')
plt.xlabel('MPG')
plt.ylabel('Frequency')

# 显示图形
plt.show()
Copy after login

Through the above code, a histogram showing the distribution of mpg data can be drawn.

Implementation process of Seaborn’s underlying technology

In the above code, we first set the style and background color of the Seaborn library, and then read the mpg data set that comes with Seaborn. Then, we used the sns.distplot() function to draw a histogram and set some additional parameters to adjust the graphic effect. Finally, we use the plt.title(), plt.xlabel() and plt.ylabel() functions to set the title of the graph, x/y axis name and other information, and then call the plt.show() function to display the graph.

The implementation process of Seaborn's underlying technology is similar to Matplotlib, and drawing is also implemented through FigureCanvas and Renderer. In the underlying technology of Seaborn, the FigureCanvas object is created through FacetGrid, and drawing is based on this canvas object. At the same time, drawing in the Seaborn library is mainly implemented through the AxesSubplot class. This class is a subclass of the Axes class in Matplotlib, but it is designed to be more efficient and easier to use, so it is used by Seaborn as the main implementation of the underlying drawing technology.

Implementation of Bokeh’s underlying technology

Bokeh is a Python library for data visualization and exploratory analysis that is interactive, responsive, and efficient in creating dynamic data visualizations. The drawing technology in Bokeh's underlying technology is mainly implemented based on JavaScript, so it can achieve more interactive and dynamic visualization effects.

下面展示一个简单的 Bokeh 代码示例,说明如何在 Python 中使用 Bokeh 库绘制一个5条折线图,其中使用 Bokeh 提供的工具箱来进行交互式操作。

from bokeh.plotting import figure, show
from bokeh.io import output_notebook

# 启用Jupyter Notebook绘图
output_notebook()

# 创建一个 Bokeh 图形对象
p = figure(title="Simple Line Graph")

# 创建折线图
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
p.line(x, y, legend="Line A", line_width=2)

y2 = [2, 3, 4, 5, 6]
p.line(x, y2, legend="Line B", line_width=2)

y3 = [4, 5, 1, 7, 8]
p.line(x, y3, legend="Line C", line_width=2)

y4 = [6, 2, 4, 8, 1]
p.line(x, y4, legend="Line D", line_width=2)

y5 = [5, 8, 6, 2, 4]
p.line(x, y5, legend="Line E", line_width=2)

# 添加工具箱
p.toolbar_location = "above"
p.toolbar.logo = "grey"

# 设置图形的X轴,Y轴以及图例
p.xaxis.axis_label = "X"
p.yaxis.axis_label = "Y"
p.legend.location = "bottom_right"

# 显示图形
show(p)
Copy after login

通过上述代码,可以绘制出一个包含5条折线的折线图,并且提供了一些 Bokeh 工具箱来提供交互式操作。

Bokeh 底层技术的实现过程

Bokeh 底层技术的实现过程中,最核心的部分就是基于 JavaScript 来实现绘图。在上述代码中,我们主要使用了 Bokeh 的 figure()函数来创建一个 Bokeh 图形对象。同时,我们也使用了 Bokeh 提供的 line()函数来创建折线图,并且添加了一些工具箱和额外的功能,如工具箱的位置、X轴/Y轴的名称和图例的位置等等。

在Bokeh 底层技术的实现过程中,将Python代码转换为JavaScript代码非常重要。Bokeh 将Python代码转换为 JavaScript 代码,然后使用 Web 技术在前端绘图。Bokeh 库中的 BokehJS 是使用 TypeScript 编写的 JavaScript 库,它实现了所有 Bokeh 的绘图功能。因此,在使用Bokeh库绘制数据可视化时,我们也需要对比对JavaScript进行一些调试和定制。

小结

数据可视化是一个重要的环节,而Python通过各种底层技术提供了多种数据可视化库,其中最为流行的有Matplotlib、Seaborn和Bokeh等。这些库都支持Python本身的各种数据类型,并且能够提供非常高效,简洁和灵活的绘制方法。

本文主要介绍了使用Python底层技术实现数据可视化的方法,并提供了各库中的具体代码示例。通过学习这些底层技术,可以更加深入地了解Python数据可视化库背后的原理和细节。

The above is the detailed content of How to implement data visualization of Python's underlying technology. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template