import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
% matplotlib inline
DataFrame.plot(x=None, y=None, kind=‘line’, ax=None, subplots=False, sharex=None, sharey=False, layout=None, figsize=None, use_index=True, title=None, grid=None, legend=True, style=None, logx=False, logy=False, loglog=False, xticks=None, yticks=None, xlim=None, ylim=None, rot=None, fontsize=None, colormap=None, table=False, yerr=None, xerr=None, secondary_y=False, sort_columns=False, **kwds)
以下属性对DataFrame有效
Series.plot():默认index为横坐标,value为纵坐标
ts = pd.Series(np.random.randn(100), index = pd.date_range("2021-01-01", periods = 100))
ts = ts.cumsum()
# 也可以用plt.plot(ts)
ts.plot(kind = "line",
figsize = (6, 4),
title = "title",
legend = True, label = "label",
ylim = [-20, 20], yticks = list(range(-20, 20, 5)),
grid = True,
style = "--g", color = "red", alpha = 0.8,
rot = 45,
use_index = True)
df = pd.DataFrame(np.random.randn(100, 4),
columns = list('ABCD'),
index = pd.date_range("2021-01-01", periods = 100))
df = df.cumsum()
# 也可以用plt.plot(df)
df.plot(kind = "line",
figsize = (6, 4),
title = "title",
legend = True,
ylim = [-20, 20], yticks = list(range(-20, 20, 10)),
grid = False,
style = "--g", colormap = "cool", alpha = 0.8,
rot = 45,
use_index = True,
subplots = True)
新版本plt.plot.bar()和plt.plot.barh()
s = pd.Series(np.random.randint(0, 10, 7), index = list('abcdefg'))
df = pd.DataFrame(np.random.rand(10, 3), columns=['a', 'b', 'c'])
单系列柱状图:
s.plot(kind = "bar", figsize = (4, 2), color = "k", alpha = 0.5)
s.plot(kind = "barh", figsize = (4, 2), color = "k", alpha = 0.5)
df.plot(kind = "bar", figsize = (6, 3), colormap = 'Reds_r')
df.plot(kind = "bar", stacked = True, figsize = (6, 3), colormap = 'Reds_r')
plt.bar(x, height, width=0.8, bottom=None, *, align=‘center’, data=None, **kwargs)
plt.barh(y, width, height=0.8, left=None, *, align=‘center’, **kwargs)
对于barh,width和height互换,bottom改为left,yerr改为xerr
添加注解:plt.text()
zip()函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
plt.figure(figsize=(10,4))
x = np.arange(10)
y1 = np.random.rand(10)
y2 = -np.random.rand(10)
plt.bar(x, y1, width = 1, align = "center", facecolor = "yellowgreen", edgecolor = "white", yerr = y1 * 0.1)
plt.bar(x, y2, width = 1, facecolor = "lightskyblue", edgecolor = "white")
for i, j in zip(x, y1):
plt.text(i - 0.25, j - 0.15, '%0.2f' % j, color = "white")
stacked为True时,为了产生堆积面积图,每列必须全部是正值或负值,否则会报错
当有数据为NaN时,自动填充0,所以需清洗缺失值
df = pd.DataFrame(np.random.rand(10, 4), columns = list('abcd'))
df.plot.area(colormap = "Greens_r", alpha = 0.5)
plt.fill():对函数与坐标轴之间的区域进行填充
plt.fill_between():对两个函数之间的区域进行填充
x = np.linspace(0, 1, 500)
y1 = np.sin(4 * np.pi * x) * np.exp(-5 * x)
y2 = -np.sin(4 * np.pi * x) * np.exp(-5 * x)
plt.fill(x, y1, 'r', x, y2, 'g', alpha = 0.5)
'''
plt.fill(x, y1, 'r', alpha = 0.5, label = 'y1')
plt.fill(x, y2, 'g', alpha = 0.5, label = 'y2')
plt.legend()
'''
x = np.linspace(0, 5 * np.pi, 1000)
y1 = np.sin(x)
y2 = 0.5
plt.fill_between(x, y1, y2, color = "b",alpha = 0.5)
plt.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None,
radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, hold=None, data=None)
s = pd.Series(3 * np.random.rand(4), index = ['a', 'b', 'c', 'd'], name = 'series')
plt.axis('equal') # 设置图表长宽相等,否则可能不是正圆
plt.pie(s,
explode = [0.1, 0, 0, 0],
labels = s.index,
colors=["r", "g", "b", "c"],
autopct = "%0.2f%%",
pctdistance = 0.8,
shadow = True,
labeldistance = 1.2,
startangle = 45,
radius = 0.8,
counterclock = False,
frame = False)
plt.hist(x, bins=10, 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)
s = pd.Series(np.random.randn(1000))
s.hist(bins = 20,
histtype = 'bar',
orientation = "vertical",
alpha = 0.5)
# 直方图归一化后,加上密度图
s.hist(bins = 20,
histtype = 'bar',
orientation = "vertical",
alpha = 0.5,
normed = True)
s.plot(kind = 'kde', style = 'k--')
df = pd.DataFrame({'a': np.random.randn(1000) + 1, 'b': np.random.randn(1000),
'c': np.random.randn(1000) - 1, 'd': np.random.randn(1000) - 2},
columns = ['a', 'b', 'c', 'd'])
df.plot.hist(stacked = True,
bins = 20,
colormap='Greens_r',
alpha=0.5)
df.hist(bins = 50) # 生成多个直方图
plt.scatter(x, y, s=20, c=None, marker=‘o’, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, hold=None, data=None, **kwargs)
其中s和c可以更加多维地展示结果
x = np.random.randn(1000)
y = np.random.randn(1000)
plt.scatter(x, y,
s = np.random.randn(1000) * 100,
c = y,
marker = '.',
cmap = 'Reds',
alpha = 0.8)
比较列与列之间的相关性,对角线为每个列的数据情况
pd.scatter_matrix(frame, alpha=0.5, figsize=None, ax=None, grid=False, diagonal=‘hist’, marker=’.’, density_kwds=None, hist_kwds=None, range_padding=0.05, **kwds)
df = pd.DataFrame(np.random.randn(100, 3), columns = ['a', 'b', 'c'])
pd.scatter_matrix(df,
alpha = 0.5,
figsize = (5, 3),
diagonal = 'kde',
marker = 'o',
range_padding = 0.2)
s = pd.Series(np.arange(20))
theta = np.arange(0, 2 * np.pi, 0.02)
plt.polar(theta, r, **kwargs),theta为弧度制角度,支持多个theta、r参数
plt.polar(s)
plt.polar(theta, theta * 3)
plt.thetagrids(np.arange(0.0, 360.0, 90), ['0', '90', 'pi', '270']) # 设置网格、标签
plt.ylim(5, 15)
fig = plt.figure(figsize = (8, 4))
ax1 = plt.subplot(121, projection = "polar") # 创建极坐标子图
# 也可以 ax1 = fig.add_subplot(121, polar = True)
ax2 = plt.subplot(122)
ax1.plot(s)
ax1.plot(theta, theta * 3)
ax2.plot(s)
ax2.plot(theta, theta * 3)
# 设置坐标轴方向,默认逆时针1,可设置为顺时针-1
ax1.set_theta_direction(-1)
# 设置角度偏移,参数为弧度,正数代表逆时针偏移
ax1.set_theta_offset(np.pi / 2)
# 设置极坐标角度网格线显示及标签,网格线和标签一一对应
ax1.set_thetagrids(np.arange(0.0, 360.0, 90), ['0', '90', 'pi', '270'])
# 设置极径网格线显示,参数必须是正数,不能是0
ax1.set_rgrids(np.arange(1, 10, 2))
# 左图
ax2.set_rlim(5, 15) # 设置显示的极径范围
# 相当于
# ax1.set_rmin(5) # 设置显示的极径最小值
# ax1.set_rmax(15) # 设置显示的极径最大值
# 右图,在左图基础上添加ax2.set_rlim(5, 15)截取极径范围
# 设置极径网格线的显示范围,与如上两个方法有重复
# ax1.set_rticks(np.arange(1, 10, 3))
fig = plt.figure(figsize = (8, 4))
ax1 = plt.subplot(121, projection = "polar")
ax2 = plt.subplot(122, projection = "polar")
data = np.random.randint(1,10,10)
theta = np.arange(0, 2 * np.pi, 2 * np.pi / 10)
# 首尾未闭合
ax1.plot(theta, data, '.--')
ax1.fill(theta, data, alpha = 0.2)
# 首尾闭合
data = np.concatenate((data, [data[0]])) # 闭合
theta = np.concatenate((theta, [theta[0]])) # 闭合
ax2.plot(theta, data, '.--')
ax2.fill(theta, data, alpha = 0.2)
data = np.random.randint(1,10,10)
theta = np.arange(0, 2 * np.pi, 2 * np.pi / 10)
bar = ax1.bar(theta, data, alpha = 0.5)
for r,bar in zip(data, bar):
bar.set_facecolor(plt.cm.jet(r/10.)) # 设置颜色