python的turtle画小绵羊_Python作图

matplotlib 是Python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图。而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中。

matplotlib官方查询网址

Matplotlib 里的常用类的包含关系为Figure -> Axes -> (Line2D, Text, etc.)一个Figure对象可以包含多个子图(Axes),在matplotlib中用Axes对象表示一个绘图区域,可以理解为子图。

可以使用subplot()快速绘制包含多个子图的图表,它的调用形式如下:

subplot(numRows, numCols, plotNum)

subplot将整个绘图区域等分为numRows行* numCols列个子区域,然后按照从左到右,从上到下的顺序对每个子区域进行编号。

面向对象的Plot代码如下:

import pandas as pd

import matplotlib.pyplot as plt

s = pd.read_csv("GMV.csv")

Fig = plt.figure(figsize=(8,4)) #创建一个figure

ax1=Fig.add_subplot(221) #画第一个图

ax1.plot_date(s.op_time,s.gmv,'c-')

ax1.set_label("GMV")

ax2=ax1.twinx() #共享x轴,创建次Y坐标轴

ax2.plot_date(s.op_time,s.USERS,'g-') #专门绘制时间函数的

ax2.set_label("Users") #Y轴label

ax2.set_title("Apr-MayGMV/USERs' Trend")#Y轴Title

ax2.legend(loc=0)

ax3=Fig.add_subplot(222)#画第二个图

ax3.plot(s.USERS,"r--")

ax4=Fig.add_subplot(223)#画第三个图

ax4.plot(s.gmv,"g^")

ax5=Fig.add_subplot(224)#画第四个图

ax5.plot(s.gmv,"bs")

Fig.show()

Fig.savefig("test.pdf")

至于关于对象编程的详细资料,matplot面向对象的编程

下面是常用图的类型,以及用matplot快速作图

1.折线图&散点图

1.1基本公式

(1)折线图

plt.plot(x,y,"g-")

(2)散点图

plt.plt(x,y,"ro")

1.2 美化折线图

首先下图剖析了一张数据图都有哪些组成部分。

1.2.1线条颜色

character

color

b

blue

‘g’

green

‘r’

red

‘c’

cyan

‘m’

magenta

‘y’

yellow

‘k’

black

‘w’

white

1.2.2 线条样式&marker样式

'-'表示实线,'--'表示虚线

character

description

'-'

solid line style

'--'

dashed line style

'-.'

dash-dot line style

':'

dotted line style

'.'

point marker

','

pixel marker

'o'

circle marker

'v'

triangle_down marker

'^'

triangle_up marker

'

triangle_left marker

'>'

triangle_right marker

'1'

tri_down marker

'2'

tri_up marker

'3'

tri_left marker

'4'

tri_right marker

's'

square marker

'p'

pentagon marker

'*'

star marker

'h'

hexagon1 marker

'H'

hexagon2 marker

'+'

plus marker

'x'

x marker

'D'

diamond marker

'd'

thin_diamond marker

''

vline marker

'_'

hline marker

1.2.3 轴标题和轴标题的限度

Function

Description

plt.xlabel("date")

X轴标题

plt.ylabel("GMV")

Y轴标题

plt.title("Trends of GMV")

图形标题

plt.xlim(0,7)

X轴的限度

plt.ylim(0,30)

Y轴的限度

1.2.4 同一坐标系多个图形

(1)将数据列和表现形式直接指定列出

plt.plot(s.USERS,"r--",s.gmv,"b+")

plt.plot(t,t,'r--',t,t**2,'bs',t,t**3,'g^')

(2)一条一条的列出

plt.plot(s.USERS,"r--")

plt.plot(s.gmv,"b+")

1.2.5 主次两套坐标系多个图形

plt.plot_date(s.op_time,s.gmv,'c-')

plt.ylabel("GMV")

plt.twinx() 创立一个独立的Y轴,共享坐标轴

plt.plot_date(s.op_time,s.USERS,'g-')

plt.ylabel("Users")

1.2.6 设置legend

import pandas as pd

import matplotlib.pyplot as plt

import matplotlib.lines as mlines

red_line = mlines.Line2D([], [], color='red', label='GMV')

green_line=mlines.Line2D([],[],color='green', label='users') #设置legend的表现形式

s = pd.read_csv("GMV.csv")

Fig = plt.figure(figsize=(8,4)) #创建一个figure

ax1=Fig.add_subplot(221) #画第一个图

plot1=ax1.plot_date(s.op_time,s.gmv,'r-',label="Gmv")

ax1.set_label("GMV")

ax2=ax1.twinx() #共享x轴,创建次Y坐标轴

plot2=ax2.plot_date(s.op_time,s.USERS,'g-',label="Users")

ax2.set_label("Users")

ax2.set_title("Apr-MayGMV/USERs' Trend")

plt.legend(handles=[red_line,green_line])

legend 的位置函数

Location String

Location Code

‘best’

0

‘upper right’

1

‘upper left’

2

‘lower left’

3

‘lower right’

4

‘right’

5

‘center left’

6

‘center right’

7

‘lower center’

8

‘upper center’

9

‘center’

10

2.直方图

ax4=Fig.add_subplot(223)#画第三个图

ax4.hist(s.gmv)

ax4.set_xlabel("GMV")

3.柱状图

import pandas as pd

import matplotlib.pyplot as plt

import numpy as np

import pylab

#coding=utf-8

promote = pd.read_csv("trends.csv")

promote.index = promote['date']

promote.__delitem__('date')

promote['GMV']=promote['GMV']/100000000

#单位为亿

S1=promote[promote.month==11]

#取去年双11的数据

S2=promote[promote.month==6]

#取618的数据

n = len(promote.index)

n1=len(S1.index)

n2=len(S2.index)

X = np.arange(n)+1

X1 = np.arange(n1)+1

Y1 = S1.GMV

X2 = np.arange(n2)+1

Y2 = S2.GMV

#X是1,2,3,4,5,6,7,8,柱的个数

Fig = plt.figure(figsize=(18,12)) #创建一个figure

ax1=Fig.add_subplot(111) #画第一个图

plt.bar(X1, S1.GMV, width=0.35,facecolor = 'lightskyblue',edgecolor = 'white')

plt.bar(X2+n1, S2.GMV, width=0.35,facecolor = 'yellowgreen',edgecolor = 'white')

#X2进行位移蓝色的位置n1

#width:柱的宽度

plt.xticks(X1,fontsize=1)

ax1.set_xticks(X,minor=True) ##设置x轴间隔

ax1.set_xticklabels(promote.index,minor=True)

for x,y in zip(X1,Y1):

plt.text(x+0.1, y+0.05, '%.2f' % y, ha='center', va= 'bottom')

for x,y in zip(X2,Y2):

plt.text(x+0.1+n1, y+0.05, '%.2f' % y, ha='center', va= 'bottom')

#给图加text

plt.title('Trends of GMV')

#将图片存储输出

plt.savefig("Gmv_trends.png")

pylab.show('Gmv_trends.png')

4.多个Index情况

#画四张图,每张图都有两个Index,同时呢需要算出每个index的max,sum,mean,and count

import pandas as pd

import numpy as np

import pylab

#coding=utf-8

promote = pd.read_csv("promote.csv")

print(promote)

promote.columns = ['date','GMV','month','year','quarter']

promax = pd.pivot_table(promote,values='GMV',index=['year','quarter'],aggfunc=max)

prosum = pd.pivot_table(promote,values='GMV',index=['year','quarter'],aggfunc=sum)

proavg = pd.pivot_table(promote,values='GMV',index=['year','quarter'],aggfunc=np.mean)

prolen = pd.pivot_table(promote,values='GMV',index=['year','quarter'],aggfunc=len)

#画四个图,最大值,总和,平均值,天数,解决的问题是多个INDEX如何作图

import matplotlib.pyplot as plt

Fig = plt.figure(figsize=(18,10)) #创建一个figure

ax1=Fig.add_subplot(221) #画第一个图

ax1=plt.gca()

###为了将面向对象的绘图库包装成只使用函数的调用接口,pyplot模块的内部保存了当前图表以及当前子图等信息。当前的图表和子图可以使用plt.gcf()和plt.gca()获得,

###分别表示"Get Current Figure"和"Get Current Axes"。在pyplot模块中,许多函数都是对当前的Figure或Axes对象进行处理,比如说:

###plt.plot()实际上会通过plt.gca()获得当前的Axes对象ax,然后再调用ax.plot()方法实现真正的绘图。

promax.plot(kind="bar",ax=ax1)

plt.grid(True,'both')

####添加图表网格线,设置网格线颜色,线形,宽度和透明度

minor_XT=ax1.get_xaxis().get_majorticklocs()

#Get the major tick locations in data coordinates as a numpy array

promax['XT_V']=minor_XT

#[0,1,2,3,4,5,6,7,8,9,10]

print(promax)

major_XT=promax.groupby(by=promax.index.get_level_values(0)).first()['XT_V'].tolist()

print(major_XT)

#get_level_values()Return an Index of values for requested level, equal to the length

#[0,3,6,9]

promax.__delitem__('XT_V')

###__delitem__(self,key):删除给定键对应的元素。

print(promax)

ax1.set_xticks(minor_XT,minor=True) ##设置x轴间隔

ax1.set_xticklabels(promax.index.get_level_values(1),minor=True)

#设置坐标轴的值

ax1.tick_params(which='major',pad=15) ##Set appearance parameters for ticks and ticklabels.

_=plt.xticks(major_XT,(promax.index.get_level_values(0)).unique(),rotation=0)

plt.title('Max of GMV')

plt.xlabel("year.quarter",fontsize=1)

ax2=Fig.add_subplot(222) #画第二个图

ax2=plt.gca()

prosum.plot(kind="bar",ax=ax2)

plt.grid(True,'both')

minor_XT=ax2.get_xaxis().get_majorticklocs()

prosum['XT_V']=minor_XT

major_XT=prosum.groupby(by=prosum.index.get_level_values(0)).first()['XT_V'].tolist()

prosum.__delitem__('XT_V')

ax2.set_xticks(minor_XT,minor=True)

ax2.set_xticklabels(prosum.index.get_level_values(1),minor=True)

ax2.tick_params(which='major',pad=15)

_=plt.xticks(major_XT,(promax.index.get_level_values(0)).unique(),rotation=0)

plt.title('Sum of GMV')

plt.xlabel("year.quarter",fontsize=1)

ax3=Fig.add_subplot(223) #画第三个图

ax3=plt.gca()

proavg.plot(kind="bar",ax=ax3)

plt.grid(True,'both')

minor_XT=ax3.get_xaxis().get_majorticklocs()

proavg['XT_V']=minor_XT

major_XT=proavg.groupby(by=proavg.index.get_level_values(0)).first()['XT_V'].tolist()

proavg.__delitem__('XT_V')

ax3.set_xticks(minor_XT,minor=True)

ax3.set_xticklabels(proavg.index.get_level_values(1),minor=True)

ax3.tick_params(which='major',pad=15)

_=plt.xticks(major_XT,(proavg.index.get_level_values(0)).unique(),rotation=0)

plt.title('Average of GMV')

#plt.show()

ax4=Fig.add_subplot(224) #画第四个图

ax4=plt.gca()

prolen.plot(kind="bar",ax=ax4)

plt.grid(True,'both')

minor_XT=ax4.get_xaxis().get_majorticklocs()

prolen['XT_V']=minor_XT

major_XT=prolen.groupby(by=prolen.index.get_level_values(0)).first()['XT_V'].tolist()

prolen.__delitem__('XT_V')

ax4.set_xticks(minor_XT,minor=True)

ax4.set_xticklabels(prolen.index.get_level_values(1),minor=True)

ax4.tick_params(which='major',pad=15)

_=plt.xticks(major_XT,(prolen.index.get_level_values(0)).unique(),rotation=0)

plt.title('Days of GMV')

plt.savefig("Gmv_promotion.png")

pylab.show('Gmv_promotion.png')

5.饼图

import pandas as pd

pd.options.mode.chained_assignment = None # default='warn'

import matplotlib.pyplot as plt

# -*- coding: utf-8 -*-

#!/usr/bin/python

import numpy as np

import pylab

promote = pd.read_csv("trends.csv")

promote.index = promote['date']

X1=promote[promote.month==6]

X2=promote[promote.month==11]

S1=X1[X1['date'].isin(['6/17','6/18','6/19','6/20'])]

S2=X2[X2['date'].isin(['11/10','11/11','11/12','11/13'])]

S3=X1[~X1['date'].isin(['6/17','6/18','6/19','6/20'])] #取反

S4=X2[~X2['date'].isin(['11/10','11/11','11/12','11/13'])]#取反

#取高潮时期的数据

S1.__delitem__('date')

S1['GMV'] = S1['GMV']/100000000

S2.__delitem__('date')

S2['GMV'] = S2['GMV']/100000000

S3['GMV'] = S3['GMV']/100000000

S3.__delitem__('date')

S4['GMV'] = S4['GMV']/100000000

S4.__delitem__('date')

X1.__delitem__('date')

X1['GMV'] = X1['GMV']/100000000

X2.__delitem__('date')

X2['GMV'] = X2['GMV']/100000000

#单位为亿

#promote.__setitem__('GMV', 'date','nGMV')

#promote.__delitem__('nGMV')

#取大促时间的数据

n1=S1['GMV']

n2=X1['GMV']

n3=S3['GMV']

n4=S4['GMV']

w1=n1.sum()/n2.sum()*100

r1=n1.mean()

r2=n3.mean()

print(w1)

m1=S2['GMV']

m2=X2['GMV']

w2=m1.sum()/m2.sum()*100

r3=m1.mean()

r4=n4.mean()

#双11占比

size1=[w1,100-w1]

#618占比

size2=[w2,100-w2]

Fig=plt.figure(figsize=(18,12))

ax1=Fig.add_subplot(221) #画第一个图

ax1.labels = [u'618session',u'normal_session']

ax1.colors = ['yellowgreen','lightskyblue']

explode = (0.05,0)

patches,l_text,p_text = plt.pie(size1,explode=explode,labels=ax1.labels,colors=ax1.colors,

labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,

startangle = 90,pctdistance = 0.6)

#labeldistance,文本的位置离远点有多远,1.1指1.1倍半径的位置

#autopct,圆里面的文本格式,%3.1f%%表示小数有三位,整数有一位的浮点数

#shadow,饼是否有阴影

#startangle,起始角度,0,表示从0开始逆时针转,为第一块。一般选择从90度开始比较好看

#pctdistance,百分比的text离圆心的距离

#patches, l_texts, p_texts,为了得到饼图的返回值,p_texts饼图内部文本的,l_texts饼图外label的文本

#改变文本的大小

#方法是把每一个text遍历。调用set_size方法设置它的属性

for t in l_text:

t.set_size=(30)

for t in p_text:

t.set_size=(20)

# 设置x,y轴刻度一致,这样饼图才能是圆的

ax1.axis('equal')

plt.title("618 session")

ax1.legend()

ax2=Fig.add_subplot(222) #画第二个图

ax2.labels = [u'Double11session',u'normal_session']

ax2.colors = ['yellowgreen','lightskyblue']

explode = (0.05,0)

patches,l_text,p_text = plt.pie(size2,explode=explode,labels=ax2.labels,colors=ax2.colors,

labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,

startangle = 90,pctdistance = 0.6)

#labeldistance,文本的位置离远点有多远,1.1指1.1倍半径的位置

#autopct,圆里面的文本格式,%3.1f%%表示小数有三位,整数有一位的浮点数

#shadow,饼是否有阴影

#startangle,起始角度,0,表示从0开始逆时针转,为第一块。一般选择从90度开始比较好看

#pctdistance,百分比的text离圆心的距离

#patches, l_texts, p_texts,为了得到饼图的返回值,p_texts饼图内部文本的,l_texts饼图外label的文本

#改变文本的大小

#方法是把每一个text遍历。调用set_size方法设置它的属性

for t in l_text:

t.set_size=(30)

for t in p_text:

t.set_size=(20)

# 设置x,y轴刻度一致,这样饼图才能是圆的

ax2.axis('equal')

plt.title("Double 11 session")

ax2.legend()

ax3=Fig.add_subplot(223,frameon=False) #画第三个图

ax3.labels = [u'618session',u'normal_session']

ax3.colors = ['yellowgreen','lightskyblue']

X1=[1,2]

Y1=[r1,r2]

plt.bar(X1, Y1, width=0.35,facecolor = 'lightskyblue',edgecolor = 'white')

#选择是否显示刻度值:x轴上,1为下,2为上;y轴上,1为左,2为右;

for tick in ax3.xaxis.get_major_ticks():

tick.label1On = False

tick.label2On = False

for tick in ax3.yaxis.get_major_ticks():

tick.label1On = False

tick.label2On = False

#选择如何显示刻度

ax3.xaxis.set_ticks_position('none')

ax3.yaxis.set_ticks_position('none')

ax3.set_xticks(X1,minor=True) ##设置x轴间隔

ax3.set_xticklabels(ax3.labels,minor=True)

for x,y in zip(X1,Y1):

plt.text(x, y+0.05, '%.2f' % y, ha='center', va= 'bottom')

ax4=Fig.add_subplot(224,frameon=False) #画第四个图,不显示坐标轴,但是刻度仍在

ax4.labels = [u'Double 11 session',u'normal_session']

ax4.colors = ['yellowgreen','lightskyblue']

X2=[1,2]

Y2=[r3,r4]

plt.bar(X2, Y2, width=0.35,facecolor = 'lightskyblue',edgecolor = 'white')

#选择是否显示刻度值:x轴上,1为下,2为上;y轴上,1为左,2为右;

for tick in ax4.xaxis.get_major_ticks():

tick.label1On = False

tick.label2On = False

for tick in ax4.yaxis.get_major_ticks():

tick.label1On = False

tick.label2On = False

#选择如何显示刻度

ax4.xaxis.set_ticks_position('none')

ax4.yaxis.set_ticks_position('none')

ax4.set_xticks(X2,minor=True) ##设置x轴间隔

ax4.set_xticklabels(ax4.labels,minor=True)

for x,y in zip(X2,Y2):

plt.text(x, y+0.05, '%.2f' % y, ha='center', va= 'bottom')

plt.savefig("Gmv_ratio.png")

pylab.show('Gmv_ratio.png')

#https://yantianmin.wordpress.com/2010/08/25/matplotlib%E7%9A%84%E4%B8%80%E4%BA%9B%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4%E5%92%8C%E6%8A%80%E5%B7%A7/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值