matplotlib

折线图

import matplotlib
import matplotlib.pyplot as plt
x = [0,1,2,3,4]  #  x 轴数据
y = [0,1,2,3,4]  #  y 轴数据
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.title('这张图的内容介绍')
plt.xlabel('x 轴数据')
plt.ylabel('y 轴数据')
plt.plot(x,y)


dev_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]

dev_y = [38496, 42000, 46752, 49320, 53200,
         56000, 62316, 64928, 67317, 68748, 73752]
py_dev_y = [45372, 48876, 53850, 57287, 63016,
            65998, 70003, 70000, 71496, 75370, 83640]
js_dev_y = [37810, 43515, 46823, 49293, 53437,
            56373, 62375, 66674, 68745, 68746, 74583]
print (plt.style.available)
plt.xkcd()
plt.plot(dev_x,dev_y,color='#b82410',label='All')
plt.plot(dev_x,py_dev_y,'^--',color='#2614e8',label='Python')
#  fmt = [颜色][marker][line]
plt.plot(dev_x,js_dev_y,'go--',label='Js')
plt.legend()
plt.title('Title')
plt.xlabel('Age')
plt.ylabel('Income')
plt.grid()
plt.savefig('img01.png')
plt.show()

柱状图

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
data = pd.read_csv('data.csv')
lang_responses = data['LanguagesWorkedWith']
cnt= Counter()
for i in lang_responses:
    cnt.update(i.split(';'))
lang =[]
popularity = []
for i in cnt.most_common(15):
    lang.append(i[0])
    popularity.append(i[1])

plt.figure(figsize=(9,6))
plt.bar(lang,popularity)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.title("最受欢迎的语言")
plt.xlabel("最喜欢的编程语言")
plt.ylabel("人次")
plt.xticks(rotation=65)

#横向排列
lang.reverse()
popularity.reverse()
plt.figure(figsize=(12,9))
#纵向画图
plt.barh(lang,popularity)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.title("最受欢迎的语言")
plt.xlabel("最喜欢的编程语言")
plt.ylabel("人次")


饼状图

import matplotlib.pyplot as plt
list1 =['JavaScript','HTML/CSS', 'SQL', 'Python', 'Java']
list2 = [59219, 55466, 47544, 36443, 35917]
plt.pie(list2,labels=list1)
explo = [0,0,0,0.1,0]
plt.pie(list2,labels=list1,explode=explo)

explo = [0.1,0,0,0,0]
plt.pie(list2,labels=list1,explode=explo,shadow=True)

explo = [0.1,0,0,0,0]
plt.pie(list2,labels=list1,explode=explo,shadow=True,startangle=0,autopct='%1.2f%%')

explo = [0.1,0,0,0,0]
plt.pie(list2,labels=list1,explode=explo,shadow=True,startangle=0,autopct='%1.2f%%',wedgeprops={'edgecolor':'black'})

explo = [0.1,0,0,0,0]
plt.pie(list2,labels=list1,explode=explo,shadow=True,startangle=0,autopct='%1.2f%%',wedgeprops={'edgecolor':'black'})
plt.title('Dev Lang pct')
plt.tight_layout()

#案例2
fifa = pd.read_csv('fifa_data.csv')
left = fifa.loc[fifa['Preferred Foot']=='Left'].count()[0]
right = fifa.loc[fifa['Preferred Foot']=='Right'].count()[0]
labels = ['Left','Right']
explo=[0.1,0]
plt.pie([left,right],labels=labels,explode=explo,shadow=True,startangle=0,autopct='%1.2f%%',wedgeprops={'edgecolor':'black'})

#处理体重fifa.Weight
def func1(d1):
    if type(d1)==str:
        return int(d1.strip('lbs'))
fifa['Weight2']=fifa.Weight.apply(func1)
#统计元素个数,把每一列都统计到第一行中,以第0列为统计结果
class1=fifa.loc[fifa.Weight2 < 125].count()[0]
class2 = fifa.loc[(fifa.Weight2 >= 125) & (fifa.Weight2 < 150)].count()[0]
class3 = fifa.loc[(fifa.Weight2 >= 150) & (fifa.Weight2 < 175)].count()[0]
class4 = fifa.loc[(fifa.Weight2 >= 175) & (fifa.Weight2 < 200)].count()[0]
class5 = fifa.loc[fifa.Weight2 > 200].count()[0]
list3= [class1,class2,class3,class4,class5]
plt.figure(figsize=(8,5),dpi = 100)
labels = ['< 125 ','125-150','150-175','175-200', '> 200']
explo=[0.4,0.2,0,0,0.4]
plt.pie(list3,labels=labels,explode=explo,pctdistance=0.8,shadow=True,startangle=0,autopct='%1.2f%%',wedgeprops={'edgecolor':'black'})

堆叠图

import matplotlib.pyplot as plt
import numpy as np
plt.style.use('fivethirtyeight')
minutes = [1, 2, 3, 4, 5, 6, 7, 8, 9]

player1 = [1, 2, 3, 3, 4, 4, 4, 4, 5]
player2 = [1, 1, 1, 1, 2, 2, 2, 3, 4]
player3 = [1, 5, 6, 2, 2, 2, 3, 3, 3]

#生成0-9的序列
index_x = np.arange(len(minutes))

w= 0.15
#下面是堆积在一起的情况,但是彼此间保留w的距离,效果好
plt.bar(index_x-w,player1,width=w)
plt.bar(index_x,player2,width=w)
plt.bar(index_x+w,player3,width=w)
plt.bar(index_x+2*w,player3,width=w)

#下面是堆积在一起的情况,效果比较差
plt.bar(minutes,player1,width=w)
plt.bar(minutes,player2,width=w)
plt.bar(minutes,player3,width=w)

#采用折线堆叠
labels=['class1','class2','class3']
colors = ['Blue','Red','Green']
plt.stackplot(minutes,player1,player2,player3,labels=labels,colors=colors)
plt.legend(loc=(0.07,0.6))

带有面积阴影的折线图

import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv('data.csv')

plt.plot(data['Age'],data['All_Devs'],label='All')
plt.plot(data['Age'],data['Python'],label='Python')
plt.legend()
overall_mid = 58000
plt.fill_between(data['Age'],data['Python'],overall_mid,where=(data['Python'] < overall_mid) ,interpolate=True,alpha = 0.2)
#plt.fill_between(data['Age'],data['Python'],overall_mid,where=(data['Python'] > overall_mid),interpolate=True,alpha = 0.2)

#两条线之间的交叉
plt.plot(data['Age'],data['All_Devs'],label='All')
plt.plot(data['Age'],data['Python'],label='Python')

overall_mid = 58000
# plt.fill_between(data['Age'],data['Python'],overall_mid,where=(data['Python'] > overall_mid)
#                  ,interpolate=True,alpha = 0.2)
plt.fill_between(data['Age'],data['Python'],data['All_Devs'],where=(data['Python'] >  data['All_Devs']),interpolate=True,alpha = 0.2,label='Python > All')
plt.fill_between(data['Age'],data['Python'],data['All_Devs'],where=(data['Python'] <= data['All_Devs']),interpolate=True,alpha = 0.2,color='red',label='Python <= All')

plt.legend()

直方图

import matplotlib.pyplot as plt
import pandas as pd
ages = [18, 19, 21, 25, 26, 26, 30, 32, 38, 45, 55]
bins=[20,30,40,50,60]
plt.hist(ages,edgecolor='black',bins=bins)


data = pd.read_csv('data.csv')
bins=[20,30,40,50,60]
plt.hist(data.Age,edgecolor='black',bins=5)

bins=[10,20,30,40,50,60,70,80,90,100]
plt.hist(data.Age,edgecolor='black',bins=bins)

bins=[10,20,30,40,50,60,70,80,90,100]
#在纵坐标上设置对数方式
plt.hist(data.Age,edgecolor='black',bins=bins,log=True)

#展示年龄的平均线
bins=[10,20,30,40,50,60,70,80,90,100]
plt.hist(data.Age,edgecolor='black',bins=bins)
median_age= 29
plt.axvline(median_age,color='red',label = 'Median')
plt.legend()

散点图

import pandas as pd
import matplotlib.pyplot as plt
x = [5, 7, 8, 5, 6, 7, 9, 2, 3, 4, 4, 4, 2, 6, 3, 6, 8, 6, 4, 1]
y = [7, 4, 3, 9, 1, 3, 2, 5, 2, 4, 8, 7, 1, 6, 4, 9, 7, 7, 5, 1]
plt.scatter(x,y,s=100,c='red',edgecolor='black')
plt.grid()

plt.scatter(x,y,s=100,c='red',edgecolor='black',linewidth=1,alpha=0.25)
plt.grid()


x = [5, 7, 8, 5, 6, 7, 9, 2, 3, 4, 4, 4, 2, 6, 3, 6, 8, 6, 4, 1]
y = [7, 4, 3, 9, 1, 3, 2, 5, 2, 4, 8, 7, 1, 6, 4, 9, 7, 7, 5, 1]
colors = [7, 5, 9, 7, 5, 7, 2, 5, 3, 7, 1, 2, 8, 1, 9, 2, 5, 6, 7, 5]
sizes = [209, 486, 381, 255, 191, 315, 185, 228, 174,
         538, 239, 394, 399, 153, 273, 293, 436, 501, 397, 539]

plt.scatter(x,y,s=sizes,c=colors,edgecolor='black',linewidth=1,alpha=0.95)
plt.grid()
cbar = plt.colorbar()
cbar.set_label('Label')

df = pd.read_csv('2019-05-31-data.csv')
plt.figure(figsize=(10,6))
plt.scatter(df.view_count,df.likes,edgecolor='black',linewidth=1,alpha=0.9)
#在横坐标和纵坐标设置对数
plt.xscale('log')
plt.yscale('log')

plt.figure(figsize=(10,6))
plt.scatter(df.view_count,df.likes,edgecolor='black',linewidth=1,alpha=0.9,c=df.ratio,cmap='summer')
#采用log
plt.xscale('log')
plt.yscale('log')
cbar =plt.colorbar()
cbar.set_label('Like/Dislike ')

时序数据的可视化

import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime,timedelta
from matplotlib import dates as mpl_dates
x = ['2019-5-24','2019-5-25','2019-5-26','2019-5-27','2019-5-28','2019-5-29','2019-5-30','2019-6-30']
y = [0,1,3,4,6,5,7,3]
plt.plot(x,y)

x2 = [
    datetime(2019,5,24),
    datetime(2019,5,25),
    datetime(2019,5,26),
    datetime(2019,5,27),
    datetime(2019,5,28),
    datetime(2019,5,29),
    datetime(2019,5,30),
    datetime(2019,6,24),
    datetime(2019,6,25),
    datetime(2019,6,26),
    datetime(2019,6,27),
    datetime(2019,6,28),
    datetime(2019,6,29),
    datetime(2019,6,30),    
]
y2 = [0,1,3,4,6,5,7,0,1,3,4,6,5,7]
plt.style.use('seaborn')
plt.plot_date(x2,y2, linestyle='solid')
plt.gcf().autofmt_xdate()
date_format = mpl_dates.DateFormatter('%b,%d %Y')
plt.gca().xaxis.set_major_formatter(date_format)

#案例2
df = pd.read_csv('data.csv')
plt.plot(df.Date,df.Close)

df.Date = pd.to_datetime(df.Date)
df.sort_values('Date',inplace=True)
plt.plot_date(df.Date,df.Close, linestyle='solid')
plt.gcf().autofmt_xdate()
date_format = mpl_dates.DateFormatter('%b,%d %Y')
plt.gca().xaxis.set_major_formatter(date_format)
plt.title('Bitcoin Price')
plt.xlabel('Date')
plt.ylabel('Price USD')

实时数据的可视化

import pandas as pd
import matplotlib.pyplot as plt
import random
from IPython.display import HTML
from itertools import count

plt.style.use('fivethirtyeight')
#定义计数器
index = count()
def animate():
    x1.append(next(index))
    y1.append(random.randint(0,50))
    plt.cla()
    plt.plot(x1,y1)
#调用动画函数实现画图
animate()


#动画案例2
from matplotlib.animation  import FuncAnimation
ani = FuncAnimation(plt.gcf(),animate,interval=1000)
HTML(ani.to_jshtml())

#动画案例3,数据来至文件
def animate2(i):
    dfa = pd.read_csv('data.txt')
    x = dfa.x
    y1 = dfa.y1
    y2 = dfa.y2
    plt.cla()
    plt.plot(x,y1,label='Stock1')
    plt.plot(x,y2,label='Stock2')
ani2 = FuncAnimation(plt.gcf(),animate2,interval=1000)
HTML(ani2.to_jshtml())



#动画案例3,生成data.txt数据程序
import csv
import random
import time
x1_value = 0
y1_value = 1000
y2_value = 1000
fieldname=["x1","y1","y2"]
with open('data.txt','w') as csvfile:
    csv_w = csv.DictWriter(csvfile,fieldnames=fieldname)
    csv_w.writeheader()
while True:
    with open('data.txt','a') as csvfile:
        csv_w = csv.DictWriter(csvfile,fieldnames=fieldname)
        info = {
            "x1" : x1_value,
            "y1" : y1_value ,
            "y2" : y2_value ,
        }
        x_value += 1
        y1_value = y1_value + random.randint(-6,10)
        y2_value = y2_value + random.randint(-4,5)
        csv_w.writerow(info)
    time.sleep(1)
    

图表的多重绘制

import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('seaborn')

data = pd.read_csv('data.csv')
ages = data.Age
all_dev = data.All_Devs
py= data.Python
js = data.JavaScript

#图交织在一起
plt.plot(ages,all_dev,label='All')
plt.plot(ages,py,label='Python')
plt.plot(ages,js,label='JS')
plt.legend()
plt.xlabel('Age')
plt.ylabel('Sal')

#3行1列绘制3张图
fig , (ax1,ax2,ax3) = plt.subplots(nrows=3,ncols=1,sharex=True,sharey=True)
print (ax1)
ax1.plot(ages,all_dev,label='All')
ax2.plot(ages,py,label='Python',color='g')
ax3.plot(ages,js,label='JS',color='r')
ax1.legend()
ax2.legend()
ax3.legend()
# ax1.set_xlabel('Age')
ax3.set_xlabel('Age')
ax1.set_ylabel('Salary')


fig = plt.figure()
#2行2列(共计4个左右排列)第一个
ax1 = fig.add_subplot(221)
#2行2列(共计4个左右排列)第二个
ax2 = fig.add_subplot(222)
#2行1列第二个,行单元格合并后的第二个元素
ax3 = fig.add_subplot(212)
print (ax1)
ax1.plot(ages,all_dev,label='All')
ax2.plot(ages,py,label='Python',color='g')
ax3.plot(ages,js,label='JS',color='r')
ax1.legend()
ax2.legend()
ax3.legend()
# ax1.set_xlabel('Age')
ax3.set_xlabel('Age')
ax1.set_ylabel('Salary')

#采用栅格方式
fig = plt.figure()
# ax1 = fig.add_subplot(221)
# ax2 = fig.add_subplot(222)
# ax3 = fig.add_subplot(212)
ax1 = plt.subplot2grid((6,2),(0,0),rowspan=2,colspan=1)
ax2 = plt.subplot2grid((6,2),(0,1),rowspan=2,colspan=1)
ax3 = plt.subplot2grid((6,2),(2,0),rowspan=2,colspan=2)
ax4 = plt.subplot2grid((6,2),(4,0),rowspan=2,colspan=1)
ax5 = plt.subplot2grid((6,2),(4,1),rowspan=2,colspan=1)
print (ax1)
ax1.plot(ages,all_dev,label='All')
ax2.plot(ages,py,label='Python',color='g')
ax3.plot(ages,js,label='JS',color='r')
ax4.plot(ages,py,label='Python',color='g')
ax5.plot(ages,js,label='JS',color='r')
ax1.legend()
ax2.legend()
ax3.legend()
# ax1.set_xlabel('Age')
ax3.set_xlabel('Age')
ax1.set_ylabel('Salary')

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值