画饼状图和柱状图


import matplotlib.pyplot as plt

import os
from pathlib import Path
import matplotlib.pyplot as plt

# 设置中文字体为微软雅黑
plt.rcParams['font.sans-serif'] = 'SimHei'
#_____________________________________________________________________________________________________________

import matplotlib.pyplot as plt

# 数据
labels = ['airplane 147', 'Radar 1069', 'Gun 666 ', 'Porthole 508']
sizes = [147, 1069, 666, 508]
colors = ['skyblue', 'orange', 'green','pink']
# 饼状图
fig, ax = plt.subplots()
ax.pie(sizes, labels=labels,colors=colors, autopct='%1.1f%%', startangle=90)

# 设置文本和标签的字体大小
ax.set_title('标签类别分布', fontsize=12)
# ax.axis('equal')
# for label in ax.get_xticklabels() + ax.get_yticklabels():
#     label.set_fontsize(20)
# # 显示图形
plt.show()



#______________________________________________________________________________________________________________
data = {'0': {'S:[0, 32x32]': 113, 'M:[32x32, 96x96]': 15, 'L:[96*96, 640x640]': 19},
        '1': {'S:[0, 32x32]': 1042, 'M:[32x32, 96x96]': 14, 'L:[96*96, 640x640]': 13},
        '2': {'S:[0, 32x32]': 490, 'M:[32x32, 96x96]': 88, 'L:[96*96, 640x640]': 88},
        '3': {'S:[0, 32x32]': 414, 'M:[32x32, 96x96]': 59, 'L:[96*96, 640x640]': 35}}

data1=[113,15,19]
data2=[1042,14,13]
data3=[490,88,88]
data4=[414,59,35]

# Plot 1
fig = plt.figure(figsize=(10, 8))  # 画布大小和像素密度
plt.ylim(0, 2000)
plt.bar(data['0'].keys(), data['0'].values(),width=0.5, align="center", color=['skyblue', 'orange', 'green'])
x = ['S:[0, 32x32]', 'M:[32x32, 96x96]', 'L:[96*96, 640x640]']
for a, b, i in zip(x, data1, range(len(x))):  # zip 函数
        plt.text(a, b + 0.01, "%d" % int(data1[i]), ha='center', fontsize=15, color="r")  # plt.text 函数
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.xlabel('大小', fontsize=16)
plt.ylabel('数量', fontsize=16)
plt.title('airplane小、中、大分布情况(960x540)', fontsize=16)
plt.show()

# Plot 2
fig = plt.figure(figsize=(10, 8))  # 画布大小和像素密度
plt.ylim(0, 2000)
plt.bar(data['1'].keys(), data['1'].values(),width=0.5, align="center", color=['skyblue', 'orange', 'green'])
for a, b, i in zip(x, data2, range(len(x))):  # zip 函数
        plt.text(a, b + 0.01, "%d" % int(data2[i]), ha='center', fontsize=15, color="r")  # plt.text 函数
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.xlabel('大小', fontsize=16)
plt.ylabel('数量', fontsize=16)
plt.title('Radar小、中、大分布情况(960x540)', fontsize=16)
plt.show()

# Plot 3
fig = plt.figure(figsize=(10, 8))  # 画布大小和像素密度
plt.ylim(0, 2000)
plt.bar(data['2'].keys(), data['2'].values(),width=0.5, align="center", color=['skyblue', 'orange', 'green'])
for a, b, i in zip(x, data3, range(len(x))):  # zip 函数
        plt.text(a, b + 0.01, "%d" % int(data3[i]), ha='center', fontsize=15, color="r")  # plt.text 函数
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.xlabel('大小', fontsize=16)
plt.ylabel('数量', fontsize=16)
plt.title('Gun小、中、大分布情况(960x540)', fontsize=16)
plt.show()

# Plot 4
fig = plt.figure(figsize=(10, 8))  # 画布大小和像素密度
plt.ylim(0, 2000)
plt.bar(data['3'].keys(), data['3'].values(),width=0.5, align="center", color=['skyblue', 'orange', 'green'])
for a, b, i in zip(x, data4, range(len(x))):  # zip 函数
        plt.text(a, b + 0.01, "%d" % int(data4[i]), ha='center', fontsize=15, color="r")  # plt.text 函数
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.xlabel('大小', fontsize=16)
plt.ylabel('数量', fontsize=16)
plt.title('Porthole小、中、大分布情况(960x540)', fontsize=16)
plt.show()

猜你喜欢

转载自blog.csdn.net/weixin_46056196/article/details/129991477