python数据可视化之matplotlib

用matplotlib进行数据可视化探索

一.柱状图

import matplotlib.pyplot as plt
import matplotlib
import pandas as pd
import numpy as np
def bar_plot():
    """
    bar plot
    """
    # 生成测试数据
    means_men = (20, 35, 30, 35, 27)
    means_women = (25, 32, 34, 20, 25)

    # 设置标题
    plt.title("title")

    # 设置相关参数
    index = np.arange(len(means_men)) #横坐标
    bar_width = 0.35 #宽度

    # 画柱状图
    plt.bar(index, means_men, width=bar_width, alpha=0.2, color="b", label="boy")  #alpha是透明度
    plt.bar(index+bar_width, means_women, width=bar_width, alpha=0.8, color="r", label="girl")  #加上bar_width是为了并列放置,否则会堆在一起
    plt.legend(loc="upper center", shadow=True) #设置图例位置

    # 设置柱状图标示,上面的数字
    for x, y in zip(index, means_men):
        plt.text(x, y+0.5, y, ha="center", va="bottom")  #ha是水平对齐,va是垂直对齐
    for x, y in zip(index, means_women):
        plt.text(x+bar_width, y+0.5, y, ha="center", va="bottom")

    # 设置刻度范围/坐标轴名称等
    plt.ylim(0, 50)
    plt.xlabel("Group")
    plt.ylabel("Scores")
    plt.xticks(index+(bar_width/2), ("A", "B", "C", "D", "E"))

    # 图形显示
    plt.show()
    return
bar_plot()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值