【python数据分析】matlabplot基本图标绘制

    xiaoxiao2022-07-12  148

    # kind → line,bar,barh...(折线图,柱状图,柱状图-横...) # label → 图例标签,Dataframe格式以列名为label # style → 风格字符串,这里包括了linestyle(-),marker(.),color(g) # color → 颜色,有color指定时候,以color颜色为准 # alpha → 透明度,0-1 # use_index → 将索引用为刻度标签,默认为True # rot → 旋转刻度标签,0-360 # grid → 显示网格,一般直接用plt.grid # xlim,ylim → x,y轴界限 # xticks,yticks → x,y轴刻度值 # figsize → 图像大小 # title → 图名 # legend → 是否显示图例,一般直接用plt.legend() # 也可以 → plt.plot()

    1、由Series生成图标,Series.plot():series的index为横坐标,value为纵坐标

    import numpy as np import pandas as pd import matplotlib.pyplot as plt % matplotlib inline ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000)) ts = ts.cumsum() ts.plot(kind='line', label = 'cc', style = '--g.', color = 'pink', alpha = 0.8, use_index = True, rot = 45, grid = True, ylim = [-50,50], yticks = list(range(-50,50,10)), figsize = (8,4), title = 'test', legend = True) plt.legend()

     

    2、由Dataframe绘制图标

    df = pd.DataFrame(np.random.randn(1000, 3), index=pd.date_range('20190101',periods=1000), columns=list('ABC')) df = df.cumsum() df.plot(kind='line', style = '--', alpha = 0.9, use_index = True, rot = 45, grid = True, figsize = (8,4), title = 'test', legend = True, subplots = False, colormap = 'pink')

    3、柱状图

    plt.figure(figsize=(10,4)) x = np.arange(10) y1 = np.random.rand(10) y2 = -np.random.rand(10) plt.bar(x,y1,width = 1,facecolor = 'yellowgreen',edgecolor = 'white',yerr = y1*0.1) plt.bar(x,y2,width = 1,facecolor = 'lightskyblue',edgecolor = 'white',yerr = y2*0.1) # x,y参数:x,y值 # width:宽度比例 # facecolor柱状图里填充的颜色、edgecolor是边框的颜色 # left-每个柱x轴左边界,bottom-每个柱y轴下边界 → bottom扩展即可化为甘特图 Gantt Chart # align:决定整个bar图分布,默认left表示默认从左边界开始绘制,center会将图绘制在中间位置 # xerr/yerr :x/y方向error bar for i,j in zip(x,y1): plt.text(i+0.3,j-0.15,'%.2f' % j, color = 'white') for i,j in zip(x,y2): plt.text(i+0.3,j+0.05,'%.2f' % -j, color = 'white') # 给图添加text # zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。

    4、饼图

    # explode:指定每部分的偏移量 # labels:标签 # colors:颜色 # autopct:饼图上的数据标签显示方式 # pctdistance:每个饼切片的中心和通过autopct生成的文本开始之间的比例 # labeldistance:被画饼标记的直径,默认值:1.1 # shadow:阴影 # startangle:开始角度 # radius:半径 # frame:图框 # counterclock:指定指针方向,顺时针或者逆时针

    s = pd.Series(3 * np.random.rand(4), index=['a', 'b', 'c', 'd'], name='series') plt.axis('equal') # 保证长宽相等 plt.pie(s, explode = [0.1,0,0,0], labels = s.index, colors=['r', 'g', 'b', 'c'], autopct='%.2f%%', pctdistance=0.6, labeldistance = 1.2, shadow = True, startangle=0, radius=1.5, frame=False) print(s)

    最新回复(0)