其他
软件应用丨Seaborn简单画图:(二)
版权声明:本文为CSDN博主「向前走别回头」的原创文章合辑,遵循 CC 4.0 BY-SA 版权协议,特此附上原文出处链接及本声明。
原文链接:
https://blog.csdn.net/weixin_39778570/article/details/81146473
https://blog.csdn.net/weixin_39778570/article/details/81147023
https://blog.csdn.net/weixin_39778570/article/details/81147415
柱状图和热力图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series, DataFrame
%matplotlib inline
import seaborn as sns
df = pd.read_clipboard()
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
df.shape
(144, 3)
# 用透视表转换
df = df.pivot(index='month', columns='year',values='passengers')
df
year 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
month
April 129 135 163 181 235 227 269 313 348 348 396 461
August 148 170 199 242 272 293 347 405 467 505 559 606
December 118 140 166 194 201 229 278 306 336 337 405 432
February 118 126 150 180 196 188 233 277 301 318 342 391
January 112 115 145 171 196 204 242 284 315 340 360 417
July 148 170 199 230 264 302 364 413 465 491 548 622
June 135 149 178 218 243 264 315 374 422 435 472 535
March 132 141 178 193 236 235 267 317 356 362 406 419
May 121 125 172 183 229 234 270 318 355 363 420 472
November 104 114 146 172 180 203 237 271 305 310 362 390
October 119 133 162 191 211 229 274 306 347 359 407 461
September 136 158 184 209 237 259 312 355 404 404 463 508
# 热力图
sns.heatmap(df)
<matplotlib.axes._subplots.AxesSubplot at 0x2214cab7e10>
# 按列的折现图
df.plot()
# annot描述
sns.heatmap(df, annot=True)
# fmt设置显示格式
sns.heatmap(df, annot=True, fmt='d')
s = df.sum()
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
# 柱状图
sns.barplot(x=s.index, y=s.values)
s.plot(kind='bar')
设置图形显示效果
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
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()
axex_style and set_style
import seaborn as sns
sns.set()
sinplot()
style = ['white', 'dark', 'whitegrid', 'darkgrid', 'ticks']
sns.set_style(style[0])
sinplot()
sns.set_style(style[1])
sinplot()
# 可以通过字典设置风格
sns.axes_style()
{'axes.axisbelow': True,
'axes.edgecolor': '.15',
'axes.facecolor': 'white',
'axes.grid': False,
'axes.labelcolor': '.15',
'axes.linewidth': 1.25,
'figure.facecolor': 'white',
'font.family': ['sans-serif'],
'font.sans-serif': ['Arial',
'DejaVu Sans',
'Liberation Sans',
'Bitstream Vera Sans',
'sans-serif'],
'grid.color': '.8',
'grid.linestyle': '-',
'image.cmap': 'rocket',
'legend.frameon': False,
'legend.numpoints': 1,
'legend.scatterpoints': 1,
'lines.solid_capstyle': 'round',
'text.color': '.15',
'xtick.color': '.15',
'xtick.direction': 'out',
'xtick.major.size': 6.0,
'xtick.minor.size': 3.0,
'ytick.color': '.15',
'ytick.direction': 'out',
'ytick.major.size': 6.0,
'ytick.minor.size': 3.0}
# 设为网格线颜色
sns.set_style(style[3], {'grid.color':'red'})
# 清空设置
sns.set()
# 设置图形比例
context = ['paper', 'notebook','talk', 'poster']
sns.set_context(context[3])
sinplot()
sns.plotting_context()
{'axes.labelsize': 17.6,
'axes.titlesize': 19.200000000000003,
'font.size': 19.200000000000003,
'grid.linewidth': 1.6,
'legend.fontsize': 16.0,
'lines.linewidth': 2.8000000000000003,
'lines.markeredgewidth': 0.0,
'lines.markersize': 11.200000000000001,
'patch.linewidth': 0.48,
'xtick.labelsize': 16.0,
'xtick.major.pad': 11.200000000000001,
'xtick.major.width': 1.6,
'xtick.minor.width': 0.8,
'ytick.labelsize': 16.0,
'ytick.major.pad': 11.200000000000001,
'ytick.major.width': 1.6,
'ytick.minor.width': 0.8}
# 设置网格线宽度
sns.set_context(context[1], rc={'grid.linewidth':3.0})
sinplot()
Seaborn调色功能
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
# 绘图,figsize设置大小
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()
sinplot()
# 导入seaborn
import seaborn as sns
sns.set()
sinplot()
# 当前画板
sns.color_palette() #RGB列表元组
[(0.29803921568627451, 0.44705882352941179, 0.69019607843137254),
(0.33333333333333331, 0.6588235294117647, 0.40784313725490196),
(0.7686274509803922, 0.30588235294117649, 0.32156862745098042),
(0.50588235294117645, 0.44705882352941179, 0.69803921568627447),
(0.80000000000000004, 0.72549019607843135, 0.45490196078431372),
(0.39215686274509803, 0.70980392156862748, 0.80392156862745101)]
# 显示画板
sns.palplot(sns.color_palette())
# 自带风格
pal_style = ['deep', 'muted', 'pastel', 'bright', 'dark','colorblind']
sns.palplot(sns.color_palette('dark'))
# 设置画板
sns.set_palette(sns.color_palette('dark'))
sinplot()
# 清空画板
sns.set()
sinplot()
# 局部使用画板with语句,外部还是系统画板
with sns.color_palette('dark'):
sinplot()
# 外部没变
sinplot()
# 自定义画板
pall = sns.color_palette([(0.1,0.2,0.3),(0.9,0.3,0.6),(0.3,0.3,0.3)])
sns.palplot(pall)
# 自动生成画板
sc = sns.color_palette('hls', 10)
sns.palplot(sc)
sns.set_palette(sc)
sinplot()
·END·
点击阅读原文,进入新型农业经营主体大数据库
往期推荐
数据Seminar
这里是大数据、分析技术与学术研究的三叉路口
出处:CSDN作者:向前走别回头推荐:青酱排版编辑:青酱
欢迎扫描👇二维码添加关注
点击阅读原文,获得更多精彩内容!