zoukankan      html  css  js  c++  java
  • python matplotlib

    安装过程

    下载Anaconda3,安装完成后发现直接在pycharm中不能导入matplotlib和numpy

    解决方法:File->setting->project interpreter 点击右上角的小齿轮,选择add local,寻找Anaconda3中python.exe

    默认位置应该在这儿:

    (有的时候会出现ProgramData 找不到的情况,请参考我下一篇文章。)

    最后点击apply就可以了

    第一课

    绘制散点图

    import matplotlib.pyplot as plt
    import numpy as np
    
    # x = [1,2,3,4,5]
    # x1 = np.array(x)   将列表x化为np对象(暂时不知道有什么用)
    
    # x = np.random.randn(1000)  随机数
    # y = np.random.randn(1000)
    
    power,money,ability = np.loadtxt("01.csv",skiprows=1,usecols=(1,2,4),unpack=True,delimiter=" ")
    #跳过行,行是从1开始数的。使用列,列是从0开始数的。delimiter文件中的数据分割标志
    #从文件中获取数据,unpack默认为False,改为True时能够将数据导入多个变量中
    
    
    plt.scatter(x,y,edgecolors='red',c='g',s=20,marker='+')  #scatter 绘制散点图
    plt.show()

     绘制折线图

    plt.plot(da,open,linestyle='-',color='red',marker='o')
    #默认plot为折线图

    绘制条状图(可以绘制在一起)

    da = [1,2,3,4,5]
    open = [3,4,2,1,5]
    index = np.arange(5)
    plt.bar(index,da,width=0.5)
    plt.bar(index+0.5,open,width=0.5)#index+0.5是平移0.5避免重合

      条状图叠在一起

    da = [1,2,3,4,5]
    open = [3,4,2,1,5]
    index = np.arange(5)
    plt.bar(index,da,width=0.5)
    plt.bar(index,open,width=0.5,bottom=da)#index+0.5是平移0.5避免重合

    绘制直方图

    array1 = np.random.randn(20000)#中心在0的满足正态分布的20000个随机变量
    plt.hist(array1,bins=100,normed=False)#bins横坐标的分组数,normed是否对数据进行标准化(频率or出现次数)
    plt.show()

    绘制2d直方图

    颜色越深代表概率越大

    array2 = np.random.randn(20000)+2
    array3 = np.random.randn(20000)+3
    
    plt.hist2d(array2,array3,bins=40)
    plt.show()

    好漂酿哦!

    饼状图

    label = 'A','B','C','D'
    value = [4,5,7,9]
    
    distance = [0,0.05,0,0]  #突出部分B被选为突出0.05
    
    plt.pie(x=value,labels=label,autopct='%.0f%%',shadow=True,explode=distance)
                                #选择是否显示百分比,选择是否显示阴影,选择突出的部分
    
    plt.show()

    附:常用颜色blue/green/red/cyan/magenta/yellow/black/white

           线段样式:实线  -  虚线  --  点画线  -.  点线  :

           常用23个点的样式:百度

           

          样式字符串:一个很pythonic的写法    颜色 点样式 线段样式

          eg.   cx--  mo:

  • 相关阅读:
    7-22 朋友圈(25 分)
    c++之函数重载
    c++之函数的其它用法
    c++之引用
    c++之内存模型
    c++实例之通讯录管理系统之清空联系人功能(七)
    c++实例之通讯录管理系统之修改联系人功能(六)
    c++实例之通讯录管理系统之查找联系人功能(五)
    c++实例之通讯录管理系统之删除联系人功能(四)
    c++实例之通讯录管理系统之显示联系人功能(三)
  • 原文地址:https://www.cnblogs.com/cunyusup/p/7368160.html
Copyright © 2011-2022 走看看