Learn to draw line charts, bar charts and pie charts with Python in three minutes

WBOY
Release: 2023-09-27 09:29:13
Original
1205 people have browsed it

Learn to draw line charts, bar charts and pie charts with Python in three minutes

Learn to draw line charts, bar charts and pie charts with Python in three minutes

Python is a very popular programming language that is widely used in data analysis and visualization. In this article, we will learn how to draw three common types of charts using Python: line charts, bar charts, and pie charts. I'll provide you with specific code examples to help you get started quickly.

  1. Line chart

A line chart is a type of chart that shows trend changes by connecting data points. In Python, we can use the matplotlib library to draw line graphs. The following is a simple example showing how to draw a line graph using Python:

import matplotlib.pyplot as plt # 数据 x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # 绘制线形图 plt.plot(x, y) # 添加标题和标签 plt.title('线形图') plt.xlabel('x轴') plt.ylabel('y轴') # 显示图表 plt.show()
Copy after login

In the above code, we first imported thematplotlib.pyplotmodule, and then defined the x-axis and y-axis data, and then use theplotfunction to draw a line graph. Finally, we added titles and labels and displayed the chart using theshowfunction.

  1. Bar chart

Histogram is a chart type that represents data through the height of a rectangle. In Python, we can also use the matplotlib library to draw histograms. Here is a simple example showing how to draw a histogram using Python:

import matplotlib.pyplot as plt # 数据 x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # 绘制柱状图 plt.bar(x, y) # 添加标题和标签 plt.title('柱状图') plt.xlabel('x轴') plt.ylabel('y轴') # 显示图表 plt.show()
Copy after login

Similar to the line chart, we first import thematplotlib.pyplotmodule and then define the x-axis and y-axis Axis data is plotted using thebarfunction to draw a histogram. Finally, we added titles and labels and displayed the chart using theshowfunction.

  1. Pie chart

A pie chart is a type of chart that represents data through the angles of a sector. In Python, we can use matplotlib library to draw pie charts. The following is a simple example showing how to draw a pie chart using Python:

import matplotlib.pyplot as plt # 数据 sizes = [30, 40, 20, 10] # 标签 labels = ['A', 'B', 'C', 'D'] # 绘制饼图 plt.pie(sizes, labels=labels, autopct='%1.1f%%') # 添加标题 plt.title('饼图') # 显示图表 plt.show()
Copy after login

In the above code, we first imported thematplotlib.pyplotmodule, then defined the data and labels, using Thepiefunction draws a pie chart. We can use thelabelsparameter to add labels and theautopctparameter to control the display percentage. Finally, we added a title and displayed the chart using theshowfunction.

Through the above examples, we can see that it is very simple to draw line charts, bar charts and pie charts using Python. You can modify the data and styles to suit your needs to create more customized charts. I hope this article can help you quickly master the basics of Python drawing.

The above is the detailed content of Learn to draw line charts, bar charts and pie charts with Python in three minutes. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!