Python data visualization, the Matplotlib library is really hardcore (18)

Hello children, hello big friends!

I'm Cat Girl, a primary school student who fell in love with Python programming.

Welcome to learn Python with cat girl.

today's topic

Today we learn how to implement data visualization with Pythons.

What is data visualization?

A bunch of data, if we look at it individually, it is difficult to see the overall special.

If these data are made into various graphs, does it look very conspicuous?

We randomly find a few pictures from the Internet as follows, and display the data in a chart, which is data visualization.

In Python, there are many data visualization libraries, such as Matplotlib, Seaborn, Pyecharts, Plotline, etc.

There is a profession called data analyst, which deals with data every day.

Matplotlib is the most widely used in the field of plane drawing. It borrows many Matlab functions and can draw high-quality charts. In addition to two-dimensional charts, it can also draw three-dimensional charts and animations.

Matplotlib needs to be installed with the pip command before use.

line chart

import matplotlib.pylot as plt

Import the sublibrary pylot in matplotlib and rename it to plt.

According to the two lists listx and listy, draw a polyline.

Syntax: plot(list x, list y)

Custom style color color

Custom style line appearance linestyle

  • solid: solid line

  • dashed: dotted line

  • dotted: dotted line

  • dashdot: dash dot

Stage Appearance of Custom Styles

You can use the marker parameter to define the appearance of the node. There are many values, and you can find it when you use it.

You can also set the size and color of the nodes

general settings

define title

define legend

canvas style

Axis scale

Axis range

Gridlines

description text

add notes

There are also general style parameters that can be used by most drawing methods.

  • color: color

  • frontsize: text size

  • ha: horizontal alignment, horizontal align

  • va: vertical alignment, vertical align

  • label: Legend

  • alpha: Opacity (0~10)

histogram

A histogram, also known as a bar chart, can be drawn using the bar() function.

Syntax: bar(list x, list y)

basic drawing

Horizontal histogram

stacked column chart

juxtaposed histogram

histogram

直方图用于统计各个区间数据的个数。

可以使用hist()函数绘制直方图。

语法:hist(data,group)

data必选

group可选,是否分组、如不设置库会自动分组,但不太理想,一般设置下更好。

hist()函数提供了很多参数,可自定义样式。

比如rwidth设置直方图宽度,取值0~1.0,color设置颜色,edgecolor设置边框颜色。

直方图和柱状图有什么区别?

直方图y轴表示统计区间的频率。

柱状图y轴表示数值。

直方图统计数据,柱状图展示数据。

饼状图

可以使用pie()绘制饼状图,没有坐标系,用于展示各个部分占总的比例。

语法:pie(列表)

pie()函数提供了许多参数,可自定义样式

  • labels:各部分标题,列表

  • colors:各部分颜色,列表

  • autopct:显示百分比

  • explode:是否拉出某部分,元组

  • shadow:是否显示阴影,元组

散点图

可以使用scatter()绘制散点图。

语法:scatter(列表x,列表y)

散点图类似折线图,只显示点,不显示点点之间的线。

scatter()函数提供了许多参数,可自定义样式

  • s:散点大小

  • color:散点颜色

  • alpha:散点不透明度(0~1.0)

  • marker:散点形状

  • label:图例

面积图

可以使用stackplot()绘制面积图。

参考资料

https://github.com/matplotlib/matplotlib

《从0到1Python快速上手》

看得出,Matplotlib使用简单,功能强大,的确是数据分析的利器,下次如果需要处理数据,一定要想起它哦!

好了,我们今天就学到这里吧!

如果遇到什么问题,咱们多多交流,共同解决。

我是猫妹,咱们下次见!

Guess you like

Origin blog.csdn.net/parasoft/article/details/129781424
Recommended