解决绘图中乱码问题
plt
.rcParams
['font.sans-serif']=['Simhei']
plt
.rcParams
['axes.unicode_minus']=False
Matplotlib中的绘图类型
导入基本模块
import matplotlib
.pyplot
as plt
import numpy
as np
np
.random
.seed
(1026)
散点图
x
= np
.linspace
(0,2*np
.pi
,50)
y
= np
.random
.random
(50)
plt
.scatter
(x
,y
,'o');
彩色映射散点图
x
= np
.random
.randint
(1,100,100)
y
= np
.random
.rand
(100)
size
= np
.random
.rand
(100) * 500
colour
= np
.random
.rand
(100)
plt
.rc
('figure', figsize
=(10, 6))
plt
.scatter
(x
, y
,
size
,
colour
,
alpha
=.7,
linewidths
=7
)
plt
.colorbar
()
plt
.show
()
柱状图、条形图
plt.bar
(x,height,width
=0.8,bottom
=None,*,align
='center',data
=None, **kwargs
)
size
= 5
a
= np
.random
.random
(size
)
b
= np
.random
.random
(size
)
c
= np
.random
.random
(size
)
d
= np
.random
.random
(size
)
x
= np
.arange
(size
)
total_width
, n
= 0.6, 3
width
= total_width
/ n
x
= x
- (total_width
- width
) / 2
plt
.bar
(x
, a
, width
=width
, label
='a')
plt
.bar
(x
+ width
, b
, width
=width
, label
='b')
plt
.bar
(x
+ 2 * width
, c
, width
=width
, label
='c')
plt
.legend
()
plt
.show
()
堆积柱状图
N
= 5
menMeans
= (20, 35, 30, 35, 27
)
womenMeans
= (25, 32, 34, 20, 25
)
ind
= np.arange
(N
)
width
= 0.35
p1
= plt.bar
(ind, menMeans, width, color
='#d62728', label
='Men')
p2
= plt.bar
(ind, womenMeans, width, bottom
=menMeans, label
='Wowen')
plt.ylabel
('Scores')
plt.title
('Scores by group and gender')
plt.xticks
(ind,
('G1',
'G2',
'G3',
'G4',
'G5'))
plt.yticks
(np.arange
(0, 81, 10
))
plt.legend
()
plt.show
()
带误差的堆积柱状图
N
= 5
menMeans
= (20, 35, 30, 35, 27
)
womenMeans
= (25, 32, 34, 20, 25
)
menStd
= (2, 3, 4, 1, 2
)
womenStd
= (3, 5, 2, 3, 3
)
ind
= np.arange
(N
)
width
= 0.35
p1
= plt.bar
(ind, menMeans, width, color
='#d62728', yerr
=menStd
)
p2
= plt.bar
(ind, womenMeans, width, bottom
=menMeans, yerr
=womenStd
)
plt.ylabel
('Scores')
plt.title
('Scores by group and gender')
plt.xticks
(ind,
('G1',
'G2',
'G3',
'G4',
'G5'))
plt.yticks
(np.arange
(0, 81, 10
))
plt.legend
((p1
[0
], p2
[0
]),
('Men',
'Women'))
plt.show
()
直方图
plt.hist
(x, bins
=None, range
=None, normed
=False, weights
=None, cumulative
=False, bottom
=None, histtype
='bar', align
='mid', orientation
='vertical', rwidth
=None, log
=False, color
=None, label
=None, stacked
=False, hold
=None, data
=None, **kwargs
)
参数说明
参数说明
x数据bins组数histtype{‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’}align{‘left’, ‘mid’, ‘right’}orientation{‘horizontal’, ‘vertical’}rwidthbar间距
x
= np
.random
.randn
(100)
plt
.hist
(x
, 20,
orientation
='horizontal');
from scipy
.stats
import norm
import matplotlib
.pyplot
as plt
mu
= 100
sigma
= 15
x
= mu
+ sigma
* np
.random
.randn
(1000)
n
, bins
, patches
= plt
.hist
(x
, 50, normed
=1)
y
= norm
.pdf
(bins
, mu
, sigma
)
plt
.plot
(bins
, y
, '--')
plt
.xlabel
('Smarts')
plt
.ylabel
('Probability density')
plt
.title
(r
'Histogram of IQ: $\mu=100$, $\sigma=15$')
plt
.tight_layout
();
plt.tight_layout
(pad
=1.08, h_pad
=None, w_pad
=None, rect
=None
)
问题注解:If you read documentation of matplotlib.mlab.normpdf, this function is deprycated and you should use scipy.stats.norm.pdf instead.
饼图
labels
= 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes
= [15, 30, 45, 10]
explode
= (0, 0.1, 0, 0)
fig1
, ax1
= plt
.subplots
()
ax1
.pie
(sizes
, explode
=explode
, labels
=labels
, autopct
='%1.1f%%',
shadow
=True,
startangle
=90)
ax1
.axis
('equal');
箱线图、小提琴图
fig
, axes
= plt
.subplots
(nrows
=1, ncols
=2, figsize
=(14, 4))
axes
[0].violinplot
(data
,
showmedians
=True
)
axes
[0].set_title
('Violin plot')
axes
[1].boxplot
(data
, sym
='r', meanline
=True, showmeans
=False)
axes
[1].set_title
('Box plot')
for ax
in axes
:
ax
.yaxis
.grid
(True)
ax
.set_ylim
(0,300000)
ax
.set_xlabel
('不同群组')
ax
.set_ylabel
('观测')
ax
.set_xticks
([y
+ 1 for y
in range(len(data
))])
ax
.set_xticklabels
(['Assets', 'Revenue', 'Spend', 'Margin', 'Pay'])
添加文本、箭头、注解等
x
= np
.linspace
(0, 10, 1000)
y
= np
.sin
(x
)
plt
.plot
(x
,y
,label
="$sin(x)$",color
="red",linewidth
=3)
plt
.text
(2,0,'文本在(2,0)这个点', family
='Simhei', fontsize
=10)
plt
.arrow
(4, -0.5, 0.5, 0.5)
plt
.annotate
("我是注解", xy
=(0.5, 0.5), xytext
=(1, -0.25),arrowprops
=dict(arrowstyle
="->"))
plt
.show
()
定制化图表样式
plt.style.available
['bmh',
'classic',
'dark_background',
'fast',
'fivethirtyeight',
'ggplot',
'grayscale',
'seaborn-bright',
'seaborn-colorblind',
'seaborn-dark-palette',
'seaborn-dark',
'seaborn-darkgrid',
'seaborn-deep',
'seaborn-muted',
'seaborn-notebook',
'seaborn-paper',
'seaborn-pastel',
'seaborn-poster',
'seaborn-talk',
'seaborn-ticks',
'seaborn-white',
'seaborn-whitegrid',
'seaborn',
'Solarize_Light2',
'tableau-colorblind10',
'_classic_test']
定制化图表样式
plt
.style
.use
('classic')
定制化临时图表样式
with plt
.style
.context
(('seaborn-ticks')):
plt
.plot
(np
.sin
(np
.linspace
(0, 2 * np
.pi)
热力图
import numpy
as np
from pandas
import DataFrame
import matplotlib
.pyplot
as plt
Index
= ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
Cols
= ['A', 'B', 'C', 'D']
df
= DataFrame
(abs(np
.random
.randn
(5, 4)), index
=Index
, columns
=Cols
)
plt
.pcolor
(df
)
plt
.yticks
(np
.arange
(0.5, len(df
.index
), 1), df
.index
)
plt
.xticks
(np
.arange
(0.5, len(df
.columns
), 1), df
.columns
)
plt
.show
()
Seaborn中一行命令即可画出精美的图片
import seaborn
as sns
sns
.heatmap
(df
, annot
=True);
参考文档
数据可视化之matplotlib绘图基础一 Matplotlib中文文档