# -*- coding: utf-8 -*-
"""
Created on Tue Oct 29 09:27:37 2019
@author: weiping
"""
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.plot(np.arange(10))
plt.show()
fig,axes = plt.subplots(2,2,sharex = True,sharey = True) # sharex/y 使所有的subplot使用相同的轴刻度
for i in range(2):
for j in range(2):
axes[i,j].plot(np.random.rand(500),color = '#CECECE',alpha = 1)
plt.subplots_adjust(wspace = 0.1,hspace = 0.1) # h/wspace设置高度和宽度的百分比[0-1]
plt.plot(np.random.randn(30).cumsum(),'ko--')
plt.show()
'''
pandas中绘图
'''
df = pd.DataFrame(np.random.randn(10,4).cumsum(0),
columns = ['a','b','c','d'])
df.plot()
df.plot(kind = 'hist',alpha = .3) #kind 可以是 line bar barh kde hist 等
df = pd.DataFrame(np.random.randn(10000),
columns = ['a'])
df.hist(bins = 100)
-----
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
x = range(2000,2010,1)
y = range(1900,1920,2)
plt.plot(y,x,color = 'red',linestyle = '-.',linewidth = 5.0)
plt.show()
x_data = ['2011', '2012', '2013', '2014', '2015', '2016', '2017']
y_data = [58000, 60200, 63000, 71000, 84000, 90500, 107000]
y_data2 = [52000, 54200, 51500,58300, 56800, 59500, 62700]
plt.plot(x_data,y_data,color = 'red',linewidth = 2,linestyle = '-.',label = 'java基础')
plt.plot(x_data,y_data2,color = 'blue',linewidth = 5,linestyle = ':' ,label = 'python基础')
my_font = fm.FontProperties(fname = r'C:\Windows\Fonts\simsun.ttc')
plt.legend(loc = 'best',prop = my_font)
plt.xlabel('year')
plt.ylabel('count')
plt.title('tushu')
plt.yticks([50000,70000,100000],[r'一般',r'优秀',r'火爆'])
plt.show()
plt.plot(x_data,y_data,'dr')
plt.show()
x = [1,3,5,2,4]
y = [2,4,5,1,3]
plt.plot(x, y) #将x,y轴坐标数据传入,第三个参数是展现形式,默认是折线图
plt.plot(x, y, 'ok-.') #黑色散点图
plt.xlim(0,10) #设置x范围在0到10
plt.ylim(0,10) #设置y范围在0到10
plt.title("show") #设置标题名,在中上方
plt.xlabel("num") #设置x轴名称,在中下方
plt.ylabel("age") #设置y轴名称,在中左方
plt.show() #可视化显示数据,注意show了之后画布就被清空了,前面的内容下次就show不出来
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set(xlim=[0.5, 4.5], ylim=[-2, 8], title='An Example Axes',
ylabel='Y-Axis', xlabel='X-Axis')
plt.show()
fig,ax = plt.subplots(nrows = 4 ,ncols = 4 )
ax[0,0].set(title = 'Upper Left')
ax[0,2].set(title = 'Upper Right')
ax[2,0].set(title = 'Lower Left')
ax[2,2].set(title = 'Lower Right')
plt.show()
fig = plt.figure()
ax1 = fig.add_subplot(321)
ax2 = fig.add_subplot(322)
ax3 = fig.add_subplot(423)
ax4 = fig.add_subplot(424)
plt.show()
import numpy as np
fig = plt.figure()
ax1 = fig.add_subplot(321)
ax2 = fig.add_subplot(322)
ax3 = fig.add_subplot(423)
ax4 = fig.add_subplot(424)
x = np.linspace(0,np.pi)
y_s = np.sin(x)
y_t = np.tan(x)
ax1.plot(x,y_t)
ax2.plot(x,y_s,color = 'red',marker = 'd',linestyle = '-.')
plt.show()
'''
散点图/气泡图
'''
x = range(10)
y = np.random.randn(10)
plt.scatter(x,y)
plt.show()
#---------------
x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)
area = (30*np.random.rand(50)) **2
plt.scatter(x,y,s = area,c=colors,alpha = .6)
plt.show()
'''
条形图
'''
np.random.seed(1)
x = np.arange(5)
y = np.random.randn(5)
fig,ax = plt.subplots(ncols =2,figsize = plt.figaspect(1./2))
ver_bar = ax[0].bar(x,y) #水平
hor_bar = ax[1].barh(x,y) #垂直
ax[0].axhline()
ax[1].axvline() #垂直
plt.show()
#不同颜色
fig,ax = plt.subplots()
bars = ax.bar(x,y)
for bar,h in zip(bars,y):
if h <= 0 :
bar.set(color = 'red')
plt.show()
'''
直方图
'''
np.random.seed(1991919191)
n_bins = 10
x = np.random.randn(1000,3)
fig,axes = plt.subplots(nrows = 2,ncols = 2 )
ax1,ax2,ax3,ax4 = axes.flatten()
colors = ['red','blue','black']
ax1.hist(x,n_bins,density = True,histtype = 'bar',color = colors,label = colors)
ax2.hist(x,n_bins,density = True,histtype = 'barstacked')
ax3.hist(x,n_bins,histtype = 'barstacked',rwidth = 0.5)
ax4.hist(x[:,0])
plt.show()
''''
饼图
''''
colors = ['red','blue','black']
sizes = [ 12,14,18]
explode=(0,0.2,0)
fig,(ax1,ax2) = plt.subplots(2)
ax1.pie(sizes,labels = colors)
ax2.pie(sizes,autopct = '%1.2f%%',shadow = True,startangle=90,explode = explode,pctdistance = 1.12)
ax2.axis('equal')
ax2.legend(labels = colors,loc = 'best')
plt.show()
'''
箱型图
'''
np.random.seed(1991919191)
x = np.random.randn(1000,3)
fig,(ax1,ax2) = plt.subplots(2)
ax1.boxplot(x[:,0])
ax2.boxplot(x,vert = False)
plt.show()
利用python进行数据分析matplotlib_api入门
最新推荐文章于 2023-04-21 17:10:56 发布