Python-matplotlib Visualization of data

Source: Internet
Author: User
Tags ggplot

In many practical problems, the data given is often visualized for easy observation.

Today, the data visualization module in Python is--matplotlib this content system to make it easy to find and use. This article comes from a summary of "data analysis using Python" and some online blogs.

1 Matplotlib Introduction Matplotlib is the leading authority of the Pythom Visualization library, and it is still the most commonly used drawing library for Python users after decades. There are many other libraries that are built on it or called directly, such as Pandas and Seaborn are matplotlib outsourcing, which allows you to use less code to use the Matplotlib method. Gallery Page has hundreds of thumbnails, opened after the source program, very suitable for learning matplotlib. 2 creation of graphs and sub-graphs 2.1 import Matplotlib
Import Matplotlib.pyplot as Plt
Import NumPy as NP

  

2.2 Building and sub-diagram mode a plt.plot () will be drawn on the most recent diagram
From numpy.random import randn  fig = plt.figure (figsize = (8,4))    #设置图的大小  ax1 = Fig.add_subplot (2,2,1)  AX2 = Fig.add_subplot (2,2,2)  ax3 = Fig.add_subplot (2,1,2)  Ax3.plot (Randn () cumsum (), ' k--')   # Plt.plot (Randn () cumsum (), ' k--') equivalent  ax1.hist (RANDN (+), bins = ten, color = ' B ', alpha = 0.3)      #bins divided into how many intervals   alpha transparency  Ax2.scatter (Np.arange (+), Np.arange (+) + 3*RANDN (30))  

2.3 Creating a sub-graph mode two
From numpy.random import randn  fig, axes = plt.subplots (2,2)                               #以数组方式访问  t = Np.arange (0., 5., 0.2)  Axes[0,0].plot (T, T, ' R-o ', T, T**2, ' BS ', T, t**3, ' g^ ')   #同时绘制多条曲线  Axes[1,1].plot (Randn () cumsum (), ' b--')  Plt.show ()  

 

2.4 Theme Settings

Use the Style.use () function

Df_iris = Pd.read_csv ('.. /input/iris.csv ')  plt.style.use (' Ggplot ')    # ' FiveThirtyEight ', ' ggplot ', ' dark_background ', ' BMH '  df_ Iris.hist (' sepal length ')  plt.show ()  

 

3 colors, markers, linetypes, ticks, labels, and legends
From numpy.random import randn  fig = plt.figure ()  ax1 = Fig.add_subplot (1,1,1)  Ax1.plot (RANDN (+). Cumsum ( ), color = ' B ', LineStyle = '--', marker = ' o ', label = ' $cumsum $ ')  # Linetype  can be directly ' K--o '  ax1.set_xlim (10,25)                                                                                     Ax1.set_title (' My first plot ')  Ax1.set_xlabel (' Stages ')  plt.legend (loc = ' best ')           #把图放在不碍事的地方  Xticks ([]) Set scale  plt.show ()  

  

Equivalent to the following code:

From numpy.random import randn  fig = plt.figure ()  ax1 = Fig.add_subplot (1,1,1)  Ax1.plot (RANDN (+). Cumsum ( ), color = ' B ', LineStyle = '--', marker = ' o ', label = ' $cumsum $ ')   #图标可以使用latex内嵌公式  Plt.xlim (10,25)                   #plt. Axis ([10,25,0,10]) set the X, Y axis range at the same time  plt.title (' My first plot ')  Plt.xlabel (' Stages ')  plt.legend (loc = ' best ')  Plt.show ()  

  

4 drawing functions in pandas in pandas, we have row labels, column labels, and grouping information. That is to say, to make a complete chart, originally need a lot of matplotlib code, now only one or two simple statements can be. Pandas has a number of advanced drawing methods that enable you to create standard charts using Dataframe object data organization features. 4.1 Line chart
From numpy.random import randn  fig, axes = plt.subplots (.)  s = PD. Series (RANDN) cumsum (), index = Np.arange (0,100,10))  s.plot (ax = axes[0])   # AX parameter Select the sub-graph    df = pd. DataFrame (Randn (10,3). Cumsum (0), columns = [' A ', ' B ', ' C '],index = Np.arange (0,100,10))  df.plot (ax = axes[1])      Plt.show ()  

 

4.2 Bar Chart
From numpy.random import rand  fig, axes = plt.subplots (  data = PD). Series (rand (+), index = list (' Abcdefghijklmnop '))  data.plot (kind = ' bar ', ax = axes[0], color = ' B ', alpha = 0.7)    #kind选择图表类型  ' bar ' vertical histogram  data.plot (kind = ' Barh ', ax = axes[1], color = ' B ', alpha = 0.7)   # ' Barh ' horizontal histogram  

  

From numpy.random import rand  fig, axes = plt.subplots (  data = PD). DataFrame (rand (6,4), index = [' One ', ' one ', ' one ', '                      three ', ' four ', ' five ', ' six '],                      columns = PD. Index ([' A ', ' B ', ' C ', ' D '], name = ' Genus '))  data.plot (kind = ' bar ', ax = axes[0], alpha = 0.5)  data.plot (kind = ' B Ar ', ax = axes[1], stacked = True, alpha = 0.5)  plt.show ()  

In addition, the histogram has a very good usage, using value_counts () to graphically display the probability of the occurrence of the values in the series, such as S.value_counts (). Plot (kind = ' bar ').

4.3 Histogram and density graphs
From numpy.random import randn  fig, axes = plt.subplots (  data = PD). Series (RANDN)  data.hist (ax = axes[0], bins =)       #直方图  data.plot (kind = ' KDE ', ax = axes[1])    #密度图 
   plt.show ()  

In fact, you can make multiple histograms at once, the layout parameter means to divide two graphs into two rows and one column, if there is no parameter, the default will be all the graph on the same line.

Df_iris = Pd.read_csv ('.. /input/iris.csv ')  columns = [' sepal length ', ' sepal width ', ' petal length ', ' petal width ']  df_iris.hist (column= Columns, layout= (2,2))  plt.show ()  

  

4.4-Box diagramThe box chart is a graphical summary of the data based on the five-number generalization (minimum, first four-digit, first four-digit (median), third four-digit, maximum), and also uses four-bit spacing IQR = Third four-digit-the first four-bit number.
Df_iris = Pd.read_csv ('.. /input/iris.csv ')  #[' sepal length ', ' sepal width ', ' petal length ', ' petal width ', ' class ']  sample_size = Df_iris [[' Petal width ', ' class ']  Sample_size.boxplot (by= ' class ')  plt.xticks (rotation=90)                     #将X轴的坐标文字旋转90度, vertical display  plt.show ()  

  

5 Reference Links
    • Gallery Page
    • matplotlib-to draw beautiful charts
    • Python-matplotlib Drawing visualization Knowledge Point finishing
    • Python Data visualization matplotlib Learning notes
    • 53814635 (record only, easy to find use)

Python-matplotlib Visualization of data

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.