赞
踩
大家好,本文将围绕python数据可视化之美:专业图表绘制指南(全彩)展开说明,python可视化图做好了怎么弄下来是一个很多人都想弄明白的事情,想搞清楚python可视化图表和excel的区别需要先了解以下几个事情。
###序言: Python的可视化工具,以下截图,均以展示图表实例,如需了解部分对象的输出结果,可参照我Github上的代码,3Q?
####【课程3.6】 基本图表绘制 plt.plot() 图表类别:线形图、柱状图、密度图,以横纵坐标两个维度为主 同时可延展出多种其他图表样式
plt.plot(kind='line', ax=None, figsize=None, use_index=True, title=None, grid=None, legend=False, 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, label=None, secondary_y=False, **kwds)
#####Series直接生成图表
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
-
- # Series直接生成图表
-
- ts = pd.Series(np.random.randn(1000), index = pd.date_range("1/1/2000", periods = 1000))
- ts = ts.cumsum()
- ts.plot(kind = "line",
- label = "heheheh", # Series需要Lable
- style = "--g.",
- color = "red",
- alpha = 0.4,
- use_index = True,
- rot = 45,
- grid = True,
- ylim = [-50, 50],
- yticks = list(range(-50, 50, 10)),
- figsize = (8,4),
- title = "test",
- legend = True)
- # plt.grid(True, linestyle = "--",color = "gray", linewidth = "0.5",axis = 'x') # 网格
- plt.legend()
- # Series.plot():series的index为横坐标,value为纵坐标
- # kind → line,bar,barh...(折线图,柱状图,柱状图-横...)
- # label → 图例标签,Dataframe格式以列名为label
- # style → 风格字符串,这里包括了linestyle(-),marker(.),color(g)
- # color → 颜色,有color指定时候,以color颜色为准
- # alpha → 透明度,0-1
- # use_index → 将索引用为刻度标签,默认为True
- # rot → 旋转刻度标签,0-360
- # grid → 显示网格,一般直接用plt.grid
- # xlim,ylim → x,y轴界限
- # xticks,yticks → x,y轴刻度值
- # figsize → 图像大小
- # title → 图名
- # legend → 是否显示图例,一般直接用plt.legend()
- # 也可以 → plt.plot()
- 复制代码
df = pd.DataFrame(np.random.randn(1000, 4), index = ts.index, columns = list("ABCD")) df = df.cumsum() df.plot(kind = "line", style = "--.", alpha = 0.4, use_index = True, rot = 45, grid = True, figsize = (8,4), title = "test", legend = True, subplots = False, colormap = "Greens")
- ![2.png](https://upload-images.jianshu.io/upload_images/2330091-aaa674c6f0e16b8f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- <br>
-
- ####【课程3.7】 柱状图、堆叠图
-     plt.plot(kind='bar/barh') , plt.bar()
-
- >##### 柱状图与堆叠图
- 复制代码
fig, axes = plt.subplots(4,1,figsize = (10,10)) s = pd.Series(np.random.randint(0,10,16), index = list("abcdefghijklmnop")) df = pd.DataFrame(np.random.rand(10,3), columns = ["a", "b", "c"])
s.plot(kind = "bar", color = "k", grid = True, alpha = 0.5, ax = axes[0]) # ax参数 -> 选择第几个子图
df.plot(kind = "bar", ax = axes[1], grid = True, colormap = "Reds_r")
df.plot(kind = "bar", ax = axes[2], grid = True, colormap = "Blues_r", stacked = True)
df.plot.barh(ax = axes[3], grid = True, stacked = True, colormap = "BuGn_r")
- ![3.png](https://upload-images.jianshu.io/upload_images/2330091-6246e2a30a134871.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
- <br>
- >##### 柱状图 plt.bar()
- 复制代码
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, facecolor = "yellowgreen", edgecolor = "white", yerr = y10.1) plt.bar(x, y2, width = 1, facecolor = "lightskyblue", edgecolor = "white", yerr = y20.1)
for i,j in zip(x,y1): plt.text(i+0.3, j-0.15, "%.2f" %j, color = "k") for i,j in zip(x,y2): plt.text(i+0.3, j+0.05, "%.2f" %-j, color = "k")
- ![4.png](https://upload-images.jianshu.io/upload_images/2330091-6698eba5c413dd16.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
- >##### 外嵌图表plt.table()
- 复制代码
data = [[ 66386, 174296, 75131, 577908, 32015], [ 58230, 381139, 78045, 99308, 160454], [ 89135, 80552, 152558, 497981, 603535], [ 78415, 81858, 150656, 193263, 69638], [139361, 331509, 343164, 781380, 52269]] columns = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail') rows = ['%d year' % x for x in (100, 50, 20, 10, 5)] df = pd.DataFrame(data,columns = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail'), index = ['%d year' % x for x in (100, 50, 20, 10, 5)]) print(df)
df.plot(kind='bar',grid = True,colormap='Blues_r',stacked=True,figsize=(8,3))
plt.table(cellText = data, cellLoc='center', cellColours = None, rowLabels = rows, rowColours = plt.cm.BuPu(np.linspace(0, 0.5,5))[::-1], # BuPu可替换成其他colormap colLabels = columns, colColours = plt.cm.Reds(np.linspace(0, 0.5,5))[::-1], rowLoc='right', loc='bottom')
plt.xticks([])
-
- ![5.png](https://upload-images.jianshu.io/upload_images/2330091-edf136a5ca2eaaac.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
- <br>
-
- ####【课程3.8】 面积图、填图、饼图
-     plt.plot.area()
-     plt.fill(), plt.fill_between()
-     plt.pie()
- >#####面积图
- 复制代码
fig, axes = plt.subplots(2, 1, figsize = (8,6)) df1 = pd.DataFrame(np.random.rand(10, 4), columns = ["a", "b", "c", "d"]) df2 = pd.DataFrame(np.random.randn(10, 4), columns = ["a", "b", "c", "d"])
df1.plot.area(colormap = "Greens_r", alpha = 0.5, ax = axes[0]) df2.plot.area(stacked = False, colormap = "Set2", alpha = 0.5, ax = axes[1])
- ![6.png](https://upload-images.jianshu.io/upload_images/2330091-1c9713e5bd3b2c70.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
- <br>
- >#####填图
- 复制代码
fig, axes = plt.subplots(2, 1, figsize = (8,6))
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) axes[0].fill(x, y1, "r", alpha = 0.5, label = "y1") axes[0].fill(x, y2, "g", alpha = 0.5, label = "y2")
x = np.linspace(0, 5 * np.pi, 1000) y1 = np.sin(x) y2 = np.sin(2 * x) axes[1].fill_between(x, y1, y2, color = "b", alpha = 0.5, label = "area")
for i in range(2): axes[i].legend() axes[i].grid()
- ![7.png](https://upload-images.jianshu.io/upload_images/2330091-ff818d5d83205389.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
-
- <br>
- >##### 饼图 plt.pie()
- 复制代码
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 = ".2f%%", pctdistance = 0.6, labeldistance = 1.2, shadow = True, startangle = 0, radius = 1.5, frame = False) print(s)
- ![8.png](https://upload-images.jianshu.io/upload_images/2330091-3535ef5b8ad5ea2f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
-
- <br>
-
- ####【课程3.9】 直方图
-     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", align = "mid", orientation = "vertical", alpha = 0.5, normed = True)
s.plot(kind = "kde", style = "k--")
- ![9.png](https://upload-images.jianshu.io/upload_images/2330091-8bfe4378a095d1b0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
-
-
- <br>
- >##### 堆叠直方图
- 复制代码
plt.figure(num=1) 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, grid = True)
df.hist(bins = 50)
- ![10.png](https://upload-images.jianshu.io/upload_images/2330091-1d112dffaeec039e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
- <br>
-
- ####【课程3.10】 散点图、矩阵散点图
-     plt.scatter(), pd.scatter_matrix()
- >##### plt.scatter()散点图
- 复制代码
plt.figure(figsize = (8,6)) x = np.random.randn(1000) y = np.random.randn(1000) plt.scatter(x, y, marker = ".", s = np.random.randn(1000)*100, cmap = "Reds", c = y, alpha = 0.8) plt.grid()
- ![11.png](https://upload-images.jianshu.io/upload_images/2330091-ed03adea5caa2bab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
-
-
- <br>
- >##### pd.scatter_matrix()散点矩阵
- 复制代码
df = pd.DataFrame(np.random.randn(100, 4), columns = ["a", "b", "c", "d"]) pd.scatter_matrix(df, figsize = (10,6), marker = "o", diagonal = "kde", alpha = 0.5, range_padding = 0.1)
- ![12.png](https://upload-images.jianshu.io/upload_images/2330091-bb622af1255631e9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
- <br>
-
- ####【课程3.11】 极坐标图
-     调用subplot()创建子图时通过设置projection='polar',便可创建一个极坐标子图,然后调用plot()在极坐标子图中绘图
- >##### 创建极坐标轴
- 复制代码
s = pd.Series(np.arange(20)) theta = np.arange(0, 2*np.pi, 0.02) print(s.head()) print(theta[:10])
fig = plt.figure(figsize = (8,4)) ax1 = plt.subplot(121, projection = "polar") ax2 = plt.subplot(122)
ax1.plot(theta, theta3, linestyle = "--", lw = 1) ax1.plot(s, linestyle = "--", marker = ".", lw = 2) ax2.plot(theta, theta3, linestyle = "--", lw = 1) ax2.plot(s) plt.grid()
- ![13.png](https://upload-images.jianshu.io/upload_images/2330091-6bae534164a5c889.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
-
-
-
- <br>
- >##### 极坐标参数设置
- 复制代码
theta = np.arange(0, 2*np.pi, 0.02) plt.figure(figsize = (8,4)) ax1 = plt.subplot(121, projection = "polar") ax2 = plt.subplot(122, projection = "polar") ax1.plot(theta, theta/6, "--", lw = 2) ax2.plot(theta, theta/6, "--", lw = 2)
ax2.set_theta_direction(-1)
ax2.set_thetagrids(np.arange(0.0, 360.0, 90), ["a", "b","c", "d"]) ax2.set_rgrids(np.arange(0.2, 2, 0.4))
ax2.set_theta_offset(np.pi/2)
ax2.set_rlim(0.2, 1.2) ax2.set_rmax(2) ax2.set_rticks(np.arange(0.1, 1.5, 0.2))
- ![14.png](https://upload-images.jianshu.io/upload_images/2330091-cba0c0dfe3f5b89d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
-
- <br>
-
- >##### 雷达图1 - 极坐标的折线图/填图 - plt.plot()
- 复制代码
plt.figure(figsize = (8,4))
ax1 = plt.subplot(111, projection = "polar") ax1.set_title("radar map\n") # 创建标题 ax1.set_rlim(0, 12)
data1 = np.random.randint(1, 10, 10) data2 = np.random.randint(1, 10, 10) data3 = np.random.randint(1, 10, 10) theta = np.arange(0, 2np.pi, 2np.pi/10)
ax1.plot(theta, data1, ".--", label = "data1") ax1.fill(theta, data1, alpha = 0.2) ax1.plot(theta, data2, ".--", label = "data2") ax1.fill(theta, data2, alpha = 0.2) ax1.plot(theta, data3, ".--", label = "data3") ax1.fill(theta, data3, alpha = 0.2)
- ![15.png](https://upload-images.jianshu.io/upload_images/2330091-7e0e8309cc23c513.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
- <br>
-
- >##### 雷达图2 - 极坐标的折线图/填图 - plt.polar()
- 复制代码
labels = np.array(["a", "b", "c", "d", "e", "f"]) # 标签 dataLenth = 6 # 数据长度 data1 = np.random.randint(0, 10, 6) data2 = np.random.randint(0, 10, 6) # 数据
angles = np.linspace(0, 2*np.pi, dataLenth, endpoint = False) # 分割圆周长 data1 = np.concatenate((data1, [data1[0]])) # 闭合 data2 = np.concatenate((data2, [data2[0]])) # 闭合 angles = np.concatenate((angles, [angles[0]])) # 闭合
plt.polar(angles, data1, "o-", linewidth = 1) # 做极坐标系 plt.fill(angles, data1, alpha = 0.25) # 填充 plt.polar(angles, data2, "o-", linewidth = 1) # 做极坐标系 plt.fill(angles, data2, alpha = 0.25) # 填充
plt.thetagrids(angles * 180/np.pi, labels) # 设置网格、标签 plt.ylim(0, 10) # polar的极值设置为ylim
- ![16.png](https://upload-images.jianshu.io/upload_images/2330091-10047c22bdc9648f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- <br>
-
- >##### 极轴图 - 极坐标的柱状图
- 复制代码
plt.figure(figsize = (8,4))
ax1 = plt.subplot(111, projection = "polar") ax1.set_title("radar map\n") # 创建标题 ax1.set_rlim(0, 12)
data = np.random.randint(1, 10, 10) theta = np.arange(0, 2np.pi, 2np.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.)) # 设置颜色 plt.thetagrids(np.arange(0.0, 360.0, 90), []) # 设置网格、标签(这里是空标签,则不显示内容)
-
- ![17.png](https://upload-images.jianshu.io/upload_images/2330091-26f1fbb919e53285.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
- <br>
-
-
- ####【课程3.12】 箱型图
-     箱型图:又称为盒须图、盒式图、盒状图或箱线图,是一种用作显示一组数据分散情况资料的统计图
- 包含一组数据的:最大值、最小值、中位数、上四分位数(Q3)、下四分位数(Q1)、异常值
- ① 中位数 → 一组数据平均分成两份,中间的数
- ② 上四分位数Q1 → 是将序列平均分成四份,计算(n+1)/4与(n-1)/4两种,一般使用(n+1)/4
- ③ 下四分位数Q3 → 是将序列平均分成四份,计算(1+n)/4*3=6.75
- ④ 内限 → T形的盒须就是内限,最大值区间Q3+1.5IQR,最小值区间Q1-1.5IQR (IQR=Q3-Q1)
- ⑤ 外限 → T形的盒须就是内限,最大值区间Q3+3IQR,最小值区间Q1-3IQR (IQR=Q3-Q1)
- ⑥ 异常值 → 内限之外 - 中度异常,外限之外 - 极度异常
-
- plt.plot.box(),plt.boxplot()
- >##### plt.plot.box()绘制
- 复制代码
fig, axes = plt.subplots(2, 1, figsize = (10,6)) df = pd.DataFrame(np.random.rand(10, 5), columns = ["A", "B", "C", "D", "E"]) color = dict(boxes = "DarkGreen", whiskers = "DarkOrange", medians = "DarkBlue", caps = "Gray")
df.plot.box(ylim = [0, 1.2], grid = True, color = color, ax = axes[0])
df.plot.box(vert = False, positions = [1, 4, 5, 6, 8], ax = axes[1], grid = True, color = color)
- ![18.png](https://upload-images.jianshu.io/upload_images/2330091-cd2b1e5a064efc2c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
-
-
-
- <br>
- >##### plt.boxplot()绘制
- 复制代码
df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E']) plt.figure(figsize=(10,4))
f = df.boxplot(sym = "o", # 异常点形状,参考marker vert = True, # 是否垂直 whis = 1.5, # IQR,默认1.5, 也可以设置区间比如[5, 95], 代表强制上下边缘为数据95%和5%位置 patch_artist = True, # 上下四分位框内是否填充,True会填充 meanline = False, showmeans = True, # 是否有均值线及其形状 showbox = True, # 是否显示箱线 showcaps = True, # 是否显示边缘线 showfliers = True, # 是否显示异常值 notch = False, # 中间箱体是否缺口 return_type = 'dict' # 返回类型为字典 ) plt.title("boxplot") print(f)
for box in f["boxes"]: box.set(color = "b", linewidth = 1) # 箱体边框颜色 box.set(facecolor = "b", alpha = 0.5) # 箱体内部填充颜色 for whisker in f["whiskers"]: whisker.set(color = "k", linewidth = 0.5, linestyle = "-") for cap in f["caps"]: cap.set(color = "red", linewidth = 2) for median in f["medians"]: median.set(color = "DarkBlue", linewidth = 2) for flier in f["fliers"]: flier.set(marker = "o", color = "y", alpha = 0.5)
-
- ![19.png](https://upload-images.jianshu.io/upload_images/2330091-c0586120340b9bba.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
- <br>
-
- > #### plt.boxplot()绘制
- 复制代码
df = pd.DataFrame(np.random.rand(10,2), columns=['Col1', 'Col2'] ) df['X'] = pd.Series(['A','A','A','A','A','B','B','B','B','B']) df['Y'] = pd.Series(['A','B','A','B','A','B','A','B','A','B']) print(df.head()) df.boxplot(by = 'X') df.boxplot(column=['Col1','Col2'], by=['X','Y'])
- ![20.png](https://upload-images.jianshu.io/upload_images/2330091-049847edf500f2d1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- <br>
-
- #####最后:
- [以上完整代码](https://github.com/ChaoRenYuan/Python/tree/master/Python数据分析工具/图表绘制工具:Matplotlib)
- 复制代码
转载于:https://juejin.im/post/5b0e6a036fb9a009f87252ee
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。