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()
 
 

你可能感兴趣的:(机器学习,数据挖掘)