In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the use of matplotlib library, the article is very detailed, has a certain reference value, interested friends must read it!
Matplotlib is a Python 2D drawing library (you can also draw 3D graphics, of course), which generates publication quality graphics in a variety of hard copy formats and cross-platform interactive environments. With Matplotlib, developers can generate drawings, histograms, power spectra, bar charts, error charts, scatter plots, etc., with only a few lines of code.
Matplotlib has two main modules: pyplot and pylab. What is the difference and relationship between them?
First of all, pyplot and pylab can draw graphics, and both API are similar, in which pylab includes many functions commonly used in numpy and pyplot modules, which are more convenient for interactive use (such as in the IPython interactive environment), both drawing and calculation can be carried out, but it is officially recommended that it is best to import pyplot and numpy respectively for project programming to make drawings and calculations.
Let's start with a simple one.
Import numpy as npfrom matplotlib import pyplot as pltx = np.arange (0,11) y = 2*xplt.plot (x, y, marker='o') plt.show ()
Multiple lines in the same picture x = np.arange (0,11) y1 = 2*xy2 = 4*x# gmure-denote green dotted line r-denotes red solid line plt.plot (x, y1, 'gmelet') plt.plot (x, y2, 'RMUE') plt.show ()
Matplotlib Api style
Matplotlib has two programming styles, one is familiar to Matlab users, and the other is object-oriented. The latter is recommended.
Matlab style Api
Plt.figure (figsize= (10,5)) x = [1,2,3] y = [2,4,6] plt.plot (x, y,'r') plt.xlabel ('x axis') plt.ylabel ('y axis') plt.title ('figure title') plt.show ()
Object-oriented Api
Fig = plt.figure (figsize= (10,5)) # add_subplot (nrows, ncols, index, * * kwargs) default (1,1,1) axes = fig.add_subplot () x = [1,2,3] y = [2,4,6] axes.plot (x, y,'r') axes.set_xlabel ('x axis') axes.set_ylabel ('y axis') axes.set_title ('figure title') plt.show ()
All get the following figure
Multiple subgraphs fig: plt.Figure = plt.figure (figsize= (10, 5)) axes1 = fig.add_subplot (2, 1, 1) # (nrows, ncols, index) # share y-axis with axes1 # the axis will have the same limits, ticks, and scale as the axis# facecolor: axis graphic background color axes2 = fig.add_subplot (2, 1, 2, sharex=axes1, sharey=axes1, facecolor='k') x = np.linspace (- 5,5) Y1 = np.sin (x) y2 = np.cos (x) axes1.plot (x, y1, color='red', linestyle='solid') axes2.plot (x, y2, color='#00ff00', linestyle='dashed') axes1.set_title ('axes1 title') axes2.set_title (' axes2 title') plt.show ()
# axes1 = fig.add_axes ([0.1,0.1,0.8,0.8]) # [left, bottom, width, height] axes2 = fig.add_axes ([0.7,0.7,0.2,0.2])
Object relation
In matplotlib, the entire image is a Figure object. A Figure object can contain one or more Axes objects, each of which is a drawing area with its own coordinate system.
Title-- title Axis-- axes Label-- axis dimensions Tick-- tick marks Tick Label-- tick notes
Abbreviated form
Point mark
Only common ones are listed (only shorthand forms are supported)
Legend axes.plot (x, y, 'ringing, label='y=2x') # oraxes.set_label (' yaw2x') axes.legend (loc='upper right') # default legend (loc='best') # set multiple plt.legend ((axes1, axes2, axes3), ('label1',' label2', 'label3') at the same time)
Adjust the upper and lower limits of the axis
# xlim (left=None, right=None, emit=True, auto=False, *, xmin=None, xmax=None) # ylim (bottom=None, top=None, emit=True, auto=False, *, ymin=None, ymax=None) axes.set_xlim (0,8) axes.set_ylim (0,8)
Until the axis display interval
If from matplotlib.ticker import MultipleLocator# sets the main tick mark interval to 2axes.xaxis.set_major_locator (MultipleLocator (2))
Adjust the display of axis labels
When the dots are dense, the x-axis tick mark labels may overlap, especially in time # 1. For time, you can use autofmt_xdate to automatically adjust alignment and rotate fig.autofmt_xdate () # 2. General ax.set_xticklabels (xticks, rotation=30, ha='right') # for date display, you can also customize the format from matplotlib.dates import DateFormatterax.xaxis.set_major_formatter (DateFormatter ('% YMY% m'))
Set axis name
# label ax.set_xlabel ('Time Series') ax.set_ylabel (' Microseismicity') displayed below or on the left side of the axis
Comprehensive example
Fig = plt.figure () axes = fig.add_subplot () t = np.linspace (0, np.pi, 64) x = np.sin (t) y = np.cos (t) + np.power (x, 2.0 / 3) # adopt parameter definition style axes.plot (x, y, color='red', marker='o', linestyle='solid', linewidth=2, label='x > 0') # use the abbreviation color marker linestyle without order You don't need to write axes.plot (- x, y, 'ro-', linewidth=2, label='x)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.