Python图表绘制Matplotlib

引入

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 导入相关模块

使用

 

   # 图表窗口1 → plt.show()
       plt.plot(np.random.rand(10))
       plt.show()
      # 直接生成图表
 特点:使用后无需写 show()   # 图表窗口2 → 魔法函数,嵌入图表

    %matplotlib inline  
    x = np.random.randn(1000)
    y = np.random.randn(1000)
    plt.scatter(x,y)
    # 直接嵌入图表,不用plt.show()
   # <matplotlib.collections.PathCollection at ...> 代表该图表对象
    # 图表窗口3 → 魔法函数,弹出可交互的matplotlib窗口

   %matplotlib notebook
   s = pd.Series(np.random.randn(100))
   s.plot(style = 'k--o',figsize=(10,5))
   # 可交互的matplotlib窗口,不用plt.show()
   # 可做一定调整
     # 图表窗口4 → 魔法函数,弹出matplotlib控制台

   %matplotlib qt5
   df = pd.DataFrame(np.random.rand(50,2),columns=['A','B'])
   df.hist(figsize=(12,5),color='g',alpha=0.8)
   # 可交互性控制台
   # 如果已经设置了显示方式(比如notebook),需要重启然后再运行魔法函数
   # 网页嵌入的交互性窗口 和 控制台,只能显示一个

   #plt.close()    
   # 关闭窗口

   #plt.gcf().clear()  
   # 每次清空图表内内容
图标的基础元素 图名、图裂、轴标签、轴边界、轴刻度、轴刻度标签
 

plt.title('全国人大代表季度考核')   #标题
plt.xlabel('月份')   #x轴标签
plt.ylabel('积分')   #y轴标签

list.plot(figsize=(6,4))    图标的大小 

plt.legend(loc='best')    #图裂的位置

best  自适应
	upper right  
	upper left
	lower left
	lower right
	right
	center left
	center right
	lower center
	upper center
	center
plt.xlim(0,10)  #x轴坐标边界  数值多大
plt.ylim(0,2)
# 自定义 xy轴刻度 数组格式
plt.xticks(range(0,5))
plt.yticks([0,0.2,0.4,0.6,0.8,1.0])
完整演示

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
% matplotlib inline
# 导入相关模块

import warnings
warnings.filterwarnings('ignore')
# 不发出警告

ts.plot(kind='line',
       label = 'hehe',
       style = '--g.',
       color = 'red',
       alpha = 0.4,
       use_index = True,
       rot = 45,
       grid = True,
       ylim = [-50,50],
       yticks = list(range(-50,50,10)),
       figsize = (8,4),
       title = 'test',
       legend = True)

创建 子图

fig,axes = plt.subplots(4,1,figsize = (10,10))

s.plot(kind='bar',color = 'k',grid = True,alpha = 0.5,ax = axes[0])  # ax参数 → 选择第几个子图
# 单系列柱状图方法一:plt.plot(kind='bar/barh')

df.plot(kind='bar',ax = axes[1],grid = True,colormap='Reds_r')
# 多系列柱状图
df.plot(kind='bar',ax = axes[2],grid = True,colormap='Blues_r',stacked=True)
# 多系列堆叠图
# stacked → 堆叠
df.plot.barh(ax = axes[3],grid = True,stacked=True,colormap = 'BuGn_r')
# 新版本plt.plot.<kind>

plt.figure(figsize=(10,4))

 
#plt.grid(True, linestyle = "--",color = "gray", linewidth = "0.5",axis = 'x')  # 网格
plt.legend()
# Series.plot():series的index为横坐标,value为纵坐标
# 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()

几种类型:

        记住几个点就行 

           

在matplotlib中,整个图像为一个Figure对象

在Figure对象中可以包含一个或者多个Axes对象

每个Axes(ax)对象都是一个拥有自己坐标系统的绘图区域

plt.figure, plt.subplot

#创建一个容器 盒子

fig = plt.figure(figsize=(10,6),facecolor = 'gray')

#容器添加子图  第几行第几个 

ax1 = fig.add_subplot(2,2,1)  # 第一行的左图

原文地址:https://www.cnblogs.com/reeber/p/11407427.html