matplotlib保姆级教学,代码直接可运行,更有很多细节等你

 一块画布上有多个图,且包括了最常见的多种柱状图

三种垂直柱状图(条形图)

matplotlib保姆级教学,代码直接可运行,更有很多细节等你_第1张图片

from matplotlib import pyplot as plt
#import matplotlib.pyplot as plt
import numpy as np

# 设置基本属性.....
# ........... 省略..........

# 国家
countries = ['挪威', '德国', '中国', '美国', '瑞典']

# 金牌个数
gold_medal = [16, 12, 9, 8, 8]
# 银牌个数
silver_medal = [8, 10, 4, 10, 5]
# 铜牌个数
bronze_medal = [13, 5, 2, 7, 5]

# 1.将x轴转换为数值
x_int = np.arange(len(countries))

# 2.设置图形的宽度
width = 0.2

# 确定x起始位置
gold_x = x_int  # 金牌起始位置

silver_x = x_int+width  # 银牌的起始位置

bronze_x = x_int + 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="铜牌") # 铜牌图形

# 将x轴的坐标变回来

# plt.xticks(x_int,labels=countries)
# 移动x标记的位置,再替换内容
plt.xticks(x_int + width,labels=countries)

#-----------显示高度文本----------------
# # 金牌
# for x,y in zip(gold_x,gold_medal):
#     plt.text(x,y,y,va="bottom",ha="center",fontsize=8)
    
# # 银牌牌
# for x,y in zip(silver_x, silver_medal):
#     plt.text(x,y,y,va="bottom",ha="center",fontsize=8)

# # 铜牌
# for x,y in zip(bronze_x, bronze_medal):
#     plt.text(x,y,y,va="bottom",ha="center",fontsize=8)

  # 金牌 # 银牌 # 铜牌
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()

matplotlib保姆级教学,代码直接可运行,更有很多细节等你_第2张图片

from matplotlib import pyplot as plt
#import matplotlib.pyplot as plt
import numpy as np

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)

# 设置坐标轴
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")

例题:

matplotlib保姆级教学,代码直接可运行,更有很多细节等你_第3张图片

from matplotlib import pyplot as plt
import numpy as np

movie = ['千与千寻', '玩具总动员4', '黑衣人:全球追缉']

real_day1 = [4053, 7548, 6543]

real_day2 = [1840, 4013, 3421]

real_day3 = [2080, 1673, 2342]

x_int = np.arange(len(movie))

width = 0.2

day1_x = x_int
day2_x = x_int + width
day3_x = x_int + width * 2

fig = plt.figure(figsize=(6,4), dpi = 150)
ax = fig.add_axes([0,0,1,1])
ax.set_title('电影票房')

plt.bar(day1_x, real_day1, width=width,color="gold", label="day1") 

plt.bar(day2_x, real_day2, width=width, color="silver",label="day2") 

plt.bar(day3_x, real_day3, width=width, color="saddlebrown",label="day3") 

plt.xticks(x_int + width,labels=movie)
for i in range(len(movie)):
    ax.text(day1_x[i],real_day1[i], real_day1[i],va="bottom",ha="center",fontsize=8)
    ax.text(day2_x[i],real_day2[i], real_day2[i],va="bottom",ha="center",fontsize=8)
    ax.text(day3_x[i],real_day3[i], real_day3[i],va="bottom",ha="center",fontsize=8)


# 显示图例
ax.legend(fontsize=6 )

    

 为了方便编写,可以在每次项目的顶部加上基础设置相关的代码:

from matplotlib import pyplot as plt
import numpy as np
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei']
# 中文负号
plt.rcParams['axes.unicode_minus'] = False

# 设置分别率 为100
plt.rcParams['figure.dpi'] = 100

# 设置大小
plt.rcParams['figure.figsize'] = (5,3)

#参数选择是任意的,此处只是举个栗子而已

你可能感兴趣的:(Python学习,matplotlib,python,开发语言)