python数据分析-10seaborn绘图

"""
import pandas as pd
import numpy as np
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
import seaborn as sns
"""
"""
#Seaborn---是matplotlib的扩展
#演示例子:
#有个文件"iris.csv"
iris = pd.read_csv("iris.csv")
print(iris.head())
# SepalLength SepalWidth PetalLength PetalWidth Name
# 0 5.1 3.5 1.4 0.2 Iris-setosaa
# 1 4.9 3.0 1.4 0.2 Iris-setosaa
# 2 4.7 3.2 1.3 0.2 Iris-setosaa
# 3 4.6 3.1 1.5 0.2 Iris-setosaa
# 4 5.0 3.6 1.4 0.2 Iris-setosaa
#需求:画一个花瓣(petal)和花萼(sepal)长度的散点图,并且点的颜色要区分鸢尾花的种类
print(iris.Name.unique())
#['Iris-setosa','Iris-versicolor','Iris-virginica']#一共有三种花
color_map = dict(zip(iris.Name.unique(),['blue','green','red']))
for species,group in iris.groupby("Name"):
plt.scatter(group["PetalLength"],group["SepalLength"],color=color_map[species],alpha=0.3,edgecolors=None,label=species)
plt.legend(frameon=True,title="Name")
plt.xlabel("PetalLength")
plt.ylabel("sepalLength")
plt.show()


#seaborn画图超级简单,一行代码即可
sns.lmplot("PetalLength","sepalLength",iris,hue="Name",fit_reg=False)
"""

"""
#----------------
#seaborn实现直方图和密度图
s1 = Series(np.random.randn(1000))
#画直方图
# plt.hist(s1)
# plt.show()
#画密度图:
# s1.plot(kind="kde")#可以画直方图
# plt.show()


#使用sns画图:distplot()直方图和密度图
# sns.distplot(s1,bins=20,hist=True,kde=False,rug=True)
# plt.show()

#sns画图:kdeplot()画密度图
# sns.kdeplot(s1,shade=True,color="r")
# plt.show()
"""

"""
#----------------------------
#seaborn实现柱状图和热力图
df = sns.load_dataset("flights")#这是一个在线的数据包,直接可以使用函数获取到
#print(df.head())
# year month passengers
# 0 1949 January 112
# 1 1949 February 118
# 2 1949 March 132
# 3 1949 April 129
# 4 1949 May 121
#print(df.shape)#(144, 3)

df = df.pivot(index="month",columns="year",values="passengers")
#print(df)
# year 1949 1950 1951 1952 1953 ... 1956 1957 1958 1959 1960
# month ...
# January 112 115 145 171 196 ... 284 315 340 360 417
# February 118 126 150 180 196 ... 277 301 318 342 391
# March 132 141 178 193 236 ... 317 356 362 406 419
# April 129 135 163 181 235 ... 313 348 348 396 461
# May 121 125 172 183 229 ... 318 355 363 420 472
# June 135 149 178 218 243 ... 374 422 435 472 535
# July 148 170 199 230 264 ... 413 465 491 548 622
# August 148 170 199 242 272 ... 405 467 505 559 606
# September 136 158 184 209 237 ... 355 404 404 463 508
# October 119 133 162 191 211 ... 306 347 359 407 461
# November 104 114 146 172 180 ... 271 305 310 362 390
# December 118 140 166 194 201 ... 306 336 337 405 432
#
# [12 rows x 12 columns]

#sns.heatmap(df)
# df.plot()
# plt.show()

# sns.heatmap(df,annot=True,fmt='d')#在每个方块里显示具体数据
# plt.show()


s = df.sum()
#print(s)
# year
# 1949 1520
# 1950 1676
# 1951 2042
# 1952 2364
# 1953 2700
# 1954 2867
# 1955 3408
# 1956 3939
# 1957 4421
# 1958 4572
# 1959 5140
# 1960 5714
# dtype: int64

# print(s.index)
# Int64Index([1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959,
# 1960],
# dtype='int64', name='year')
# print(s.values)#[1520 1676 2042 2364 2700 2867 3408 3939 4421 4572 5140 5714]
#画柱状图
#sns.barplot(x=s.index,y=s.values)
#同样方法:
# s.plot(kind="bar")
# plt.show()
"""

#----------------------
#seaborn设置图形显示效果
import pandas as pd
import numpy as np
from pandas import Series,DataFrame
import matplotlib.pyplot as plt


# x = np.linspace(0,14,100)
# y1 = np.sin(x)
# y2 = np.sin(x+2)*1.25
#
# def sinplot():
# plt.plot(x,y1)
# plt.plot(x,y2)
# sinplot()
# plt.show()

import seaborn as sns
"""
#seaborn提供了5中风格:这是背景款式的设置
style = ['darkgrid','dark','white','whitegrid','tricks']
sns.set_style(style[0],{ 'grid.color': 'red'})
#sinplot()
#plt.show()
#print(sns.axes_style())#会显示出来设置的样式详细信息,可以据此信息在上面进行具体的设置,放在字典里即可更改设置了
sns.set()#清空设置
sinplot()
plt.show()
"""
"""
#sns设置曲线
#plotting_context() and set_context()
#内置了四种曲线样式
context = ['paper','notebook','talk','poster']
sns.set_context(context[2],rc={'grid.linewidth': 3.0})
sinplot()
plt.show()
print(sns.plotting_context())#显示所有曲线的设置参数信息,进而在上面进行设置
"""

#------------------------------
#Seaborn 强大的调色功能
def sinplot():
x = np.linspace(0,14,100)
plt.figure(figsize=(8,6))
for i in range(4):
plt.plot(x,np.sin(x+i)*(i+0.75),label="sin(x+%s)*(%s+0.75)" % (i,i))
plt.legend()

import seaborn as sns
# sinplot()
# plt.show()
#print(sns.color_palette())#返回调色板信息,共有六种色板
# [(0.12156862745098039, 0.4666666666666667, 0.7058823529411765), (1.0, 0.4980392156862745, 0.054901960784313725), (0.17254901960784313, 0.6274509803921569, 0.17254901960784313), (0.8392156862745098, 0.15294117647058825, 0.1568627450980392), (0.5803921568627451, 0.403921568627451, 0.7411764705882353), (0.5490196078431373, 0.33725490196078434, 0.29411764705882354), (0.8901960784313725, 0.4666666666666667, 0.7607843137254902), (0.4980392156862745, 0.4980392156862745, 0.4980392156862745), (0.7372549019607844, 0.7411764705882353, 0.13333333333333333), (0.09019607843137255, 0.7450980392156863, 0.8117647058823529)]
# sns.palplot(sns.color_palette())#显示色板图像
# plt.show()
#seaborn提供了六种主题的色板
# pal_style = ['deep','muted','pastel','bright','dark','colorblind']
# sns.palplot(sns.color_palette("deep"))
# plt.show()

# sns.set_palette(sns.color_palette("dark"))
# sinplot()
# plt.show()
#
# sns.set()#恢复原始状态
# sinplot()
# plt.show()


#在with语句里面操作,会显示with里面的操作,然后关闭,显示原始操作
# with sns.color_palette("dark"):
# sinplot()
# plt.show()
#
# sinplot()
# plt.show()


#自定义色块
#pal1 = sns.color_palette([(0.5,0.1,0.7),(0.3,0.1,0.9)])
# sns.palplot(pal1)
# plt.show()
#另一种方法:
sns.palplot(sns.color_palette("hls",8))
plt.show()
posted @ 2019-07-04 10:44  nikecode  阅读(760)  评论(0编辑  收藏  举报