Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to Visualize drawing with matplotlib in python

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use python matplotlib visual drawing, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

1. Introduction to Matplotlib

Introduction to Matplotlib:

Matplotlib is a python 2D drawing library, which generates published quality graphics in a variety of hard copy formats and cross-platform interactive environment. Matplotlib is relatively perfect for image beautification, can customize the color and style of lines, can draw multiple small pictures on a drawing paper, can also draw multiple lines on a picture, and can easily visualize and compare the data.

Compared with Excel drawing related icons, using Matplotlib to draw related icons has a higher degree of freedom, and can solve the problem that excel can not draw a large amount of data and more dimensions.

How to use Matplotlib:

In python environment:

Pip install matplotlib

In jupyter notebook:

! Pip install matplotlib

(it is strongly recommended to install anaconda so that some commonly used packages do not need to be installed separately.) after installing anaconda, just guide the package directly

Import matplotlib.pyplot as plt # plt is an alias

Matplotlib draws graphics:

Line chart plot bar chart bar bar chart barh pie chart pie scatter chart scatter histogram hist box chart boxplot subchart subplot2, Matplotlib drawing 1) line chart

A line chart (line chart) is a graph that connects data points sequentially. A line chart is a graph in which data arranged in columns or rows of a worksheet can be drawn into a line chart. A line chart can display continuous data that changes over time (according to common scale settings), so it is very suitable for displaying the trend of data at equal intervals.

Plt.figure (figsize= (10d6)) # resize canvas plt.plot (data ["date"], data ["self-delivery sales"], color='y') # adjust color plt.xticks (rotation=45) # x-axis tilt angle plt.show () through color

The colors can be set to many, as shown in the following table:

'r 'red' g 'green' b 'blue' c 'cyan' m 'magenta' y 'yellow' k 'black' w 'white light pink # FFB6C1 scarlet # DC143C

At the bottom is the rgb color value. You can view the RGB color value and the hexadecimal color code conversion tool in more detail.

Plt.figure (figsize=) plt.plot (data ["date"], data ["Total sales"], color='r',linewidth=0.5,marker= "*", linestyle='-.') plt.plot (data ["date"], data ["FBA sales"], color='g',linewidth=0.5,marker= "2", linestyle='-') plt.plot (data ["date"], data ["self-distribution sales"], color='y',linewidth=0.5,marker= "s" Linestyle='--') plt.xticks (rotation=45) # rotate x-axis callout font = {"family": "kaiti", # set font style "size": "20" # set font size} plt.rc ("font" * * font) # set the X axis label plt.xlabel ("time") # set the y axis label plt.ylabel ("amount") plt.show () # linewidth is the width of the set line # marker is the inflection mark style of the set line

Common font name:

Italic kaiti boldface SimHei Microsoft Yahei Microsoft YaHei New Song style NSimSun imitation Song _ GB2312FangSong_GB2312 italic _ GB2312KaiTi_GB2312

Common linetypes:

Solid line'- 'dotted line': 'broken line'-'dash line'. Do not draw lines'or''

Use the tag:

Point "." Upper triangle "^" upper trigeminal "2" square "s" asterisk "*" diamond "D" renders the specified character. For example, "$f$" is marked with the letter f.' $. $'2) histogram

Bar chart, also known as bar chart, column statistical chart (German: s ä ulendiagramm, English: bar chart, Spanish: diagrama de barras), also known as bar chart, bar chart, is a kind of statistical chart with the length of rectangle as a variable. A bar chart is used to compare two or more values (at different times or under different conditions) with only one variable and is usually used for smaller data set analysis. Bar diagrams can also be arranged horizontally or expressed in a multi-dimensional way.

Matplotlib.pyplot.bar (x, height, width=0.8, bottom=None, *, align='center', data=None, * kwargs)

Parameter description:

X: floating point array, x-axis data of column chart.

Height: floating-point array, the height of the bar chart.

Width: floating point array, the width of the bar chart.

Bottom: floating-point array, y-coordinate of the base. Default is 0.

Align: the alignment of the column chart to the x coordinate. 'center' is centered on the x position, which is the default value. Edge': aligns the left edge of the column chart with the x position. To align the bar at the right edge, you can pass a negative width value and align='edge'.

* * other parameters of kwargs::.

Plt.figure (figsize= (10d5)) plt.bar (data ["sort"] Data ["sales volume"]) # # adjust canvas frame color: top: bottom left: left right: rightax=plt.gca () ax.spines ["top"]. Set_color ("w") ax.spines ["bottom"]. Set_color ("r") ax.spines ["left"]. Set_color ("r") ax.spines ["right"]. Set_color ("w") # # adjust x and y axis scale plt.xlim (data.index.values [0] Data.index.values [- 1]) # x axis from 0 to the last 0 first-1 last plt.ylim (np.min (data ["sales volume"), np.max (data ["sales volume")) # smallest to maximum plt.show ()

3) Bar chart

A bar chart (bar chart) is a graph that represents the amount of data in terms of the height or length of bars of the same width. Bar charts can be horizontally or vertically, also known as column chart. In addition, the bar chart has the form of simple bar chart, compound bar chart and so on.

Plt.figure (figsize= (10d5)) plt.barh (data ["sort"] Data ["sales volume"]) # # adjust canvas frame color: top bottom: bottom left: left right: rightax=plt.gca () ax.spines ["top"]. Set_color ("w") ax.spines ["bottom"]. Set_color ("r") ax.spines ["left"]. Set_color ("r") ax.spines ["right"]. Set_color ("w") # # adjust x and y axis scale plt.xlim (np.min (data ["sales volume"]) Np.max (data ["sales volume")) # x axis from 0 to the last 0 first-1 last plt.ylim (data.index.values [0], data.index.values [- 1]) # minimum to maximum plt.show ()

The bar chart is to turn the bar chart upside down and adjust the X and Y axes.

3) Pie chart matplotlib.pyplot.pie (x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=0, radius=1, counterclock=True, wedgeprops=None, textprops=None, center=0, 0, frame=False, rotatelabels=False, *, normalize=None, data=None) [source]

Parameter description:

X: floating-point array that represents the area of each sector.

Explode: array that represents the interval between sectors. The default value is 0.

Labels: list, labels for each sector. The default value is None.

Colors: array that represents the color of each sector. The default value is None.

Autopct: set the display format of each sector percentage in the pie chart,% d%% integer percentage,% 0.1f one decimal place,% 0.1f% one decimal place percentage,% 0.2f% two decimal place percentage.

Labeldistance: where the label tag is drawn, relative to the radius, the default value is 1.1, such as 20] plt.hist (data1 ["quantity"], bins=data1.index.values [- 1] # xz axis how much is displayed, align = "mid", density=True# frequency setting) plt.xlim (10) plt.show ()

6) Box diagram

Box chart (Box-plot), also known as box chart, box chart or box chart, is a statistical chart used to display a group of data dispersion data. It gets its name because it is shaped like a box. It is also often used in various fields, often in quality management. It is mainly used to reflect the characteristics of the original data distribution, and can also be used to compare the distribution characteristics of multiple groups of data. The method of drawing box chart is to find out the upper edge, lower edge, median and two quartiles of a group of data, then connect two quartiles to draw the box, and then connect the upper edge and lower edge to the box, the median is in the middle of the box.

Plt.boxplot (data [data ["sales volume"]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report