Python's Matplotlib histogram (vertical histogram and horizontal histogram), histogram and pie chart

  • To start, we import matplotlib and numpy libraries.
from matplotlib import pyplot as plt
import numpy as np
  • Set the basic configuration, set the Chinese font as bold, excluding the Chinese minus sign, set the resolution to 100, and set the image display size to (5,3).
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['figure.dpi'] = 100
plt.rcParams['figure.figsize'] = (5,3)

1. Histogram

  • A histogram is a chart that uses rectangular columns to represent categories of data.
  • Histograms can be drawn vertically or horizontally.
  • Its height is directly proportional to the value it represents.
  • The histogram shows the comparative relationship between different categories, the horizontal axis X of the chart specifies the category being compared, and the vertical axis Y indicates the specific category value

insert image description here

2. Vertical histogram

matplotlib.pyplot.bar(x, height, width: float = 0.8, bottom = None, *, align: str = ‘center’, data = None, **kwargs)
  • The specific meanings of its parameters are as follows:
  • x represents the x coordinate, and the data type is float type, which is generally a fixed-step list generated by np.arange().
  • height indicates the height of the histogram, that is, the y coordinate value, the data type is float type, generally a list, including all the y values ​​for generating the histogram.
  • width indicates the width of the histogram, the value is between 0 and 1, and the default value is 0.8.
  • bottom indicates the starting position of the histogram, that is, the starting coordinate of the y-axis, and the default value is None.
  • align indicates the center position of the histogram, "center", "lege" edge, the default value is 'center'.
  • color indicates the color of the histogram, the default is blue.
  • alpha means transparency, the value is between 0 and 1, and the default value is 1.
  • label represents the label, which needs to be generated by calling plt.legend() after setting.
  • edgecolor represents the border color (ec).
  • linewidth represents border width, float or array-like, default is None (lw).
  • tick_label indicates the tick label of the column, a string or a list of strings, the default value is None.
  • linestyle Indicates the line style (ls).

1. Basic histogram

  • We can simply draw a histogram for observation, the x-axis data is generated by the range function, and the y-axis data can be set as an array.
import matplotlib.pyplot as plt
x = range(5)
​data = [5, 20, 15, 25, 10]
​plt.title("基本柱状图")
plt.grid(ls="--", alpha=0.5)
plt.bar(x, data)

insert image description here

  • (1) The bottom parameter.
  • The bottom parameter indicates the starting position of the histogram, that is, the starting coordinate of the y-axis, and the default value is None.
  • We still use the same data as the above example, but the difference is that we modify the starting point of the y-axis data.
  • It should be noted here that the array set by the bottom parameter corresponds to the parameter of the y-axis data one by one, and the shape of the two arrays must be the same.
import matplotlib.pyplot as plt
x = range(5)
​data = [5, 20, 15, 25, 10]
​plt.title("基本柱状图")
​plt.grid(ls="--", alpha=0.5)
plt.bar(x, data, bottom=[10, 20, 5, 0, 10])

insert image description here

  • (2) The color of the histogram.
  • Regarding the color of the histogram, we also use the above data to operate. At this time, the bottom parameter of the histogram is not set, and at the same time, the color of the histogram is set to green.
import matplotlib.pyplot as plt
x = range(5)
data = [5, 20, 15, 25, 10]
​plt.title("设置柱状图颜色")
plt.grid(ls="--", alpha=0.5)
​plt.bar(x, data ,facecolor="green")
#plt.bar(x, data ,color="green")

insert image description here

  • Then, in some specific cases, the single color of the histogram is not conducive to our subsequent observation. Therefore, we can use the facecolor function to set the color of each histogram separately.
import matplotlib.pyplot as plt
x = range(5)
data = [5, 20, 15, 25, 10]
plt.title("color参数设置柱状图不同颜色")
plt.grid(ls="--", alpha=0.5)
plt.bar(x, data ,color=['r', 'g', 'b'])

insert image description here

  • (3) Histogram stroke.
  • The relevant keyword arguments for strokes are:
  • edgecolor or ec.
  • linestyle 或 ls。
  • linewidth or lw.
  • For details, see the following specific examples.
import matplotlib.pyplot as plt​
data = [5, 20, 15, 25, 10]
plt.title("设置边缘线条样式")
plt.bar(range(len(data)), data, ec='r', ls='--', lw=2)

insert image description here

2. Multiple histograms at the same position

  • Draw multiple histograms at the same x-axis position, mainly by adjusting the width of the histogram and the starting position of the x-axis of each histogram.
  • For example, we have the gold, silver, bronze and total medal counts for Norway, Germany, China, USA, and Sweden in the 2022 Winter Olympics.

insert image description here

  • For this, we need to draw the following graph:

insert image description here

  • Analyzing the histogram, we can get:
  • (1) This example needs to calculate the x-axis, so the value of the x-axis needs to be rotated.
  • (2) Determine the starting position of the x-axis of each histogram in the same x-axis.
  • (3) It is necessary to set the width of the graphics.
  • (4) The starting position of graphic 2 = the starting position of graphic 1 + the width of the graphic.
  • (5) The starting position of graphic 3 = the starting position of graphic 1 + 2 times the width of the graphic.
  • (6) The text content needs to be displayed cyclically for each histogram.
  • (7) A legend needs to be displayed.
  • The specific histogram drawing process is as follows:
  • First, we need to import countries and the number of gold, silver and bronze medals for each country.
countries = ['挪威', '德国', '中国', '美国', '瑞典']
gold_medal = [16, 12, 9, 8, 8]
silver_medal = [8, 10, 4, 10, 5]
bronze_medal = [13, 5, 2, 7, 5]
  • At this point, if we draw directly, we will find that the medal histograms of each country overlap, and the width of each histogram is too large.
  • This is because they all default the starting coordinate of the y-axis, that is, the bottom parameter is 0, and the width parameter is 0.8 by default because it is not set.
plt.bar(countries, gold_medal,color="gold")
plt.bar(countries,silver_medal,color="silver")
plt.bar(countries,bronze_medal,color="red")

insert image description here

  • In view of the above-mentioned problems in the drawing process and the sample map of the final result, we need to draw the gold, silver and bronze histograms of each country together, so we need to calculate the coordinates of the country parameters on the x-axis.
  • Since strings cannot be directly used for arithmetic operations, we use np.arange to convert them into arrays, and then return the string country, and set the width of the histogram to 0.2
x = np.arange(len(countries))
print(x)
width = 0.2
#[0 1 2 3 4]
  • We then determine the starting position of the Gold, Silver and Bronze histograms for each country.
  • The starting position of the gold medal is the x value corresponding to each country, the starting position of the silver medal is the corresponding x value of each country plus the width of the gold histogram, the starting position of the bronze medal is similar to the silver medal, plus twice the The position of the gold medal is enough (here we set the width of each histogram to 0.2).
gold_x = x​
silver_x = x + width
​bronze_x = x + 2 * width
  • After the above operation is completed, the graphs can be drawn separately.
plt.bar(gold_x,gold_medal,width=width,color="gold")
​plt.bar(silver_x,silver_medal,width=width,color="silver")
​plt.bar(bronze_x,bronze_medal,width=width, color="saddlebrown")

insert image description here

  • At this point, we notice that the abscissa of the histogram plotted is the number and has not yet returned our country parameter.
  • For this, we need to change the coordinates of the x-axis back.
plt.xticks(x+width, labels=countries)

insert image description here

  • Finally, we display the height parameter and legend of each histogram, and we get the image we need.
for i in range(len(countries)):
    plt.text(gold_x[i],gold_medal[i], gold_medal[i],va="bottom",ha="center",fontsize=8)
    plt.text(silver_x[i],silver_medal[i], gold_medal[i],va="bottom",ha="center",fontsize=8)
    plt.text(bronze_x[i],bronze_medal[i], gold_medal[i],va="bottom",ha="center",fontsize=8)  
plt.legend()

insert image description here

  • Code summary:
#库导入
from matplotlib import pyplot as plt
import numpy as np

#参数设置
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['figure.dpi'] = 100
plt.rcParams['figure.figsize'] = (5,3)

#国家和奖牌数据导入
countries = ['挪威', '德国', '中国', '美国', '瑞典']
gold_medal = [16, 12, 9, 8, 8]
silver_medal = [8, 10, 4, 10, 5]
bronze_medal = [13, 5, 2, 7, 5]

#将横坐标国家转换为数值
x = np.arange(len(countries))
width = 0.2

#计算每一块的起始坐标
gold_x = x
silver_x = x + width
bronze_x = x + 2 * width

#绘图
plt.bar(gold_x,gold_medal,width=width,color="gold",label="金牌")
plt.bar(silver_x,silver_medal,width=width,color="silver",label="银牌")
plt.bar(bronze_x,bronze_medal,width=width, color="saddlebrown",label="铜牌")

#将横坐标数值转换为国家
plt.xticks(x + width,labels=countries)

#显示柱状图的高度文本
for i in range(len(countries)):
    plt.text(gold_x[i],gold_medal[i], gold_medal[i],va="bottom",ha="center",fontsize=8)
    plt.text(silver_x[i],silver_medal[i], gold_medal[i],va="bottom",ha="center",fontsize=8)
    plt.text(bronze_x[i],bronze_medal[i], gold_medal[i],va="bottom",ha="center",fontsize=8)  

#显示图例
plt.legend(loc="upper right") 
  • Other knowledge points: Rotate X-axis tick label text in Matplotlib.
  • (1) plt.xticks(rotation= ) Rotate the Xticks label text.
  • (2) fig.autofmt_xdate(rotation= ) Rotate the Xticks label text.
  • (3) ax.set_xticklabels(xlabels, rotation= ) Rotate the Xticks label text.
  • (4) plt.setp(ax.get_xticklabels(), rotation=) Rotate the Xticks label text.
  • (5) ax.tick_params(axis='x', labelrotation= ) Rotate the Xticks label text.

3. Stacked column chart

  • The so-called stacked histogram is to stack the histograms of different groups together, and the height of the stacked histogram shows the result value of the addition of the two. As shown in the picture:

insert image description here

  • Analyzing this graph gives:
  • (1) The initial height of the gold medal list is: bronze medal data + silver medal data.
  • (2) The starting height of the silver medal list is: silver medal height.
  • (3) The starting height of the bronze medal list is: 0.
  • (4) The addition of data at the starting position requires knowledge of numpy.
  • (6) It is necessary to determine the color of the histogram.
  • (7) Display the legend.
  • The specific drawing method is completely similar to the previous multi-bar graph with the same position, the difference is that we need to pay attention to the numerical calculation of bottom, and at the same time, the starting coordinates of the gold, silver and bronze medals are exactly the same.
#库导入
from matplotlib import pyplot as plt
import numpy as np

#参数设置
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['figure.dpi'] = 100
plt.rcParams['figure.figsize'] = (5,3)

#国家和奖牌数据输入、柱状图宽度设置
countries = ['挪威', '德国', '中国', '美国', '瑞典']
gold_medal = np.array([16, 12, 9, 8, 8])
silver_medal = np.array([8, 10, 4, 10, 5])
bronze_medal = np.array([13, 5, 2, 7, 5])
​width = 0.3

#绘图
plt.bar(countries, gold_medal, color='gold', label='金牌',
        bottom=silver_medal + bronze_medal,width=width)
plt.bar(countries, silver_medal, color='silver', label='银牌', bottom=bronze_medal,width=width)
​plt.bar(countries, bronze_medal, color='#A0522D', label='铜牌',width=width)

#设置y轴标签,图例和文本值
​plt.ylabel('奖牌数')
​plt.legend(loc='upper right')for i in range(len(countries)):
    max_y = bronze_medal[i]+silver_medal[i]+gold_medal[i]
    plt.text(countries[i], max_y, max_y, va="bottom", ha="center")

insert image description here

3. Horizontal histogram

1. Basic histogram

  • A horizontal bar chart can be generated by calling the barh() function in Matplotlib.
  • The usage of the barh() function is basically the same as that of the bar() function, except that when calling the barh() function, the y parameter is used to pass in the y-axis data, and the width parameter is used to pass in the data representing the bar width.
  • The ha (horizontal alignment) control text's x position parameter represents the left, middle or right side of the text's bounding box.
  • The va (vertical alignment) control text's y-position parameter represents the bottom, center, or top of the text's bounding box.
plt.barh(y, width, height=0.8, left=None, *, align='center', **kwargs)
  • For example, we take the number of national gold medals in the vertical histogram as an example to draw.
countries = ['挪威', '德国', '中国', '美国', '瑞典']
gold_medal = np.array([16, 12, 9, 8, 8])
plt.barh(countries, width=gold_medal)

insert image description here

2. Multiple histograms at the same position

  • Draw multiple histograms at the same y-axis position, mainly by adjusting the width of the histogram and the starting position of the y-axis of each histogram.
  • For example, we have data on box office changes for 3 movies over the last three days.
movie = ['新蝙蝠侠', '狙击手', '奇迹笨小孩']
real_day1 = [4053, 2548, 1543]
real_day2 = [7840, 4013, 2421]
real_day3 = [8080, 3673, 1342]
  • For this, we need to draw the following image.
    insert image description here
  • Analyzing this graph gives:
  • (1) Since the calculation of the height is involved, first convert the y-axis to a numerical value.
  • (2) It is necessary to set the height of the same graph.
  • (3) Calculate the starting position of each graphic height.
  • (4) Draw graphics.
  • (5) Replace the y-axis data.
  • Due to the calculation involved, the data is converted into a numpy array, and the method of drawing horizontal multi-bar graphs with positions is exactly the same as that of vertical ones, so I won't describe them here.
#库导入
from matplotlib import pyplot as plt
import numpy as np

#参数设置
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['figure.dpi'] = 100
plt.rcParams['figure.figsize'] = (5,3)

#数据的输入
movie = ['新蝙蝠侠', '狙击手', '奇迹笨小孩']
real_day1 = np.array( [4053, 2548, 1543])
​real_day2 = np.array([7840, 4013, 2421])
​real_day3 = np.array([8080, 3673, 1342])
​​
#y轴转换为数值型
num_y = np.arange(len(movie))
#设置同图形的高度
height = 0.2
#计算每个图形高度的起始位置
movie1_start_y = num_y              
movie2_start_y = num_y + height      
movie3_start_y = num_y + 2 * height  ​
#绘制图形​
plt.barh(movie1_start_y, real_day1, height=height)
plt.barh(movie2_start_y, real_day2,  height=height)
plt.barh(movie3_start_y, real_day3, height=height)
# 计算宽度值和y轴值,替换y轴数据
​plt.yticks(num_y + height, movie)

insert image description here

3. Stacked column chart

  • The horizontal stacked histogram is similar to the vertical stacked histogram in that different groups of histograms are stacked together, and the height of the stacked histogram shows the result value of the addition of the two. As shown in the picture:

insert image description here

  • Analyzing this graph gives:
  • (1) Determine the position of the graph from the left side.
  • (2) Set the same width.
  • (3) Draw graphics and set the left parameter.
  • (4) Label the data.
  • Since calculations are involved in the process, the data needs to be transferred to numpy arrays.
  • The other parts are similar to the process of drawing a vertical stacked histogram, so I won't go into details here.
#库导入
from matplotlib import pyplot as plt
import numpy as np

#参数设置
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['figure.dpi'] = 100
plt.rcParams['figure.figsize'] = (5,3)

#数据的输入
movie = ['新蝙蝠侠', '狙击手', '奇迹笨小孩']
real_day1 = np.array( [4053, 2548, 1543])
real_day2 = np.array([7840, 4013, 2421])
real_day3 = np.array([8080, 3673, 1342])
#确定距离左侧​
left_day2 = real_day1​
left_day3 = real_day1 + real_day2 
# 设置线条高度
height = 0.2
# 绘制图形:
plt.barh(movie, real_day1, height=height)
plt.barh(movie, real_day2, left=left_day2, height=height)
plt.barh(movie, real_day3, left=left_day3, height=height) 
# 设置数值文本,计算宽度值和y轴为值​
sum_data = real_day1 + real_day2 +real_day3
for i in range(len(movie)):
    plt.text(sum_data[i], movie[i], sum_data[i],va="center" , ha="left")

insert image description here

4. Histogram plt.hist()

  • Histogram, also known as quality distribution diagram, is a kind of bar graph, which represents the distribution of data by a series of vertical line segments with different heights. The horizontal axis of the histogram represents the data type, and the vertical axis represents the distribution.
  • The difference between histogram and histogram:
  • Histograms are used in probability distributions, which show the probability of a sequence of values ​​occurring within a given range of values.
  • Histograms are used to show the frequency of each category.

insert image description here

histogram histogram
Histograms are generally used to describe the comparison of discrete categorical data Histograms are generally used to describe the distribution relationship of continuous data
The width of each column is fixed, and there will be spacing between the columns The width of each column can be different, and generally there is no spacing
Variables on the horizontal axis can be sorted arbitrarily Horizontal axis variables have certain order rules
  • Segments the range of statistical values, that is, divides the entire range of values ​​into a series of intervals, and then counts how many values ​​are in each interval. Histograms can also be normalized to show relative frequencies. It then shows the fraction belonging to each of several classes whose heights sum to 1.
  • Its syntax template is as follows:
plt.hist(x, bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, normed=None, *, data=None, **kwargs)
  • The specific meanings of its parameters are as follows:
  • x: The data used for the histogram must be a one-dimensional array; multi-dimensional arrays can be flattened before drawing; a required parameter.
  • bins: The number of bars in the histogram, that is, the number of groups to be divided, the default is 10.
  • weights: an array of weights with the same shape as x; each element in x is multiplied by the corresponding weight value and then counted; if normed or density is True, the weights will be normalized. This parameter can be used to draw a histogram of the binned data.
  • density: Boolean, optional. If True, the first element of the returned tuple will normalize the counts to form a probability density, that is, the area (or integral) under the histogram sums to 1. This is achieved by dividing the count by the number of observations multiplied by the width of the bin rather than by the total number of observations. If stacking is also true, then the histogram is normalized to 1 (instead of normed).
  • bottom : array, scalar value or None; the position of the bottom of each bar relative to y=0. If a scalar value, each bar is offset up/down by the same amount relative to y=0. If it is an array, move the corresponding column according to the value of the array element; that is, the upper and lower distances of the histogram.
  • histtype: {'bar', 'barstacked', 'step', 'stepfilled'}; bar is a traditional bar histogram; barstacked is a stacked bar histogram; step is an unfilled bar histogram with only outer Border; stepfilled is a filled histogram; when the value of histtype is step or stepfilled, the rwidth setting is invalid, that is, the interval between columns cannot be specified, and they are connected together by default.
  • align: {'left', 'mid', 'right'}; left means the center of the column is located at the left edge of the bins; mid means the column is located between the left and right edges of the bins; right means the center of the column is located at the right edge of the bins.
  • color: Specific color, array (element is color) or None.
  • label: string (sequence) or None; when there are multiple datasets, use the label parameter to distinguish them.
  • normed: Whether to normalize the obtained histogram vector, that is, to display the proportion, the default is 0, not normalized; it is not recommended to use, it is recommended to use the density parameter instead.
  • edgecolor: The border color of the histogram.
  • alpha: transparency.
  • For example, we can use np.random.randint to randomly generate 300 random data in the interval [140,180] (excluding 180).
x_value = np.random.randint(140,180,300)
plt.hist(x_value, bins=10, edgecolor='white')
plt.title("数据统计")
plt.xlabel("身高")
plt.ylabel("比率")

insert image description here

1. return value

  • n: array or list of arrays, returns the value of the histogram.
  • bins: array, returns the interval range of each bin.
  • patches: A list or a list of lists, returning the data contained in each bin, which is a list.
  • Experiment with the above example and observe the return values ​​of num, bins and patches respectively.
num
#array([25., 28., 34., 39., 29., 25., 37., 34., 26., 23.])

bins_limit
#array([140. , 143.9, 147.8, 151.7, 155.6, 159.5, 163.4, 167.3, 171.2,
#       175.1, 179. ])

for i in patches:
    print(i)
    print(i.get_x())
    print(i.get_y())
    print(i.get_height())
    print(i.get_width())

insert image description here

patches[0].get_width()
#3.9000000000000057
  • Additional knowledge points:
  • xy: xy position (the value of x, bins_limits is the separation value when grouping, and the value of y starts from 0).
  • width: The width is the interval range of each bin (bins_limits is the separation value when grouping).
  • height: The height is the density value (n is the frequency corresponding to the grouping interval).
  • angle: angle.
  • Take the initial histogram as an example to observe the above parameters.

insert image description here

2. Add a line histogram

  • In the histogram, we can also add a line chart to help us view the data changes.
  • First create the Axes object via pyplot.subplots().
  • Call the hist() method through the Axes object to draw the histogram, and return the x, y data required by the line chart.
  • The Axes object then calls plot() to draw the line graph.
  • We can modify the previous code.
#创建一个画布
fig, ax = plt.subplots()
# 绘制直方图
num,bins_limit,patches = ax.hist(x_value, bins=10, edgecolor='white')
# 注意num返回的个数是10,bins_limit返回的个数为11,需要截取
print(bins_limit[:-1])

# 曲线图
ax.plot(bins_limit[:10], num, '--',marker="o")
plt.xticks(bins_limit,rotation=45)

insert image description here

3. Unequally spaced grouping

  • The histograms of the plane are all equidistant, but sometimes we need to get the histogram of unequal distance. At this time, we only need to determine the upper and lower limits of the grouping and specify histtype="bar".
fig, ax = plt.subplots()
x = np.random.normal(100,20,100)
bins = [50, 60, 70, 90, 100,110, 140, 150]
ax.hist(x, bins, color="g",rwidth=0.5)
ax.set_title('不等距分组')
plt.show()

insert image description here

4. Multi-type histogram

  • When we use the histogram to check the frequency of data, we sometimes check the frequency of multiple types of data.
  • At this time, we can pass a variety of data in the form of a list to the x data of the hist() method.
  • We specify 10 groups, each group has ABC three types, and generate 10000, 5000, 2000 values ​​respectively. The actual drawing code is not much different from the single-type histogram, but a legend item is added in the ax.hist function Specify the legend label name first.
n_bins=10
​fig,ax=plt.subplots(figsize=(8,5))
​x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]]
​ax.hist(x_multi, n_bins, histtype='bar',label=list("ABC"))
​ax.set_title('多类型直方图')
​ax.legend()

insert image description here

5. Stacked histogram

  • We sometimes compare the data collected by two groups of different target groups under the same data range.
  • For this, we prepare two sets of data:
x_value = np.random.randint(140,180,200)
x2_value = np.random.randint(140,180,200)
  • Histogram attribute data: Pass in two sets of data in the form of a list.
  • Set histogram stacked: to True to allow data overlay.
plt.hist([x_value,x2_value],bins=10, stacked=True)
#([array([16., 23., 27., 22., 13., 22., 18., 21., 18., 20.]),
#  array([39., 46., 44., 35., 33., 47., 41., 42., 33., 40.])],
# array([140. , 143.9, 147.8, 151.7, 155.6, 159.5, 163.4, 167.3, 171.2,
#        175.1, 179. ]),

insert image description here

5. Pie chart pie()

  • A pie chart is used to display a data series. Specifically, a pie chart displays the percentage of each item in a data series to the sum of the items.
  • Matplotlib provides a pie() function that produces a pie chart of the data in an array. You can use x/sum(x) to calculate each slice's percentage of the pie's sum.
  • The syntax template of the pie() function is as follows:
pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None)
  • The parameters of the pie() function are described as follows:
  • x: array sequence, the array elements correspond to the number and size of the fan-shaped area.
  • labels: A sequence of list strings, with a label name for each fan-shaped area.
  • colors: Set the color for each fan-shaped area. By default, it is automatically set according to the color cycle.
  • autopct: Format the string "fmt%pct", format the label of each slice using a percentage, and place it inside the slice.
  • pctdistance: Set the distance between the percentage label and the center of the circle.
  • labeldistance: Set the distance between each sector label (legend) and the center of the circle.
  • explode: Specifies the highlighting of certain parts of the pie chart, that is, an explosion.
  • shadow: Whether to add the shadow effect of the pie chart.

insert image description here

  • For example, we set the size to (5,5), define the labels of the pie, the amount of each label, and draw the pie chart.
plt.rcParams['figure.figsize'] = (5,5)
labels = ['娱乐','育儿','饮食','房贷','交通','其它']
​x = [200,500,1200,7000,200,900]
​plt.pie(x,labels=labels)
#[Text(1.09783,0.0690696,'娱乐'),
#  Text(1.05632,0.30689,'育儿'),
#  Text(0.753002,0.801865,'饮食'),
#  Text(-1.06544,-0.273559,'房贷'),
#  Text(0.889919,-0.646564,'交通'),
#  Text(1.05632,-0.30689,'其它')])

insert image description here

1. Percentage display percentage

  • autopct: format string.
  • We define the label of the pie, the amount of each label, %.2f%% displays the percentage, and retains 2 decimal places.
labels = ['娱乐','育儿','饮食','房贷','交通','其它']
​x = [200,500,1200,7000,200,900]
​plt.title("饼图示例-8月份家庭支出")
​plt.pie(x,labels=labels,autopct='%.2f%%')
#[Text(1.09783,0.0690696,'娱乐'),
#  Text(1.05632,0.30689,'育儿'),
#  Text(0.753002,0.801865,'饮食'),
#  Text(-1.06544,-0.273559,'房贷'),
#  Text(0.889919,-0.646564,'交通'),
#  Text(1.05632,-0.30689,'其它')],
# [Text(0.598816,0.0376743,'2.00%'),
#  Text(0.576176,0.167395,'5.00%'),
#  Text(0.410728,0.437381,'12.00%'),
#  Text(-0.58115,-0.149214,'70.00%'),
#  Text(0.48541,-0.352671,'2.00%'),
#  Text(0.576176,-0.167395,'9.00%')])

insert image description here

2. Separation of pie charts

  • explode: Specifies the highlighting of certain parts of the pie chart.
  • We define the label of the pie, the amount of each label, %.2f%% displays the percentage, and retains 2 decimal places.
labels = ['娱乐','育儿','饮食','房贷','交通','其它']
x = [200,500,1200,7000,200,900]
​explode = (0.03,0.05,0.06,0.04,0.08,0.21)
​plt.pie(x,labels=labels,autopct='%3.2f%%',explode=explode)

insert image description here

3. Set the pie chart percentage and text distance from the center position

  • pctdistance: Set the distance between the percentage label and the center of the circle.
  • labeldistance: Set the distance between each sector label (legend) and the center of the circle.
labels = ['娱乐','育儿','饮食','房贷','交通','其它']
​x = [200,500,1200,7000,200,900]
​explode = (0.03,0.05,0.06,0.04,0.08,0.1)
​plt.pie(x,labels=labels,autopct='%3.2f%%',explode=explode, labeldistance=1.35, pctdistance=1.2)

insert image description here

4. Legend

labels = ['娱乐','育儿','饮食','房贷','交通','其它']
​x = [200,500,1200,7000,200,900]
​explode = (0.03,0.05,0.06,0.04,0.08,0.1)
​plt.pie(x,labels=labels,autopct='%3.2f%%',explode=explode, labeldistance=1.35, pctdistance=1.2)
plt.legend()

insert image description here

  • You can make the pie chart a perfect circle by setting the scale of x and y to be the same plt.axis('equal').

Guess you like

Origin blog.csdn.net/weixin_45891612/article/details/129082746