In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the common knowledge of python drawing". In daily operation, I believe many people have doubts about the common knowledge of python drawing. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what is the common knowledge of python drawing?" Next, please follow the editor to study!
Common knowledge of python drawing
Figure (), subplot () and subplots (), figure.add_subplot (), figure.add_axes (), plt.plot (), plt.xlim () and plt.ylim (), plt.legeng (), plt.show ()
Figure () function
Function prototype:
Matplotlib.pyplot.figure (num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=, clear=False, * * kwargs)
-- Create a new figure, or activate an existing figure. Create a new canvas or activate an existing canvas
Parameters:
Num: unique number of int or str, figure. If you do not specify to call figure, it will increase the auto-passing int from 1 by default.
Figsize: specifies the width and height of the figure in inches; = (weight,height)
Dpi: specifies the resolution of the drawing object, that is, how many pixels per inch. The default value is 801 inches equals 2.5cm.A4 paper is 21*30cm paper.
Facecolor: background color, facecolor='blue'
Edgecolor: border color
Frameon: whether to display the border or not
(1) for drawing only one picture, you can not use plt.figure (). By default, the figure will be drawn in a canvas.
Import matplotlib.pyplot as pltx= [1pje 2je 3] y1 = [1pje 2je 4] y2 = [1pje 4je 8] plt.plot (xrecy y1jue color = "red", label = "red") plt.plot (xmemy2pint color = "green", label = "green") plt.legend () # without this statement, the upper right corner labelplt.show () will not be displayed.
(2) if you want to draw two pictures, you must use plt.figure () twice. You can specify num or not specify num. If you call it twice, it will add 1 by default.
Import matplotlib.pyplot as pltx= [1Jing 2jue 3] y1 = [1je 2jue 4] y2 = [1pje 4je 8] plt.figure () plt.plot (xmemy1mcecolor = "red", label = "red") plt.legend () # if there is no such statement, the lower right corner labelplt.figure () plt.plot () label = "green") plt.legend () # without this statement, the upper right corner labelplt.show () will not be displayed.
Of course, you can also specify the same num, which will output the image in the same canvas in accordance with the above output.
This gives us the opportunity to add images to a canvas later, simply by calling plt.figure (num), and then calling the drawing function, such as plt.plot (), and so on.
Subplot () and subplots ()
Subplot () can divide figure into n subgraphs, and each execution of subplot () will generate a subgraph of the corresponding position.
Subplot () function prototype:
Subplot (nrows,ncols,sharex,sharey,subplot_kw,**fig_kw) `
Parameters:
Nrows: number of rows of subplot
Ncols: number of columns of subplot
Sharex: all subplot should use the same X-axis scale (adjusting xlim will affect all subplot)
Sharey: all subplot should use the same Y-axis scale (adjusting ylim will affect all subplot)
Subplot_kw: a keyword dictionary used to create each subplot
* * fig_kw: other keywords when creating figure
Import numpy as npimport matplotlib.pyplot as pltx = np.arange (0100) # drawing 1plt.subplot (2je 2je 1) # equivalent to plt.subplot (221) plt.plot (x, x) # drawing 2plt.subplot (2jue 2) plt.plot (x,-x) # drawing 3plt.subplot (2m 2J 3) plt.plot (x, x * * 2) plt.grid (color='r', linestyle='--', linewidth=1,alpha=0.3) # drawing 4#plt.subplot (224) # plt.plot (x Np.log (x)) plt.show ()
The subplots () function is similar to the subplot () parameter
Import numpy as npimport matplotlib.pyplot as pltx = np.arange (0,100) # Partition subgraph fig,axes=plt.subplots (2) # returns a canvas and axis object You can take out the drawing axis and call the drawing function ax1=axes [0LECHERO] ax2=axes [0JEI 1] ax3=axes [1JEO] ax4=axes [1JEI] # drawing 1ax1.plot (x, x) # drawing 2ax2.plot (x,-x) # drawing 3ax3.plot (x, x * * 2) ax3.grid (color='r', linestyle='--', linewidth=1,alpha=0.3) # drawing 4#ax4.plot (x, np.log (x)) plt.show ()
Unlike subplot (), subplots () draws all the axes at once and then calls the drawing function
Figure objects can add subimages or drawing areas figure.add_subplot () import numpy as npimport matplotlib.pyplot as pltx = np.arange (0,100) # New figure objects fig=plt.figure () # New subgraphs 1ax1=fig.add_subplot (2je 2) ax1.plot (x, x) # New subgraphs 3ax3=fig.add_subplot (2m 2m 3) ax3.plot (x, x * * 2) ax3.grid (color='r', linestyle='--', linewidth=1 Alpha=0.3) # newly created subgraph 4ax4=fig.add_subplot (2je 2ju 4) ax4.plot (x, np.log (x)) plt.show ()
It can be seen that the effect is no different from that of subplot.
Figure.add_axes () import numpy as npimport matplotlib.pyplot as plt# New figurefig = plt.figure () # definition data x = np.array ([1, 2, 3, 4, 5, 6, 7]) # percentage of the new area ax1#figure, drawn from the position of 10% of figure, whether the width and height is 80% # image of figure or left, bottom, width, height = 0.1,0.1,0.8 within figure 0. Get the drawn handle ax1 = fig.add_axes ([left, bottom, width, height]) # get a region ax1.plot (xjournal xonome2,'r') ax1.set_title ('a1') # plt.plot (x, y,' r') # new region ax2, nested in ax1 left, bottom, width, height = 0.2,0.6,0.25 0.2 get the drawn handle ax2 = fig.add_axes ([left, bottom, width, height]) # get another region ax2.plot (XLINGON3,'b') ax2.set_title ('a2') plt.show ()
This allows you to draw images anywhere in figure, making it more flexible!
Plt.plot ()
Parameters:
X, y: X is optional, and if x does not, it will default from 0 to nMel 1, which is the index of y.
Fmt: str, optional, operations that define the color and style of lines, such as "ro" is the red circle, "Rmuri -" is the red dotted line, this is a quick styling method, more parameters can refer to the last keyboard arguments.
Kwargs: Line2D properties, optional this is a lot of optional content, you can specify a lot of content in it, such as "label" specifies the label of the line, "linewidth" specifies the width of the line, and so on.
Import matplotlib.pyplot as plta = [1, 2, 3, 4] # y is the value of a, x is the index b = [5, 6, 7, 8] plt.plot (a, b, 'gMurray, label =' aa') plt.xlabel ('this is x') plt.ylabel ('this is y') plt.title ('this is a demo') plt.legend () # shows the sample plt.show ()
Plt.xlim () and plt.ylim () import matplotlib.pyplot as pltimport numpy as npx = np.linspace (1Magi 10LY 1000) y = np.random.rand (1000) plt.scatter (x meme y Lab el = "scatter figure") plt.legend () plt.xlim (1m 10) # specify the scale values of x and y plt.ylim (0LJ 1) plt.show ()
Plt.legeng ()
Legend- (translation: (map or icon) legend, description, explanation)
1. Set the location of the column
Plt.legend (loc='') # fill in the position, such as upper left, then the legend is in the upper left corner
two。 Set the font size of the legend
Fontsize: int or float or {'xx-small',' x murmurs, 'small',' medium', 'large',' x murals, 'xx-large'}
3. Set the legend border and background
Plt.legend (loc='best',frameon=False) # remove the legend border plt.legend (loc='best',edgecolor='red') # set the legend border color plt.legend (loc='best',facecolor='blue') # set the legend background color. If there is no frame, the parameter is invalid.
4. Set the legend title
Legend = plt.legend (["BJ", "SH"], title='Beijing VS Shanghai') import matplotlib.pyplot as pltimport numpy as npx = np.arange ("BJ", "SH"], loc='upper left',title='Beijing VS Shanghai' #, frameon=False (["BJ", "SH"], loc='upper left',title='Beijing VS Shanghai' #, frameon=False Edgecolor='green', facecolor='black') plt.show ()
Plt.show ()
Plt.show () is to display pictures, for some people say why I did not use this function, can still display pictures?
It may be that you are using an interactive programming environment such as IPython. For example, jupyter notebook is also an internal call to IPython, because this class can output results in time. If you want to IDE (integrated development environment, such as pycharm), you must use plt.show ()
At this point, the study of "what is the common knowledge of python drawing" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.