用python绘制熊猫图案_利用Python进行数据分析_Pandas_绘图和可视化_Matplotlib

1 认识Figure和Subplot

import matplotlib.pyplot as plt

matplotlib的图像都位于Figure对象中

fg = plt.figure()

通过add_subplot创建subplot

ax1 = fg.add_subplot(1,2,1)

ax2= fg.add_subplot(1,2,2)

设置坐标轴的范围

plt.xlim((-1, 1))

plt.ylim((0, 3))

设置坐标轴的lable

matplotlib.pyplot.xlabel(xlabel, fontdict=None, labelpad=None, **kwargs)

plt.xlabel('横轴:时间', fontproperties = 'SimHei', fontsize = 15, color ='green')

plt.ylabel('纵轴:振幅', fontproperties= 'SimHei', fontsize = 15)

plt.figure()的作用

如下例子中,plt.subplot()如果不加,则4个图都在同一个figure对象汇总,而加上plt.subplot(),则每个图分别在一个figure对象中

importmatplotlib.pyplot as pltimportpandas as pdfrom numpy.random importrandn

x= np.linspace(-1, 1, 50)

y1= 2 * x + 1

#plt.figure()

ax1 = plt.subplot(4,2,1)

ax1.plot(x, y1)

y2= x**2

#plt.figure()

ax2 = plt.subplot(4,2,2)

ax2.plot(x, y2)

y3= 2*x+1

#plt.figure()

ax3 = plt.subplot(4,2,3)#plt.subplot(3,2,4) : 分成3行2列,共6个绘图区域,在第4个区域绘图。排序为行优先。也可 plt.subplot(324),将逗号省略。

ax3.plot(x, y3)

y4= x**2

#plt.figure()

ax4 = plt.subplot(4,2,4)

ax4.plot(x, y4)

plt.show()

plt的常用函数

plt.scatter#(数据,点的大小) 绘制单个点

plt.show()# 显示出来

plt.title #(名字,大小)

plt.xlable()

plt.ylable()

plt.tick_params()#设置刻度的大小

plt.axis([0,1100,0,111000]) # 设置坐标轴的取值范围

plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues,

edgecolor='none', s=40) # 把颜色设置为一个动态的变量,并使用cmp高数pylot使用哪个颜色的映射

plt.savefig(“squares_plot.png”,bbox_inches= "tight")# 第一个参数指定要以什么样的文件名保存图表,第二个参数指定将图标多余的空白区域裁掉

plt.axes().get_xaxis().set_visible(false)# 使x轴不可见

plt.figure(figsize= (10,6)) # 设置绘图窗口的尺寸

plt的图表函数

plt.plot(x,y , fmt) :绘制坐标图

plt.boxplot(data, notch, position): 绘制箱形图

plt.bar(left, height, width, bottom) : 绘制条形图

plt.barh(width, bottom, left, height) : 绘制横向条形图

plt.polar(theta, r) : 绘制极坐标图

plt.pie(data, explode) : 绘制饼图

plt.scatter(x, y) :绘制散点图

plt.hist(x, bings, normed) : 绘制直方图

fig,axes = plt.subplots()

importnumpy as npimportmatplotlib.pyplot as plt

fig,axes= plt.subplots(2,3)

plt.subplots_adjust(wspace=0,hspace=0)#wspace控制宽度百分比,hspace控制高度的百分比,用作subplot之间的距离

axes[1,2]

797069-20190111135516325-1642301283.png

importnumpy as npimportmatplotlib.pyplot as plt

x= np.linspace(0, 2*np.pi, 400)

y= np.sin(x**2)

fig,axes= plt.subplots(2,3)

plt.subplots_adjust(wspace=0,hspace=0)#wspace控制宽度百分比,hspace控制高度的百分比,用作subplot之间的距离

axes[1,2].plot(x,y)

plt.show()

797069-20190111141413180-168060266.png

颜色、标记、线型

axes.plot(x,y,'g--')

Colors

797069-20190111144457529-2056228878.png

Markers

797069-20190111144511622-1533722897.png

Line Styles

797069-20190111144521414-1420005122.png

刻度、标签、图例

plt.xlim() #返回当前的X轴的范围

plt.xlim(0,10)#设置当前的X轴的范围

修改X轴刻度

importnumpy as npimportmatplotlib.pyplot as plt

plt.figure()

ax=plt.subplot()

ax.plot(randn(1000).cumsum())#ax.set_xticks([0,250,500,750,1000])

ax.set_xticklabels(['1w','2w','3w','4w','5w','6w','7w'],rotation=30,fontsize='small')

ax.set_title('Test plot!')

plt.show()

797069-20190111155856044-641847658.png

importnumpy as npimportmatplotlib.pyplot as plt

plt.figure()#创建figure对象

ax=plt.subplot()#一张图中 传入多个元素,需要传入label参数

ax.plot(randn(1000).cumsum(),'g--',label='180210.IB',)

ax.plot(randn(1000).cumsum(),'c.',label='170201.IB',)

ax.plot(randn(1000).cumsum(),'m:',label='180201.IB',)#ax.legend(loc='best') # 添加图例(左上角那玩意)

plt.legend(loc='best') #添加图例(左上角那玩意)

ax.set_title('Test plot!') #设置图标的标题

plt.savefig('D:\Test plot.svg')

plt.savefig('D:\Test plot.pdf')

plt.savefig('D:\Test plot.jpg')

plt.savefig('D:\Test plot.png',dpi=400,bbox_inches = 'tight') #保存png格式,dpi 分辨率,bbox_inches 最小白边

plt.show()

797069-20190111165834004-993535508.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值