###序言: Python的可视化工具,如下截图,均以展现图表实例,如需了解部分对象的输出结果,可参照我Github上的代码,3Q🌹python
####【课程3.1】 Matplotlib简介及图表窗口 Matplotlib → 一个python版的matlab绘图接口,以2D为主,支持python、numpy、pandas基本数据结构,运营高效且有较丰富的图表库git
图表窗口1 → plt.show()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 图表窗口1 → plt.show()
plt.plot(np.random.rand(10))
plt.show()
# 直接生成图表
复制代码
图表窗口2 → 魔法函数,嵌入图表
# 图表窗口2 → 魔法函数,嵌入图表
% matplotlib inline
x = np.random.randn(1000)
y = np.random.randn(1000)
plt.scatter(x,y)
# 直接嵌入图表,不用plt.show()
# <matplotlib.collections.PathCollection at ...> 表明该图表对象
复制代码
% matplotlib notebook s = pd.Series(np.random.randn(100)) s.plot(style = 'k--o',figsize=(10,5))github

<br>
>##### 图表窗口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)spring
#plt.close()数组
#plt.gcf().clear()bash
PS:这里须要用代码调试
<br>
####【课程3.2】 图表的基本元素
    图表内基本参数设置
>##### 图名,图例,轴标签,轴边界,轴刻度,轴刻度标签等
复制代码
df = pd.DataFrame(np.random.rand(10,2),columns=['A','B']) fig = df.plot(figsize=(6,4))数据结构
plt.title('Interesting Graph - Check it out') # 图名 plt.xlabel('Plot Number') # x轴标签 plt.ylabel('Important var') # y轴标签dom
plt.legend(loc = 'upper right')svg
plt.xlim([0,12]) # x轴边界 plt.ylim([0,1.5]) # y轴边界 plt.xticks(range(10)) # 设置x刻度 plt.yticks([0,0.2,0.4,0.6,0.8,1.0,1.2]) # 设置y刻度 fig.set_xticklabels("%.1f" %i for i in range(10)) # x轴刻度标签 fig.set_yticklabels("%.2f" %i for i in [0,0.2,0.4,0.6,0.8,1.0,1.2]) # y轴刻度标签函数
print(fig,type(fig))

<br>
>##### 其余元素可视性
复制代码
x = np.linspace(-np.pi,np.pi,256,endpoint = True) c, s = np.cos(x), np.sin(x) plt.plot(x, c) plt.plot(x, s)
plt.grid(True, linestyle = "--",color = "gray", linewidth = "0.5",axis = 'x')
plt.tick_params(bottom='on',top='off',left='on',right='off')
import matplotlib matplotlib.rcParams['xtick.direction'] = 'out' matplotlib.rcParams['ytick.direction'] = 'inout'
frame = plt.gca() #plt.axis('off')
#frame.axes.get_xaxis().set_visible(False) #frame.axes.get_yaxis().set_visible(False)

<br>
####【课程3.3】 图表的样式参数
    linestyle、style、color、marker
>##### linestyle参数
复制代码
plt.plot([i**2 for i in range(100)], linestyle = '-.')

<br>
>##### marker参数
复制代码
s = pd.Series(np.random.randn(100).cumsum()) s.plot(linestyle = '--', marker = '.')

<br>
>##### color参数
复制代码
plt.hist(np.random.randn(100), color = 'g',alpha = 0.8)
df = pd.DataFrame(np.random.randn(1000, 4),columns=list('ABCD')) df = df.cumsum() df.plot(style = '--.',alpha = 0.8,colormap = 'GnBu')

<br>
>##### style参数,能够包含linestyle,marker,color
复制代码
ts = pd.Series(np.random.randn(1000).cumsum(), index=pd.date_range('1/1/2000', periods=1000)) ts.plot(style = '--g.',grid = True)

<br>
>##### 总体风格样式
复制代码
import matplotlib.style as psl print(plt.style.available)
psl.use('ggplot') ts = pd.Series(np.random.randn(1000).cumsum(), index=pd.date_range('1/1/2000', periods=1000)) ts.plot(style = '--g.',grid = True,figsize=(10,6))

<br>
####【课程3.4】 刻度、注解、图表输出
    主刻度、次刻度
>##### 刻度
复制代码
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
t = np.arange(0.0, 100.0, 1) s = np.sin(0.1np.pit)np.exp(-t0.01) ax = plt.subplot(111) #注意:通常都在ax中设置,再也不plot中设置 plt.plot(t,s,'--*') plt.grid(True, linestyle = "--",color = "gray", linewidth = "0.5",axis = 'both')
#plt.legend() # 图例
xmajorLocator = MultipleLocator(10) # 将x主刻度标签设置为10的倍数 xmajorFormatter = FormatStrFormatter('%.0f') # 设置x轴标签文本的格式 xminorLocator = MultipleLocator(5) # 将x轴次刻度标签设置为5的倍数
ymajorLocator = MultipleLocator(0.5) # 将y轴主刻度标签设置为0.5的倍数 ymajorFormatter = FormatStrFormatter('%.1f') # 设置y轴标签文本的格式 yminorLocator = MultipleLocator(0.1) # 将此y轴次刻度标签设置为0.1的倍数
ax.xaxis.set_major_locator(xmajorLocator) # 设置x轴主刻度 ax.xaxis.set_major_formatter(xmajorFormatter) # 设置x轴标签文本格式 ax.xaxis.set_minor_locator(xminorLocator) # 设置x轴次刻度
ax.yaxis.set_major_locator(ymajorLocator) # 设置y轴主刻度 ax.yaxis.set_major_formatter(ymajorFormatter) # 设置y轴标签文本格式 ax.yaxis.set_minor_locator(yminorLocator) # 设置y轴次刻度
ax.xaxis.grid(True, which='both') #x坐标轴的网格使用主刻度 ax.yaxis.grid(True, which='minor') #y坐标轴的网格使用次刻度
#删除坐标轴的刻度显示 #ax.yaxis.set_major_locator(plt.NullLocator()) #ax.xaxis.set_major_formatter(plt.NullFormatter())

<br>
>##### 注解
复制代码
df = pd.DataFrame(np.random.randn(10,2)) df.plot(style = '--o') plt.text(5,0.5,'hahaha',fontsize=10)

<br>
>#####图表输出
复制代码
df = pd.DataFrame(np.random.randn(1000, 4), columns=list('ABCD')) df = df.cumsum() df.plot(style = '--.',alpha = 0.5) plt.legend(loc = 'upper left') plt.savefig('C:/Users/Hjx/Desktop/pic.png', dpi=400, bbox_inches = 'tight', facecolor = 'g', edgecolor = 'b')

<br>
####【课程3.5】 子图
    在matplotlib中,整个图像为一个Figure对象
在Figure对象中能够包含一个或者多个Axes对象
每一个Axes(ax)对象都是一个拥有本身坐标系统的绘图区域
    plt.figure, plt.subplot
>##### plt.figure() 绘图对象
复制代码
fig1 = plt.figure(num=1,figsize=(4,2)) plt.plot(np.random.rand(50).cumsum(),'k--') fig2 = plt.figure(num=2,figsize=(4,2)) plt.plot(50-np.random.rand(50).cumsum(),'k--')

<br>
>##### 子图建立1 - 先创建子图而后填充图表
复制代码
fig = plt.figure(figsize=(10,6),facecolor = 'gray')
ax1 = fig.add_subplot(2,2,1) # 第一行的左图 plt.plot(np.random.rand(50).cumsum(),'k--') plt.plot(np.random.randn(50).cumsum(),'b--')
ax2 = fig.add_subplot(2,2,2) # 第一行的右图 ax2.hist(np.random.rand(50),alpha=0.5)
ax4 = fig.add_subplot(2,2,4) # 第二行的右图 df2 = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd']) ax4.plot(df2,alpha=0.5,linestyle='--',marker='.')

<br>
>##### 子图建立2 - 建立一个新的figure,并返回一个subplot对象的numpy数组 → plt.subplot
复制代码
fig,axes = plt.subplots(2,3,figsize=(10,4)) ts = pd.Series(np.random.randn(1000).cumsum()) print(axes, axes.shape, type(axes))
ax1 = axes[0,1] ax1.plot(ts)

<br>
>#####plt.subplots,参数调整
复制代码
fig,axes = plt.subplots(2,2,sharex=True,sharey=True)
for i in range(2): for j in range(2): axes[i,j].hist(np.random.randn(500),color='k',alpha=0.5) plt.subplots_adjust(wspace=0,hspace=0)

<br>
>##### 子图建立3 - 多系列图,分别绘制
复制代码
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD')) df = df.cumsum() df.plot(style = '--.',alpha = 0.4,grid = True,figsize = (8,8), subplots = True, layout = (2,3), sharex = False) plt.subplots_adjust(wspace=0,hspace=0.2)

<br>
#####最后:
[以上完整代码](https://github.com/ChaoRenYuan/Python/tree/master/Python数据分析工具/图表绘制工具:Matplotlib)
复制代码