SoFunction
Updated on 2024-10-29

Python data analysis using matplotlib line graphs, bar charts and mixed bar charts

Introduction to matplotlib

  • Matplotlib is a plotting library for Python. It can be used with NumPy, providing an effective open-source alternative to MatLab. It can also be used with graphics toolkits such as PyQt and wxPython.
  • Install Matplotlib library command: type pip install matplotlib in the cmd command window.

matplotlib plotting line graphs

1, draw a folded line of the folded line graph

# -*- coding:utf-8 -*-
import matplotlib
import  as plt
# Handling garbled code
['-serif'] = ['SimHei']  # Chinese in bold
x = [1, 2, 3, 4]
y = [10, 50, 20, 100]
# "r" means red, ms used to set the size of the *
(x, y, "r", marker='*', ms=10, label="a")
# ([1, 2, 3, 4], [20, 30, 80, 40], label="b")
(rotation=45)
("Date of release")
("Number of novels")
("80Novels.com Active.")
# upper left Display legend a to the upper left corner
(loc="upper left")
# Display specific values on a line graph, ha parameter controls horizontal alignment, va controls vertical alignment
for x1, y1 in zip(x, y):
    (x1, y1 + 1, str(y1), ha='center', va='bottom', fontsize=20, rotation=0)
("")
()

Graphic effect display:

Attention:savefig() is the graphical storage into thephotographshow() is to display the graph.

2, drawing multiple folding lines

# -*- coding:utf-8 -*-
import matplotlib
import  as plt
['-serif'] = ['SimHei']  # Chinese in bold
x = [1, 2, 3, 4]
y1 = [45, 50, 20, 100]
y2 = [26, 10, 76, 25]
y3 = [11, 66, 55, 88]
y4 = [69, 50, 35, 100]
(x, y1, marker='*', ms=10, label="a")
(x, y2, marker='*', ms=10, label="b")
(x, y3, marker='*', ms=10, label="c")
(x, y4, marker='*', ms=10, label="d")
(rotation=45)
("Date of release")
("Number of novels")
("80Novels.com Active.")
(loc="upper left")
# Display specific values on a line graph, ha parameter controls horizontal alignment, va controls vertical alignment
for y in [y1, y2, y3, y4]:
    for x1, yy in zip(x, y):
        (x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
("")
()

Graphic effect display:

Matplotlib Plotting Histograms

1. Plotting ordinary bar charts

# -*- coding:utf-8 -*-
import matplotlib
import  as plt
['-serif'] = ['SimHei']  # Chinese in bold
# Construct data
x = [1, 2, 3, 4]
y = [450, 500, 200, 1000]
# Mapping
(x=x, height=y, label='The Complete Book Library', color='steelblue', alpha=0.8)
# Display specific values on a bar chart, ha parameter controls horizontal alignment, va controls vertical alignment
for x1, yy in zip(x, y):
    (x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
# Setting the title
("80Novels.com Active.")
# Set names for both axes
("Date of release")
("Number of novels")
# Show legend
()
("")
()

Graphic effect display:

2. Plotting multiple histograms

# -*- coding:utf-8 -*-
import matplotlib
import  as plt
['-serif'] = ['SimHei']  # Chinese in bold
# Construct data
x = ['2015', '2016', '2017', '2018', '2019']
y1 = [4500, 5000, 2000, 7000, 10000]
y2 = [5200, 7000, 5000, 9000, 11000]
# Mapping
(x=x, height=y1, label='python', color='steelblue', alpha=0.8)
(x=x, height=y2, label='java', color='indianred', alpha=0.8)
# Display specific values on a bar chart, ha parameter controls horizontal alignment, va controls vertical alignment
for x1, yy in zip(x, y1):
    (x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
for x1, yy in zip(x, y2):
    (x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
# Setting the title
("python vs. java books.")
# Set names for both axes
("Year")
("Sales.")
# Show legend
()
("")
()

Graphic effect display:

3, draw a bar chart of the bar column side-by-side display

# -*- coding:utf-8 -*-
import numpy as np
import matplotlib
import  as plt

['-serif'] = ['SimHei']  # Chinese in bold
# Construct data
x = ['2015', '2016', '2017', '2018', '2019']
y1 = [4500, 5000, 2000, 7000, 10000]
y2 = [5200, 7000, 5000, 9000, 11000]
bar_width = 0.3
# Change the x-axis data to use range(len(x_data), which is 0, 1, 2...
(x=range(len(x)), height=y1, label='python', color='steelblue', alpha=0.8, width=bar_width)
# Change the x-axis data to use (len(x_data))+bar_width, the
# is bar_width, 1+bar_width, 2+bar_width... This is then juxtaposed with the first bar chart
(x=(len(x)) + bar_width, height=y2, label='java', color='indianred', alpha=0.8, width=bar_width)
# Display specific values on a bar chart, ha parameter controls horizontal alignment, va controls vertical alignment
for x1, yy in enumerate(y1):
    (x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
for x1, yy in enumerate(y2):
    (x1 + bar_width, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
# Setting the title
("python vs. java.")
# Set names for both axes
("Year")
("Sales.")
# Show legend
()
("")
()

Graphic effect display:

Matplotlib plots a mixture of bars and lines

1, drawing a mixture of columns and lines

# -*- coding:utf-8 -*-
import matplotlib
import  as plt
['-serif'] = ['SimHei']  # Chinese in bold
# Construct data
x = [2, 4, 6, 8]
y = [450, 500, 200, 1000]
# Mapping
(x=x, height=y, label='The Complete Book Library', color='steelblue', alpha=0.8)
# Display specific values on a bar chart, ha parameter controls horizontal alignment, va controls vertical alignment
for x1, yy in zip(x, y):
    (x1, yy + 1, str(yy), ha='center', va='bottom', fontsize=20, rotation=0)
# Setting the title
("80Novels.com Active.")
# Set names for both axes
("Date of release")
("Number of novels")
# Show legend
()
# Drawing line graphs
(x, y, "r", marker='*', ms=10, label="a")
(rotation=45)
(loc="upper left")
("")
()

Graphic effect display:

summarize

To this point this article on Python data analysis using matplotlib to draw line graphs, bar charts and bar mixed graphs of the article is introduced to this, more related Python matplotlib draw line graphs bar graphs bar mixed graphs content, please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!