This is the 8th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

In the Internet era, a lot of data are generated on the network every day. After data analysis, how to better interpret the meaning behind the data needs to be visualized.

Python also supports module 3 in data visualization

  • Matplotlib module: The most used visual library for Python
  • Seaborn module: Graphic visualization based on Matplotlib
  • Pycharts module: Library for generating Echarts charts

In this episode, we will learn about the graphical methods provided by matplotlib module. Let’s go~

1. Overview of matplotlib module

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

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

  • Matplotlib module features

    • Easy to create charts such as publication quality charts, interactive data can be enlarged and reduced
    • Custom charts provide full control over line styles, import and embed multiple file formats
    • High scalability, compatible with third-party modules
    • The Matplotlib module resource manual is informative and easy to get started
  • Matplotlib module

    Matplotlib is a major third party visualization module in Python that you need to download using PIP

    pip install matplotlib
    Copy the code
  • Use the matplotlib module

    In the Matplotlib module, the Pyplot class is the most commonly used.

    • A:
    from matplotlib import pyplot
    Copy the code
    • Method 2:
    import matplotlib.pyplot as plt
    Copy the code

🔔 Important note

  1. Matplotlib module official information
  2. View the internal code description of Matplotlib

2. Matplotlib.pyplot

The matplotlib.pyplot module is one of the modules we use most often to draw ICONS

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

3. Matplotlib.pyplot chart display

  • Draw a line chart

    • Using pyplot.. The plot () method
    from matplotlib import pyplot
    
    Set the font format of the chart
    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()
    Copy the code

  • Draw a bar chart

    • Using pyplot.. The 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 the code

  • Draw the pie chart

    • Plot the pie chart using the Pyplot.pie () method
    • Also set each partition interval using the Pyplot. axis method
    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.1 f % %',shadow=False,startangle=90)
    pyplot.axis("equal")
    
    pyplot.title("data analyze")
    pyplot.show()
    Copy the code

  • Draw a scatter plot

    • Scatter plot using Pyplot.Scatter (x,y)
    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("Elements of a")
    pyplot.ylabel(Elements of "b")
    
    pyplot.show()
    Copy the code

conclusion

In this installment, we’ll take a brief look at the matplotlib.pyplot module for plotting related charts such as broken lines, bars, scatters, and pie charts

During the learning process, we found the PyPlot module easy to use and found that all of our data before the presentation was the key point

That’s the content of this episode. Please give us your thumbs up and comments. See you next time