Home  >  Article  >  Backend Development  >  Generate a waffle chart using pyWaffle in Python

Generate a waffle chart using pyWaffle in Python

王林
王林forward
2023-08-17 11:49:421271browse

Data visualization is essential for efficient information understanding and presentation. Among the many chart types available, waffle charts are a novel way of displaying data in a grid-like structure with square tiles. The powerful Python module PyWaffle facilitates the development of waffle charts, similar to many computational and data analysis methods. In this article, we will look at how to create a waffle chart using the sophisticated Python module PyWaffle. Let’s install PyWafle and see how to use it to visualize categorical data.

Run the following command in your cmd to install the library and then import it into your code

pip install pywaffle
The Chinese translation of

Example 1

is:

Example 1

In this example, we have examined the monthly sales report of a shopping mall to analyze the sales of each department.

algorithm

Step-1: Import the required libraries. Now start the code by creating a dataset. This dataset will be passed to the library and the waffle chart will be based on this dataset. The dataset created here is also known as data dictionary

Step-2: Name the categories for the dataset and assign values ​​to create the chart. These category names also appear on the chart, so make sure they make sense

Step 3: Use the plt.figure() function to pass parameters

Step-4: Add a title and other parameters to customize the chart.

Step 5: Use the plt.show() method to display the chart.

import matplotlib.pyplot as plt
from pywaffle import Waffle

# 数据
data = {'类别A': 15,
    '类别B': 35,
    '类别C': 50}

# 绘图
fig = plt.figure(
   FigureClass=Waffle,
   rows=5,
   columns=10,
   values=data,
   legend={'loc': 'upper left', 'bbox_to_anchor': (1, 1)},
   colors=['#2196f3', '#ff9800', '#4caf50'],
)

# 添加标题
plt.title('按类别分布的销售情况')

# 显示图表
plt.show()

Output

Generate a waffle chart using pyWaffle in Python

The Chinese translation of

Example 2

is:

Example 2

In this example, we will try to create another chart for the scores of four games of a game.

algorithm

Step-1: List the match results in a data block by creating a data dictionary.

Step 2: Create categories in the dataset and name them with the names of the winning teams

Step-3: Now assign values ​​to create the chart. These values ​​are parameters of the chart

Step 4: Use the plt.figure() function and add parameters in it. You can also add a title and other parameters to customize the chart.

Step-5: Use the plt.show() method to display the chart.

import matplotlib.pyplot as plt
from pywaffle import Waffle

# 数据
data = {'澳大利亚队': 230,
    '英格兰队': 290,
    '印度队': 250}

# 绘图
fig = plt.figure(
   FigureClass=Waffle,
   rows=5,
   columns=10,
   values=data,
   legend={'loc': 'upper left', 'bbox_to_anchor': (1, 1)},
   colors=['#2196f3', '#ff9800', '#4caf50'],
)

# 添加标题
plt.title('比赛得分')

# 显示图表
plt.show()

Output

Generate a waffle chart using pyWaffle in Python

in conclusion

PyWaffle makes it easy to create waffle charts in Python, allowing users to easily visualize categorical data. There are many customizable options, including icons, icon legends, block aspect ratio, and size. Waffle charts that incorporate these factors tend to be more educational and engaging. PyWaffle can be a useful tool when it comes to data visualization, so try using this library to add more functionality to your application

The above is the detailed content of Generate a waffle chart using pyWaffle in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete