Python数据可视化matplotlib(三)——绘制基本的图表
Python数据可视化matplotlib(三)——绘制基本的图表
怎样快速学会韩语import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
% matplotlib inline
import matplotlib.style as psl
psl.u('_classic_test')
基本图表绘制 plt.plot()
图表类别:线形图、柱状图、密度图,以横纵坐标两个维度为主
regard是什么意思同时可延展出多种其他图表样式清明节的英文
plt.plot(kind=‘line’, ax=None, figsize=None, u_index=True, title=None, grid=None, legend=Fal,
style=None, logx=Fal, logy=Fal, loglog=Fal, xticks=None, yticks=None, xlim=None, ylim=None,
rot=None, fontsize=None, colormap=None, table=Fal, yerr=None, xerr=None, label=None, condary_y=Fal, **kwds)线性表
Series直接⽣成图表
ts = pd.Series(np.random.randn(1000),index=pd.date_range('1/1/2010',periods=1000))
ts = ts.cumsum()
ts.plot(kind='line',#图表类型
color='r',
style='-gx',
alpha=0.5,
u_index=True,#是否使⽤数据中给出的index
rot=0,# 旋转刻度标签
ylim=[-50,50],
yticks=list(range(-50,50,10)),
title='time ries',
legend=True,
label='cad')
# Series.plot():ries的index为横坐标,value为纵坐标
# kind → line,(折线图,柱状图,柱状图-横...)
# label →图例标签,Dataframe格式以列名为label
# style →风格字符串,这⾥包括了linestyle(-),marker(.),color(g)
# color →颜⾊,有color指定时候,以color颜⾊为准
# alpha →透明度,0-1
# u_index →将索引⽤为刻度标签,默认为True
# rot →旋转刻度标签,0-360
# grid →显⽰⽹格,⼀般直接⽤id
# xlim,ylim → x,y轴界限
# xticks,yticks → x,y轴刻度值
# figsize →图像⼤⼩
# title →图名
# legend →是否显⽰图例,⼀般直接⽤plt.legend()
# 也可以→ plt.plot()
Dataframe直接⽣成图表
df = pd.DataFrame(np.random.randn(1000,4), index = ts.index, columns=list('abcd')) df = df.cumsum()连衣裙的英文
df.plot(style='--.',
alpha=0.8,
ylim=[-100,100],
figsize=(10,8),
grid=True,
yticks=list(range(-100,125,25)),
title='text',
subplots=True,#是否将每个系列分成不同的⼦图
)
# subplots →是否将各个列绘制到不同图表,默认Fal
# 也可以→ plt.plot(df)
柱状图与堆叠图
直接⽣成图
# 柱状图与堆叠图
# plt.xkcd() #漫画风格
fig,axes=plt.subplots(4,1, figsize=(12,12))
s = pd.Series(np.random.randint(0,10,16), index=list('abcdfeghjklmnopq')) df = pd.DataFrame(np.random.rand(10,3), columns=['a','b','c'])
#单系列柱状图
s.plot(kind='bar',ax=axes[0],grid=True,legend=True,label='s', alpha=0.6)
#多系列柱状图
df.plot(kind='bar', ax=axes[1],colormap='Reds_r')
#多系列堆叠图
df.plot(kind='bar',ax=axes[2],colormap='Blues_r', stacked=True)
# stacked →堆叠
df.plot.barh(ax = axes[3],grid =True,stacked=True,colormap ='BuGn_r') # 新版本plt.plot.<kind>
柱状图 plt.bar()
plt.figure(figsize=(10,4))
x = np.arange(10)
y1 = np.random.rand(10)
y2 = np.random.rand(10)
gressoplt.bar(x, y1, width =0.8, facecolor ='green', edgecolor='black', yerr = y1*0.1)
plt.bar(x,-y2, width =0.8, facecolor ='yellow', edgecolor='black', yerr = y2*0.1)
# x,y参数:x,y值
toaster
# width:宽度⽐例
# facecolor柱状图⾥填充的颜⾊、edgecolor是边框的颜⾊
# left-每个柱x轴左边界,bottom-每个柱y轴下边界→ bottom扩展即可化为⽢特图 Gantt Chart
# align:决定整个bar图分布,默认left表⽰默认从左边界开始绘制,center会将图绘制在中间位置
# xerr/yerr :x/y⽅向error bar 误差值
voltage是什么意思for i, j in zip(x, y1):
<(i-0.2, j+0.1,'%.2f'%j, color='blue')
for i, j in zip(x, y2):
<(i,-j-0.2,'%.2f'%j, color='blue')
# 给图添加text
# zip() 函数⽤于将可迭代的对象作为参数,将对象中对应的元素打包成⼀个个元组,然后返回由这些元组组成的列表。
外嵌图表
# 外嵌图表plt.table()
'''
table(cellText=None, cellColours=None,cellLoc='right', colWidths=None,rowLabels=None, rowColours=None, rowLoc='left', colLabels=None, colColours=None, colLoc='center',loc='bottom', bbox=None)
'''
data =[[66386,174296,75131,577908,32015],
[58230,381139,78045,99308,160454],
[89135,80552,152558,497981,603535],
[78415,81858,150656,193263,69638],
[139361,331509,343164,781380,52269]]
columns=('Freeze','Wind','Flood','Quake','Hail')
rows =['%d year'% x for x in(100,50,20,10,5)]
df = pd.DataFrame(data, columns=('Freeze','Wind','Flood','Quake','Hail'),
index=rows)
print(df)
df.plot(kind='bar', grid=True, colormap='Blues_r',stacked=True,
edgecolor='black',rot=0)
#创建堆叠图
plt.table(cellText = data,
cellLoc ='center',
cellColours =None,
rowLabels = rows,
rowColours = BuPu(np.linspace(0,0.5,5))[::-1],# BuPu可替换成其他colormap
colLabels = columns,
colColours = Reds(np.linspace(0,0.5,5))[::-1],
rowLoc='right',
loc='bottom')
天外有天的意思
# cellText:表格⽂本
# cellLoc:cell内⽂本对齐位置
# rowLabels:⾏标签
# colLabels:列标签
局外人英文# rowLoc:⾏标签对齐位置
local
# loc:表格位置→ left,right,top,bottom
Freeze Wind Flood Quake Hail
100 year 66386 174296 75131 577908 32015
50 year 58230 381139 78045 99308 160454
20 year 89135 80552 152558 497981 603535
10 year 78415 81858 150656 193263 69638
5 year 139361 331509 343164 781380 52269
⾯积图,填图,饼图
⾯积图 plt.plot.area()