Matplotlib Study 1

Source: Internet
Author: User
Tags cos dashed line image processing library sin
PIL (Python Image Library) image processing library


http://blog.csdn.net/y15166022470/article/details/51172091








>>> Import Matplotlib.pyplot as Plt
>>> Import Matplotlib.cbook as Cbook
>>> image_file = cbook.get_sample_data (' logo2.png ')
>>> image = Plt.imread (image_file)
>>> plt.imshow (image)
<matplotlib.image.axesimage Object at 0x0000000007607e10>
>>> plt.axis (' off ')
(-0.5, 559.5, 119.5,-0.5)
>>> Plt.show ()




Quick Draw


Drawing with the Pyplot module


Matplotlib's Pyplot module provides a mapping API similar to MATLAB, allowing users to quickly draw two-dimensional charts.






>>> Import NumPy as NP
>>> Import Matplotlib.pyplot as PLT (1)
>>> x = np.linspace (0, 10, 1000)
>>> y = np.sin (x)
>>> z = Np.cos (x**2)
>>> plt.figure (figsize= (8,4)) (2)
<matplotlib.figure.figure Object at 0x00000000029ef9e8>
>>> Plt.plot (x,y,label= "$sin (x) $", color= "Red", linewidth=2) (3)
[<matplotlib.lines.line2d object at 0x0000000002412748>] (4)
>>> plt.plot (x, y, "b--", label= "$cos (x^2) $")
[<matplotlib.lines.line2d object at 0x00000000029d3cf8>]
>>> Plt.xlabel ("Time (s)") (5)
<matplotlib.text.text Object at 0x00000000029d8160>
>>> Plt.ylabel ("Volt")
<matplotlib.text.text Object at 0x0000000007613940>
>>> Plt.title ("Pyplot first Example")
<matplotlib.text.text Object at 0x00000000076108d0>
>>> Plt.ylim ( -1.2,1.2)
(-1.2, 1.2)
>>> Plt.legend ()
<matplotlib.legend.legend Object at 0x00000000075fe198>
>>> Plt.show () (6)




1) First load the Matplotlib drawing module Pyplot, and rename it to PLT.


2) call figure () to create a figure (chart) object, and it will become the current figure object. You can also directly invoke the next plot () without creating a figure object, and Matplotlib will automatically create a figure object. The Figsize parameter specifies the width and height of the figure object, in inches. You can also specify the resolution of the figure object using the DPI parameter, which is the number of pixels per inch, using the default value of 80. So the figure object created in this example has a width of "8*80=640" pixels. However, when you save the chart as an image with the Save button in the toolbar after the drawing window is displayed, the size of the saved image is "800*400" pixels. This is because different DPI settings are used when you save the image.
>>> matplotlib.rcparams["savefig.dpi"]
100
Because the DPI is set to 100 when the image is saved, the width of the saved image is "8*100=800" pixels. Rcparams is a dictionary that holds all the configurations that are read from the configuration file, and when the various drawing functions are called, these configurations will be the default values for various parameters.


3) Plt.plot (x,y,label= "$sin (x) $", color= "Red", linewidth=2)
After creating the Fifure object, next Call plot () to draw in the current figure object. Actually plot () is plotted on the axes (sub-graph) object, and if there is no axes object in the current figure object, it will create a axes object that is almost full of the entire chart, and make this axes object the current axes object. The preceding parameter of plot () is an object that represents the X-and y-axis data, using the NumPy array. Use the singular keyword to specify the various properties of the plotted curve:
Label: assigns a label name to the curve, which is displayed in the diagram. If the label string has a character ' $ ' before and after it, Matplotlib will display it as a mathematical formula using the latex engine embedded in the period.
Color: Specifies the colors of the curve, which can be in English words, or
LineWidth: Specifies the width of the curve, either not an integer, or using the abbreviated parameter name LW.


4) Plt.plot (x,z, "b--", label= "$cos (x^2) $")
The color and linearity of the curve are specified directly through the third parameter "b--", where ' B ' denotes blue, '--' indicates that the Linetype is a dashed line.


5) Plt.xlabel ("Time (s)")
Xlabel, Ylabel: Sets the title text of the x and Y axes, respectively.
Title: Sets the caption of the sub-graph.
Xlim, Ylim: Sets the display range of the x and Y axes, respectively.
Legend: Displays the diagram, which represents the label of each curve and the rectangular area of the style.


6)








Object-oriented plotting




Current charts and sub-graphs can be obtained using GCF () and GCA (), respectively, with the initials "get current figures" and "Get current Axis".




Run E:/python lianxi/matplotlib_simple_plot.py

















Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.