解决绘图中乱码问题
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)
# y = np.sin(x)
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,
# marker=r'$\clubsuit$'
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 # 有多少个类型,只需更改n即可
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((p1[0], p2[0]), ('Men', 'Women'))
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.legend()
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’} |
rwidth | bar间距 |
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(); # 调整间距以防止ylabel被覆盖
plt.tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None)
# 参数
# pad:子图边框和figure边框的距离,用空白填补
# h_pad、w_pad:子图的纵横边框之间的距离,用空白填补
问题注解: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) # 突出'Hogs'
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,
# vert=False,
# showmeans=True,
showmedians=True
)
axes[0].set_title('Violin plot')
axes[1].boxplot(data, sym='r', meanline=True, showmeans=False)
axes[1].set_title('Box plot')
# adding horizontal grid lines
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'])
# add x-tick labels 换做一种方式设置标签
# plt.setp(axes, xticks=[y + 1 for y in range(len(data))],
# 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) # 4, -0.5, 4.5, 0
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);