Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw Matplotlib Line Chart by Python

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to draw a Matplotlib line chart by Python". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Matplotlib drawing

In data analysis, data visualization is also very important, through the intuitive display of the process, the results of data, can help us to clearly understand the data, and then better analysis. Next, let's talk about the Matplotlib library, a data visualization tool in Python data analysis.

Matplotlib is a very powerful Python 2D drawing library, using it, we can show data more intuitively in the form of charts, achieve data visualization, it is also very convenient to use, and support the drawing of line chart, bar chart, pie chart, histogram, scatter chart and so on.

You can install using the following command:

Simple example of pip install matplotlib# or conda install matplotlib

Draw the y=2x+1 equation diagram:

Import matplotlib.pyplot as plt# creates figure object (drawing object) plt.figure (figsize= (4,6)) # drawing image x = [1,2,3,4] y = [2*i+1 for i in x] plt.plot (x, y) # display image plt.show ()

The results are as follows:

Where:

Pyplot is a sublibrary of Matplotlib, which provides drawing interfaces and functions, and can draw charts quickly.

Plt.figure (figsize= (4,6)) means to create a drawing object. If it is not created, calling the plot function will automatically create a drawing object. The syntax format is as follows:

Plt.figure (num=None, figsize=None, dpi=None,)

Commonly used parameters are described as follows:

Num: represents the drawing number, which can be int or string type. The default is None. If you do not transfer, a new drawing is created, and the drawing number is incremented; a reference to the drawing corresponding to that number is passed.

Figsize: represents the width and height of the canvas and receives an integer tuple

Dpi: represents the resolution of the drawing object

The plt.plot () function represents the drawing of a chart in the current drawing object, and x and y represent the data on the x and y axes, respectively.

Plt.show () indicates that the image is displayed, and the figure object resources are released after display.

Second, line chart drawing

A line chart is a graph that connects data points sequentially, which can reflect the variation of the variable y with the variable x. Matplotlib provides the plot () function to draw a line chart, and its syntax format is as follows:

Plt.plot (* args, * * kwargs)

The common parameters and descriptions are as follows:

X, y: represent the data corresponding to the x-axis and y-axis, respectively, and receive the list type parameters

Color: indicates the color of the broken line

Marker: indicates the type of point on the broken line, such as ".", "o", "v", and so on.

Linestyle: indicates the type of broken line, defaults to "-", represents solid line, sets it to "-" indicates long dashed line, sets it to "-." Represents a dotted line, and is set to ":" to indicate a dotted line

Linewidth: indicates the thickness of the broken line

Alpha: indicates the transparency of the point and receives the decimal between 0: 1

For example, draw a line chart of the average temperature change in an area from Monday to Sunday:

Import matplotlib.pyplot as pltplt.figure (figsize= (10,8)) # mean temperature data from Monday to Sunday plt.plot ([1, 2, 3, 4, 5, 6, 7], [12, 11, 11, 13, 13, 10, 10]) plt.show ()

The output is as follows:

But as you can see, the chart is not very good-looking. We can add some labels and legends to the chart to make it clearer and better-looking.

The specific methods are as follows:

Plt.title (): specifies the title of the current chart, including name, location, color, font size, etc.

Plt.xlabel (): specifies the name, location, color, font size, etc., of the x-axis of the current chart.

Plt.ylabel (): specifies the name, location, color, font size, etc., of the y-axis of the current chart.

Plt.xlim (): specifies the range of the x-axis of the current chart

Plt.ylim (): specifies the range of the y-axis of the current chart

Plt.xticks (): specifies the x-axis scale of the current chart

Plt.yticks (): specifies the y-axis scale of the current chart

Import matplotlib.pyplot as plt# settings support Chinese plt.rcParams ['font.family'] = [' SimHei'] plt.figure (figsize= (10, 8)) plt.plot ([1 i for i in range (20)] [:: 5], [12, 11], 11, 13, 13 and 10], line, marker= ".) plt.xlabel (" time ") plt.ylabel (" temperature ") plt.yticks ([i for i in range (20)] [:: 5]) plt.show ()

The output is as follows:

This is the end of the content of "how to draw Matplotlib Line Chart by Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report