Use Python to draw a chart summary, and use python to draw a chart.

Source: Internet
Author: User

Use Python to draw a chart summary, and use python to draw a chart.

Before using Python to draw a chart, we need to install two library files: numpy and matplotlib.

Numpy is an open-source Python numeric computing extension that can be used to store and process large matrices. It is more efficient than Python's own data structure. matplotlib is a Python image framework, the effect of the image drawn using it is similar to that drawn using MATLAB.

Next I will use some simple code to introduce how to plot using Python.

1. Drawing

Histogram

Importmatplotlib. pyplotaspltimportnumpyasnpmu = 100 sigma = 20x = mu + sigma * np. random. randn (20000) # number of samples plt. hist (x, bins = 100, color = 'green', normed = True) # bins displays several vertices and whether normed standardizes the data plt. show ()

Bar Chart

importmatplotlib.pyplotaspltimportnumpyasnpy=[20,10,30,25,15]index=np.arange(5)plt.bar(left=index,height=y,color='green',width=0.5)plt.show()

Line chart

importmatplotlib.pyplotaspltimportnumpyasnpx=np.linspace(-10,10,100)y=x**3plt.plot(x,y,linestyle='--',color='green',marker='<')plt.show()

Scatter chart

Importmatplotlib. pyplotaspltimportnumpyasnpx = np. random. randn (1000) y = x + np. random. randn (1000) * 0.5plt.scatter (x, y, s = 5, marker = '<') # s indicates the area, and marker indicates the drawing plt. show ()

Pie Chart

Importmatplotlib. pyplotaspltimportnumpyasnplabels = 'A', 'B', 'C', 'D' fracs = [15,30, 45,10] plt. axes (aspect = 1) # Make the x-axis ratio the same explode = [0, 0.05,] # highlight a part of the Area plt. pie (x = fracs, labels = labels, autopct = '%. 0f % ', explode = explode) # autopct displays the percentage plt. show ()

Box chart

It is mainly used to display data dispersion. Graphs are divided into the upper edge, the upper quartile, the median, the lower quartile, and the lower edge. Abnormal values outside

importmatplotlib.pyplotaspltimportnumpyasnpnp.random.seed(100)data=np.random.normal(size=(1000,4),loc=0,scale=1)labels=['A','B','C','D']plt.boxplot(data,labels=labels)plt.show()

Ii. Image Adjustment

1. 23 point shapes

"."point","pixel"o"circle"v"triangle_down"^"triangle_up"<"triangle_left">"triangle_right"1"tri_down"2"tri_up"3"tri_left"4"tri_right"8"octagon"s"square"p"pentagon"*"star"h"hexagon1"H"hexagon2"+"plus"x"x"D"diamond"d"thin_diamond

2. Abbreviations of 8 default colors

b:blueg:greenr:redc:cyanm:magentay:yellowk:blackw:white

3. Four linear

-Solid line-dotted line-. dotted line: dotted line

4. Draw a subgraph on a graph

Importmatplotlib. pyplotaspltimportnumpyasnpx = np. arange (1, 1,100) plt. subplot (221) #2 rows, 2 columns, and 2 columns. plot (x, x) plt. subplot (222) plt. plot (x,-x) plt. subplot (223) plt. plot (x, x * x) plt. subplot (224) plt. plot (x, np. log (x) plt. show ()

5. Generate a grid

importmatplotlib.pyplotaspltimportnumpyasnpy=np.arange(1,5)plt.plot(y,y*2)plt.grid(True,color='g',linestyle='--',linewidth='1')plt.show()

6. Generate a legend

importmatplotlib.pyplotaspltimportnumpyasnpx=np.arange(1,11,1)plt.plot(x,x*2)plt.plot(x,x*3)plt.plot(x,x*4)plt.legend(['Normal','Fast','Faster'])plt.show()

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.