[数据可视化] matplotlib条形图

1.柱状图

1.1 基本柱状图
import matplotlib.pyplot as plt

num_list = [2, 4, 6, 8]
plt.bar(range(len(num_list)), num_list)
plt.show()

在这里插入图片描述

1.2 设置颜色
import matplotlib.pyplot as plt

num_list = [2, 4, 6, 8]
plt.bar(range(len(num_list)), num_list, fc='c')
plt.show()

在这里插入图片描述

1.3 设置颜色列表
import matplotlib.pyplot as plt

num_list = [2, 4, 6, 8]
plt.bar(range(len(num_list)), num_list, color=['c','royalblue','coral'])
plt.show()

在这里插入图片描述

1.4 并列柱状图
import matplotlib.pyplot as plt

name_list = ['One', 'Two', 'Three', 'Four']
num_list1 = [2, 4, 6, 8]
num_list2 = [1, 2, 3, 4]
x = list(range(len(num_list1)))
total_width, n = 0.8, 2
width = total_width / n

plt.bar(x, num_list1, width=width, label='south')
for i in range(len(x)):
    x[i] = x[i] + width
plt.bar(x, num_list2, width=width, label='north', tick_label=name_list, fc='coral')
plt.legend()
plt.show()

在这里插入图片描述

1.5 区段颜色划分柱状图
import matplotlib.pyplot as plt

x = [1,2,3,4,5,6,7,8,9,10,11,12]
y = [6,3,9,2,6,16,8,10,4,14,18,6]

# 销量的不同区段标为不同的颜色
def get_color(x, y):
    color = []
    for i in range(len(x)):
        if y[i] < 5:
            color.append("green")
        elif y[i] < 10:
            color.append("c")
        elif y[i] < 15:
            color.append("gold")
        else:
            color.append("coral")
    return color

plt.bar(x, y, label="sale", color=get_color(x, y), tick_label=x)

for a, b in zip(x, y):
    plt.text(a, b+0.1, b, ha='center', va='bottom')  # 数据标签
plt.legend(loc="upper left")  # 图例
plt.xlabel('date')
plt.title('month sale')
plt.show()

在这里插入图片描述

1.6 蝴蝶柱状图
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(10)
y1 = np.random.rand(10)
y2 = -np.random.rand(10)


plt.bar(x, y1, yerr=y1*0.1)  # yerr:误差线
plt.bar(x, y2, fc='coral')

for a, b in zip(x,y1):
    plt.text(a-0.3, b+0.1, '%.2f'%b, color='black')
for a, b in zip(x,y2):
    plt.text(a-0.3, b-0.1, '%.2f'%b, color='black')
plt.show()

在这里插入图片描述

2.堆叠柱状图

import matplotlib.pyplot as plt

name_list = ['One', 'Two', 'Three', 'Four']
num_list1 = [2, 4, 6, 8]
num_list2 = [1, 2, 3, 4]
plt.bar(range(len(num_list)), num_list1, label='south')
plt.bar(range(len(num_list)), num_list2, bottom=num_list1, label='north', tick_label=name_list, fc='coral')
plt.legend()
plt.show()

在这里插入图片描述

3.条形图

3.1 基本条形图
import matplotlib.pyplot as plt

name_list = ['One', 'Two', 'Three', 'Four']
num_list = [2, 4, 6, 8]
plt.barh(range(len(num_list)), num_list, tick_label=name_list)
plt.show()

在这里插入图片描述

3.2 带标签条形图
import matplotlib.pyplot as plt

dic = {'a': 22, 'b': 10, 'c': 6, 'd': 4, 'e': 2, 'f': 10, 'g': 24, 'h': 16, 'i': 1, 'j': 12}

s = sorted(dic.items(), key=lambda x: x[1], reverse=False)
x, y= list(), list()

for i in s:
    x.append(i[0])
    y.append(i[1])

plt.barh(x, y)
for a, b in zip(x, y):
    plt.text(b+1, a, b, ha='center', va='center')  # 设置数据标签
plt.legend(["label"], loc='lower right')  # 设置图例
plt.ylabel('name')  # y轴名称
plt.xlabel('number')  # x轴名称
plt.title('title')  # 标题
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值