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

What is the Ring Graph in Python data Visualization

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

Share

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

Today, I'll show you what the ring graph looks like in Python data visualization. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.

1. Introduction

The ring chart (circle) is functionally the same as the pie chart, the whole ring is divided into different parts, and each arc is used to represent the proportional value of each data. However, the blank space in the center can be used to display other related data, which provides a richer output of data information than the standard pie chart.

In this article, we will introduce two methods of drawing donuts in Matplolib. Simple methods of using pie charts and parameter wedgeprops, as well as complex methods of using polar and horizontal bar charts.

two。 Method 1: pie chart form

There is no direct way to draw a doughnut chart in Matplotlib, but we can use the parameter wedgeprops in the pie chart to quickly convert the pie chart into a ring chart.

First, let's draw a simple pie chart:

Import matplotlib.pyplot as pltplt.pie ([87, 13], startangle=90, colors= ['# 5DADE2regions,'# 515A5A']) plt.show ()

The results are as follows:

Then we add the parameter wedgeprops and define the width of the edge of the ring graph, as follows:

Fig, ax = plt.subplots (figsize= (6,6)) ax.pie ([87 width':0.3 13], wedgeprops= {'width':0.3}, startangle=90, colors= [' # 5DADE2regions,'# 515A5A']) plt.show ()

The results are as follows:

There's nothing to it. Now we can use the central space to make our data more obvious.

The code is as follows:

Fig, ax = plt.subplots (figsize= (6,6)) wedgeprops= {'width':0.3,' edgecolor':'black', 'linewidth':3} ax.pie ([87 linewidth':3 13], wedgeprops=wedgeprops, startangle=90, colors= [' # 5DADE2,'# 515A5A']) plt.title ('Worldwide Access to Electricity', fontsize=24, loc='left') plt.text (0,0, "87%", ha='center', va='center', fontsize=42) plt.text (- 1.2,-1.2) "Source: ourworldindata.org/energy-access", ha='left', va='center', fontsize=12) plt.show ()

The results are as follows:

The donut chart is especially useful when we have a simple comparison to display. In my opinion, the best way to use them is like a circular progress bar, for example, we have an example of a single scale to highlight.

Of course, we can further simplify the chart above.

The code is as follows:

Fig, ax = plt.subplots (figsize= (6,6)) data = [87,13] wedgeprops= {'width':0.3,' edgecolor':'black', 'lw':3} patches, _ = ax.pie (data, wedgeprops=wedgeprops, startangle=90, colors= [' # 5DADE2signals, 'white']) patches [1] .set _ zorder (0) patches [1] .set _ edgecolor (' white') plt.title ('Worldwide Access to Electricity', fontsize=24, loc='left') plt.text (0,0) F "{data [0]}%", ha='center', va='center', fontsize=42) plt.text (- 1.2,1.3, "Source: ourworldindata.org/energy-access", ha='left', va='top', fontsize=12) plt.show ()

The results are as follows:

3. Method 2: bar chart form

Although this solution is more complex than the previous one, it provides some exciting options for customization.

Let's start with a simple example with the following code:

From math import pifig, ax = plt.subplots (figsize= (6,6), subplot_kw= {'projection':'polar'}) data = 87 startangle = 90x = (data * pi * 2) / 100 # convert x data from percentageleft = (startangle * pi * 2) / 360 # convert start from angleax.barh (1, x, left=left, height=1, color='#5DADE2') plt.ylim (- 3,3) plt.show ()

The results are as follows:

Then let's deal with the angle. We have to convert the x-coordinate of each element before adding it to the axis.

The code is as follows:

From math import pifig, ax = plt.subplots (figsize= (6,6), subplot_kw= {'projection':'polar'}) data = 87startangle = 90x = (data * pi * 2) / 100left = (startangle * pi * 2) / 360 # this is to control where the bar startsplt.xticks ([]) plt.yticks ([]) ax.spines.clear () ax.barh (1, x, left=left, height=1, color='#5DADE2') plt.ylim (- 3,3) plt.text (0,-3, "87%") Ha='center', va='center', fontsize=42) plt.show ()

The results are as follows:

Using this method, we achieve the same effect as above; of course, we add multiple progress bars and define the distance between them to make the visualization richer.

The code is as follows:

From math import piimport numpy as npfrom matplotlib.patches import Patchfrom matplotlib.lines import Line2Dfig, ax = plt.subplots (figsize= (6,6)) ax = plt.subplot (projection='polar') data = [82,75,91] startangle = 90colors = ['# 4393E5eggs,'# 43BAE5eggs,'# 7AE6EA'] xs = [(I * pi * 2) / 100 for i in data] ys = [- 0.2,1 2.2] left= (startangle * pi * 2) / 360 # this is to control where the bar starts# plot bars and points at the end to make them roundfor i, x in enumerate (xs): ax.barh (ys [I], x, left=left, height=1, color=colors [I]) ax.scatter (x+left, ys [I], color=colors [I], zorder=2) ax.scatter (left, ys [I], slug 350, color=colors [I], zorder=2) plt.ylim (- 4) 4) # legendlegend_elements = [Line2D ([0], [0], marker='o', color='w', label='Group blocks, markerfacecolor='#4393E5', markersize=10), Line2D ([0], [0], marker='o', color='w', label='Group bands, markerfacecolor='#43BAE5', markersize=10), Line2D ([0], [0], marker='o', color='w', label='Group cations, markerfacecolor='#7AE6EA') Markersize=10)] ax.legend (handles=legend_elements, loc='center', frameon=False) # clear ticks, grids, spinesplt.xticks ([]) plt.yticks ([]) ax.spines.clear () plt.show ()

The results are as follows:

What can python do Python is a programming language, built in many effective tools, Python is almost omnipotent, the language is easy to understand, easy to start, powerful, in many fields, such as the most popular big data analysis, artificial intelligence, Web development and so on.

These are all the contents of the ring graph in Python data visualization, and you can search the previous articles or browse the following articles to learn more about how the ring diagram in Python data visualization is related. I believe the editor will add more knowledge to you. I hope you can support it!

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