Analysis of matplotlib module for Python visualization

preface

In the Internet era, a lot of data will be generated in the network every day. After data analysis, we need to visually display the data to better interpret the meaning behind the data.

In data visualization, Python also supports third modules.

  • matplotlib module: the most used visualization Library in Python
  • seaborn module: graphic visualization based on matplotlib
  • pycharts module: a class library used to generate Echarts charts

In this issue, we learn the graphic methods provided by the matplotlib module, Let's go~

1. Overview of Matplotlib module

matplotlib module is a third-party open source module developed by John Hunter team and sponsored by NumFOCUS.

The matplotlib module is a comprehensive library for Python to create static, dynamic and interactive visualization.

  • matplotlib module features

    • Easy to create charts, such as publishing quality chart and interactive data, which can be enlarged and reduced
    • Customized charts can fully control line styles, import and embed a variety of file formats
    • High scalability, compatible with third-party modules
    • matplotlib module material manual is rich in information and can be used quickly
  • matplotlib module acquisition

    matplotlib is the mainstream third-party visualization module in Python. We need to download it using pip

    pip install matplotlib
     Copy code
  • The matplotlib module uses

  • python super full database installation package learning route project source code free sharing
    In the matplotlib module, the pyplot class is the most commonly used.

    • Mode 1:
    from matplotlib import pyplot
     Copy code
    • Mode 2:
    import matplotlib.pyplot as plt
     Copy code

🔔 Important note

  1. matplotlib module official information
  2. See matplotlib internal code description

matplotlib.pyplot related methods

matplotlib.pyplot  Module is one of the most commonly used modules for drawing icons

methodeffect
pyplot.title(name)Title of chart
pyplot.xlabel(name)X axis name of the chart
pyplot.ylabel(name)y-axis name of the chart
pyplot.show()Print out chart
pyplot.plot(xvalue,yvalue)Draw a line chart
pyplot.bar(xvalue,yvalue)Draw a column chart
pyplot.axis(data)A convenient way to get or set some axis properties
pyplot.scatter(data)Scatter plot
pyplot.subplot(data)Draw subgraph
pyplot.grid(boolean)Display mesh, the default is False
pyplot.text()Process text
pyplot.pie(data)Draw pie chart
pyplot.boxplot(data)Draw box diagram
pyplot.hist(data)Draw histogram

3. matplotlib.pyplot chart display

  • Draw a line chart

    • Use the pyplot..plot() method
    from matplotlib import pyplot
    
    # Format chart font
    pyplot.rcParams["font.sans-serif"]=['SimHei']
    pyplot.rcParams["axes.unicode_minus"]=False
    
    pyplot.plot([1,2,3,4,5,6],[45,20,19,56,35,69])
    
    pyplot.title("data analyze")
    pyplot.xlabel("data")
    pyplot.ylabel("sum")
    
    pyplot.show()


  • Draw histogram

    • Use the pyplot..bar() method
    • Using the above data again, you can see the histogram
    pyplot.bar([1,2,3,4,5,6],[45,20,19,56,35,69])
    Copy code

    Draw pie chart

  • Use the pyplot.pie() method to draw a pie chart
  • At the same time, use the pyplot.axis method to set the interval of each partition

Draw pie chart

use pyplot.pie () method to draw a pie chart, and use pyplot.axis method to set the interval of each partition

from matplotlib import pyplot
labels = ["windows","MAC","ios","Android","other"]
sizes = [50,10,5,15,20]
explode = [0,0.1,0,0,0]
pyplot.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=False,startangle=90)
pyplot.axis("equal")

pyplot.title("data analyze")
pyplot.show()

Scatter plot

  • Use pyplot.scatter(x,y) to draw a scatter plot
import numpy as np
from matplotlib import pyplot

data = {"a":np.arange(50),"c":np.random.randint(0,50,50),"d":np.random.randn(50)}

data['b'] = data['a']+10*np.random.randn(50)
data['d'] = np.abs(data['d'])*100

pyplot.scatter("a","b",c='c',s='d',data=data)

pyplot.title("data analyze")
pyplot.xlabel("element a")
pyplot.ylabel("element b")

pyplot.show()


summary

In this issue, we will simply learn about the matplotlib.pyplot module drawing related charts such as broken line, column, scatter point and round cake

In the process of learning, we found that the pyplot module is easy to use, and all our data is the key point before presentation

The above is the content of this issue. Welcome to praise and comment. I'll see you next time~


 

Tags: Python Javascript Programmer Data Analysis echarts

Posted on Wed, 10 Nov 2021 10:26:19 -0500 by marcusb