数据可视化之Matplotlib绘图基础二

解决绘图中乱码问题

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’}
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();   # 调整间距以防止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);

在这里插入图片描述

参考文档

数据可视化之matplotlib绘图基础一
Matplotlib中文文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值