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 use the Matplotlib Library of Python data Science

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article editor for you a detailed introduction of "Python data science Matplotlib library how to use", the content is detailed, the steps are clear, the details are handled properly, I hope this "Python data science Matplotlib library how to use" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Matplotlib is a two-dimensional drawing library of Python, which is used to generate all kinds of graphics that meet the publishing quality or cross-platform interactive environment.

Work flow

The basic steps of Matplotlib drawing:

1 prepare data

2 create a graphic

3 drawing

4 Custom Settin

5 Save the drawin

6 display graphics

Import matplotlib.pyplot as pltx = [1Jing 2Jing 3jue 4] # step1y = [10Jet 20je 25pas 30] fig = plt.figure () # step2ax = fig.add_subplot (111L) # step3ax.plot (x, y, color='lightblue', linewidth=3) # step3\ 4ax.scatter ([2Med 4Jing 6], [5J 15J 25], color='darkgreen', marker=' ^') ax.set_xlim (1 6. 5) plt.savefig ('foo.png') # step5plt.show () # step6 prepare data

One-dimensional data

Import numpy as np x = np.linspace (0,10,100) y = np.cos (x) z = np.sin (x)

Two-dimensional data or picture

Data = 2 * np.random.random ((10,10)) data2 = 3 * np.random.random ((10,10)) Y, X = np.mgrid [- 3 data2 100j,-3 data2] U =-1-Xerogram 2 + YV = 1 + X-Y**2from matplotlib.cbook import get_sample_dataimg = np.load

Draw a graph

Import matplotlib.pyplot as plt

Canvas

Fig = plt.figure () fig2 = plt.figure (figsize=plt.figaspect (2.0))

Coordinate axis

The graph is drawn with the coordinate axis as the core, and in most cases, the subgraph can meet the demand. The subgraph is the axis of the grid system.

Fig.add_axes () ax1 = fig.add_subplot (221) # row-col-numax3 = fig.add_subplot fig3, axes = plt.subplots (nrows=2,ncols=2) fig4, axes2 = plt.subplots (ncols=3) drawing routines

One-dimensional data

Fig, ax = plt.subplots () lines = ax.plot (XMague y) # scale or color unconnected points axes with lines or marked join points ax.scatter (XMague y). Bar ([1rem 2m 3], [3m 4m 5]) # draw an equal width longitudinal rectangle axes [1m 0] .barh ([0.5 m 1m 2.5], [0r 1] ) # draw a contour horizontal rectangle axes [1Mague 1] .axhline (0.45) # draw a horizontal line axes parallel to the axis. Axvline (0.65) # draw a vertical line ax.fill perpendicular to the axis (xQuery coloring colors blue blue') # draw a filled polygon ax.fill_between (xrecoverycolorific fellowship) # fill between the y value and 0

Two-dimensional data or picture

Import matplotlib.image as imgpltimg = imgplt.imread ('img, cmap='gist_earth', interpolation='nearest', vmin=-200, vmax=200)) fig, ax= plt.subplots () im = ax.imshow (img, cmap='gist_earth', interpolation='nearest', vmin=-200, vmax=200) # color table or RGB array axes2 [0] .pcolor (data2) # two-dimensional array pseudo-color axes2 [0] .pcolormesh (data) # two-dimensional array CS = plt.contour U) # Contour axes2 [2] .contourf (data) axes2 [2] = ax.clabel (CS) # Contour label

Vector field

Axes [0Magne1] .arrow (0memo0meme0.5) # add arrows to the axes axes [1mem1] .quiver (ymemz) # 2D arrowhead axes [0jor1] .streamplot (Xmemy Ypen1) # 2D arrowheads

Data distribution

Ax1.hist (y) # histogram ax3.boxplot (y) # Box figure ax3.violinplot (z) # Violin diagram

Customize graphic colors, color bars, and color tables

Plt.plot (x, x) ax.plot (x, y, alpha = 0.4) ax.plot (x, y, cantilever k') fig.colorbar (im, orientation='horizontal') im = ax.imshow (img, cmap='seismic')

Marking

Fig, ax = plt.subplots () ax.scatter (xmemymenmark = ".") ax.plot (xmemymemmarker = "o")

Linetype

Plt.plot (xrecedence ydepartment linewidthparts 4.0) plt.plot (xrecedence yrecinct lswriting skills solidi') plt.plot (xmeme yreco LSZHI LSZHYZHANGLING LSZHULICUR') plt.plot (XZHINGYJEI LINGHANGLING LINGHANGLING) plt.setp (lines,color='r',linewidth=4.0)

Text and annotations

Ax.text (1,-2.1, 'Example Graph', style='italic') ax.annotate ("Sine", xy= (8,0), xycoords='data', xytext= (10.5,0), textcoords='data', arrowprops=dict (arrow, connection),)

Mathematical symbol

Plt.title (r'$sigma_i=15 $', fontsize=20) size limits, legends, and layouts

Size limit and automatic adjustment

Ax.margins # add inner margin ax.axis ('equal') # set the aspect ratio to 1ax.set (xlim=, ylim= [- 1.5, 1.5]) # set the limit ax.set_xlim between x-axis and y-axis (0d10.5)

Legend

Ax.set (title='An Example Axes', ylabel='Y-Axis', xlabel='X-Axis') # sets the label of the title and x, y axis ax.legend (loc='best') # automatically selects the best legend location

Marking

Ax.xaxis.set (ticks=range (1p5), ticklabels= [3Jing 100Jing Lili 12, "foo"]) # manually set the X axis scale ax.tick_params (axis='y', direction='inout', length=10) # set the length and direction of the Y axis

Subgraph spacing

Fig3.subplots_adjust (wspace=0.5, hspace=0.3, left=0.125, right=0.9, top=0.9, bottom=0.1) fig.tight_layout () # sets the subgraph layout of the canvas

Coordinate axis boundary line

Ax1.spines ['top'] .set_visible (False) # hides the top axis ax1.spines [' bottom'] .set_position (('outward',10)) # sets the position of the bottom edge to outward

Save

# Save canvas plt.savefig ('foo.png') # Save transparent canvas plt.savefig (' foo.png', transparent=True)

Display graphic

Plt.show ()

Close and clear

Plt.cla () # clear axes plt.clf () # clear canvas plt.close () # close the window here, this article "how to use the Python data Science Matplotlib Library" has been introduced, you need to master the knowledge points of this article, you still need to practice and use it to understand, if you want to know more related articles, welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report