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 Classic Pie Chart and Bar Chart by matplotlib in Python

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how matplotlib in Python draws classic pie charts and bar charts, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.

Matplotlib is a Python toolkit based on numpy. This package provides a wealth of data drawing tools, mainly used to draw some statistical graphics. You can find a wide variety of examples:

Through data mapping, we can convert boring numbers into charts that are easily accepted by people, thus leaving a more impressive impression. In fact, more than a hundred years ago, Nightingale used statistical graphics to persuade the British government to improve the health of the army.

We will take GDP data as an example to see how to draw classic pie and bar charts.

data

Here is the data we will use for the top 10 GDP countries in 2011 and their specific GDP:

USA 15094025China 11299967India 4457784Japan 4440376Germany 3099080Russia 2383402Brazil 2293954UK 2260803France 2217900Italy 1846950 pie chart

Let's first draw the pie chart (pie plot). The pie chart is suitable for expressing the percentage of GDP in each country. The area of each piece represents the percentage:

The specific code is as follows. You can see that we mainly use the matplotlib.pyplot toolkit:

# Make a pie chart# This script is written by Vamei, http://www.cnblogs.com/vamei# you may freely use it.import matplotlib.pyplot as plt# quants: GDP# labels: country namelabels = [] quants = [] # Read datafor line in file ('.. / data/major_country_gdp'): info = line.split () labels.append (info [0]) quants.append (float (info [1])) # make a square figureplt.figure (1, figsize= (6)) # For China Make the piece explode a bitdef explode (label, target='China'): if label = = target: return 0.1else: return 0expl = map (explode,labels) # Colors used. Recycle if not enough.colors = ["pink", "coral", "yellow", "orange"] # Pie Plot# autopct: format of "percent" string;plt.pie (quants, explode=expl, colors=colors, labels=labels, autopct='%1.1f%%',pctdistance=0.8, shadow=True) plt.title ('Top 10 GDP Countries', bbox= {' facecolor':'0.8', 'pad':5}) plt.show () bar chart

Let's try a bar chart (bar plot). The height of each bar represents the GDP of each country. The higher the bar, the higher the GDP:

The code is as follows:

"" Make a pie chartThis script is written by Vamei Http://www.cnblogs.com/vameiyou may freely use it. "" import matplotlib.pyplot as pltimport numpy as np# quants: GDP# labels: country namelabels = [] quants = [] # Read datafor line in file ('.. / data/major_country_gdp'): info = line.split () labels.append (info [0]) quants.append (float (info [1])) width = 0.4ind = np.linspace (0.5, 9.5) # make a square figurefig = plt.figure (1) Figsize= # Bar Plotax.bar (ind-width/2,quants,width,color='coral') # Set the ticks on x-axisax.set_xticks (ind) ax.set_xticklabels (labels) # labelsax.set_xlabel ('Country') ax.set_ylabel (' GDP (Billion US dollar)') # titleax.set_title ('Top 10 GDP Countries', bbox= {' facecolor':'0.8', 'pad':5}) plt.show ()

In this code, we use the ax object to control the scale and the country name corresponding to the scale. This is a little different from what we did at pie plot (pie plot can do the same, but it's not necessary).

These are all the contents of the article "how to draw classic pie charts and bar charts by matplotlib in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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