import matplotlib.pyplot as plt
#准备绘图数据
import numpy as np
x=np.arange(0,1,0.05)
print(x)
[0. 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65
0.7 0.75 0.8 0.85 0.9 0.95]
#y=sin(2*pi*x)
y=np.sin(2*np.pi*x)
print(y)
[ 0.00000000e+00 3.09016994e-01 5.87785252e-01 8.09016994e-01
9.51056516e-01 1.00000000e+00 9.51056516e-01 8.09016994e-01
5.87785252e-01 3.09016994e-01 1.22464680e-16 -3.09016994e-01
-5.87785252e-01 -8.09016994e-01 -9.51056516e-01 -1.00000000e+00
-9.51056516e-01 -8.09016994e-01 -5.87785252e-01 -3.09016994e-01]
#plt.plot
plt.plot(x,y)
plt.show()
plt.plot(x,y,'b-*',label='sin')#
plt.title('My first plot')#加标题
plt.xlabel('x label')#x轴标签
plt.ylabel('y label')#y轴标签
plt.legend(loc='best')
plt.show()
#figure和subplot
#绘制多个图标
fig=plt.figure()
ax1=fig.add_subplot(221)#创建两行两列的子图,1表示第一个子图
ax2=fig.add_subplot(223)#
plt.show()
#figure和subplot
#绘制多个图标
fig=plt.figure()
ax1=fig.add_subplot(221)#创建两行两列的子图,1表示第一个子图
ax2=fig.add_subplot(222)
ax3=fig.add_subplot(223)#
ax2.plot(x,y)
plt.show()
#颜色,线形和标记
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(x,y,'r--*',label='sin')
#也可写成,ax.plot(x,y,color='r',linestyle='--',marker='*')
ax.legend(loc='best')
plt.show()
fig,ax=plt.subplots(2,2)#创建4个子图
ax[1,1].plot(x,y)
plt.show
<function matplotlib.pyplot.show(*args, **kw)>
#标题标签,图
fig,ax=plt.subplots()
ax.plot(x,y,'g--o',label='sin')
ax.set(title='my first plot',xlabel='x',ylabel='y')
ax.legend(loc='best')
ax.grid()
plt.show()
#y2=cos(2*pi*x)
y2=np.cos(2*np.pi*x)
print(y2)
[ 1.00000000e+00 9.51056516e-01 8.09016994e-01 5.87785252e-01
3.09016994e-01 6.12323400e-17 -3.09016994e-01 -5.87785252e-01
-8.09016994e-01 -9.51056516e-01 -1.00000000e+00 -9.51056516e-01
-8.09016994e-01 -5.87785252e-01 -3.09016994e-01 -1.83697020e-16
3.09016994e-01 5.87785252e-01 8.09016994e-01 9.51056516e-01]
fig,ax=plt.subplots()
ax.plot(x,y,'b--*',label='sin')
ax.plot(x,y2,'r--*',label='cos')
plt.legend(loc='best')#upper right为右上角,upper left左上角
ax.set(title='sin&cos',xlabel='x',ylabel='y')
plt.show()
#将图标保存到本地
fig.savefig('myfig.png')
常用图标绘图
import pandas as pd
df=pd.read_excel('I:/python_file/jupyter-notebook/data/data2.xlsx',index_col='年份')
df.head()#显示前五列
国内生产总值(亿元) 第一产业增加值(亿元) 第二产业增加值(亿元) 第三产业增加值(亿元)
年份
2000 100280.1 14717.4 45664.8 39897.9
2001 110863.1 15502.5 49660.7 45700.0
2002 121717.4 16190.2 54105.5 51421.7
2003 137422.0 16970.2 62697.4 57754.4
2004 161840.2 20904.3 74286.9 66648.9
len(df)
19
y=df['国内生产总值(亿元)'].head
y
<bound method NDFrame.head of 年份
2000 100280.1
2001 110863.1
2002 121717.4
2003 137422.0
2004 161840.2
2005 187318.9
2006 219438.5
2007 270232.3
2008 319515.5
2009 349081.4
2010 413030.3
2011 489300.6
2012 540367.4
2013 595244.4
2014 643974.0
2015 689052.1
2016 744127.2
2017 820754.3
2018 900309.5
Name: 国内生产总值(亿元), dtype: float64>
y=df['国内生产总值(亿元)'].values
y
array([100280.1, 110863.1, 121717.4, 137422. , 161840.2, 187318.9,
219438.5, 270232.3, 319515.5, 349081.4, 413030.3, 489300.6,
540367.4, 595244.4, 643974. , 689052.1, 744127.2, 820754.3,
900309.5])
x=df.index.values
x
array([2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018], dtype=int64)
#制定默认字体
from pylab import mpl
mpl.rcParams['font.sans-serif']=['Fangsong']
#折线图
fig,ax=plt.subplots()
ax.plot(x,y,'r-*',label='GDP')
ax.set(title='GDP增长',xlabel='年份',ylabel='gdp')
ax.legend(loc='best')
plt.show()
#柱形图
fig,ax=plt.subplots()
ax.bar(x,y,0.5,color='skyblue')
ax.set(title='GDP增长',xlabel='年份',ylabel='gdp')
plt.show()
#水平柱形图-条形图
fig,ax=plt.subplots()
ax.barh(x,y,0.5,color='skyblue')
ax.set(title='GDP增长',xlabel='gdp',ylabel='年份')
plt.show()
#饼图
fig,ax=plt.subplots()
ax.pie(y,labels=x)
plt.show()
#饼图
fig,ax=plt.subplots()
ax.pie(y[:5],labels=x[:5],explode=[0,0.05,0.1,0.15,0.2])#explode设置间距
plt.show()
#散点图
fig,ax=plt.subplots()
ax.scatter(x,y)
ax.set(title='GDP增长',xlabel='gdp',ylabel='年份')#
plt.show()
#pandas中的绘图函数
df.head()
国内生产总值(亿元) 第一产业增加值(亿元) 第二产业增加值(亿元) 第三产业增加值(亿元)
年份
2000 100280.1 14717.4 45664.8 39897.9
2001 110863.1 15502.5 49660.7 45700.0
2002 121717.4 16190.2 54105.5 51421.7
2003 137422.0 16970.2 62697.4 57754.4
2004 161840.2 20904.3 74286.9 66648.9
y=df['国内生产总值(亿元)'].values
x=df.index.values
print(y)
print(x)
[100280.1 110863.1 121717.4 137422. 161840.2 187318.9 219438.5 270232.3
319515.5 349081.4 413030.3 489300.6 540367.4 595244.4 643974. 689052.1
744127.2 820754.3 900309.5]
[2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
2014 2015 2016 2017 2018]
fig,ax=plt.subplots()
ax.plot(x,y,'r')
ax.set(title='GDP增长',xlabel='gdp',ylabel='年份')
plt.show()
#pandas绘制折线图
df['国内生产总值(亿元)'].plot(color='r')
<matplotlib.axes._subplots.AxesSubplot at 0x22fb0274278>
#pandas绘制柱形图
df['国内生产总值(亿元)'].plot(kind='bar',color='r')
<matplotlib.axes._subplots.AxesSubplot at 0x22fb01c7f60>
#pandas绘制水平柱形图
df['国内生产总值(亿元)'].plot(kind='barh',color='r')
<matplotlib.axes._subplots.AxesSubplot at 0x22faeebdeb8>
#pandas绘制饼图
df['国内生产总值(亿元)'].plot(kind='pie')
<matplotlib.axes._subplots.AxesSubplot at 0x22faeeb65f8>
#pandas绘制饼图
df['国内生产总值(亿元)'].plot(kind='area')
<matplotlib.axes._subplots.AxesSubplot at 0x22fb0319080>
df.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x22fb0216f28>
df.plot(kind='bar')
<matplotlib.axes._subplots.AxesSubplot at 0x22faf1302b0>
#绘制堆积柱形图
df.plot(kind='bar',stacked=True)
<matplotlib.axes._subplots.AxesSubplot at 0x22fb048eef0>
#直方图
from pandas import Series
hist_data=Series([2,3,4,5,2,3,3,4,6,5])
hist_data.plot(kind='hist')
<matplotlib.axes._subplots.AxesSubplot at 0x22faf03ec88>
from pandas import Series
hist_data=Series([2,3,4,5,2,3,3,4,6,5])
mybines=[1,3,5,7]
hist_data.plot(kind='hist',bins=mybines)
<matplotlib.axes._subplots.AxesSubplot at 0x22fb06f6c50>