前言

数据可视化的第三方库挺多的,这里我次要举荐三个,别离是 Pygal、Bokeh、Plotly,废话不多说,间接上~~

举荐

数据可视化的库有挺多的,这里举荐几个比拟罕用的:

  • Matplotlib
  • Pygal
  • Bokeh
  • Seaborn
  • Ggplot
  • Plotly
  • Pyechart

Pygal

pygal官网地址(http://www.pygal.org/en/stable/)

装置pygal模块

pygal模块的装置非常简单,只需输出一行pip命令即可

1 pip install pygal

装置实现:

pygal模块介绍

pygal是Python的第三方库,他的次要性能就是数据可视化,行将数字转化成图表的模式来出现,它提供的图表款式有柱状图、折线图、饼状图、雷达图......

柱状图
单列柱状图
import pygalview = pygal.Bar()#图表名view.title = '柱状图'#增加数据view.add('numbers', [0,2,4,6,8,10])#在浏览器中查看#view.render_in_browser()#保留为view.svg(也能够保留为jpg)view.render_to_file('view.svg')

效果图:

留神:svg图片用零碎自带的图片查看器关上可能会显示全彩色,能够尝试应用Google浏览器关上

多列柱状图
#增加数据view.add('numbers', [0,2,4,6,8,10])view.add('numbers_2', [0,1,3,5,7,9])

重叠柱状图
view = pygal.StackedBar()

横向柱状图
view = pygal.HorizontalStackedBar()

折线图

简略折线图
import pygalview = pygal.Line()#图表名view.title = '折线图'#增加数据view.add('numbers', [0,2,4,6,8,10])view.add('numbers_2', [0,1,3,5,7,9])#在浏览器中查看#view.render_in_browser()#保留为view.svg(也能够保留为jpg)view.render_to_file('view.svg')

效果图:

纵向折线图
view = pygal.HorizontalLine()

重叠折线图
view = pygal.StackedLine(fill=True)

饼状图

简略饼状图
import pygalview = pygal.Pie()#图表名view.title = '饼状图'#增加数据view.add('A', 31)view.add('B', 55)view.add('C', 14)#保留为view.svg(也能够保留为jpg)view.render_to_file('view.svg')

效果图:

多级饼状图
#增加数据view.add('A', [31,25])view.add('B', [55,38])view.add('C', [14,37])

圆环图
#设置空心圆半径view = pygal.Pie(inner_radius=0.4)

半圆图
view = pygal.Pie(half_pie=True)

雷达图

根底雷达图

import pygalview = pygal.Radar()#图表名view.title = '雷达图'#增加数据(能够为任意个)view.add('A', [31,56,34,67,34])view.add('B', [23,18,57,45,35])view.add('C', [14,45,76,34,76])#保留为view.svg(也能够保留为jpg)view.render_to_file('view.svg')

效果图:

plotly

plotly 文档地址(https://plot.ly/python/#finan...)

Plotly 是一款用来做数据分析和可视化的在线平台,性能十分弱小,能够在线绘制很多图形比方条形图、散点图、饼图、直方图等等。而且还是反对在线编辑,以及多种语言python、javascript、matlab、R等许多API。它在python中应用也很简略,间接用pip install plotly就能够了。举荐最好在jupyter notebook中应用,pycharm操作不是很不便。应用Plotly能够画出很多媲美Tableau的高质量图:


这里尝试做了折线图、散点图和直方图,代码如下:
首先导入库

from plotly.graph_objs import Scatter,Layoutimport plotlyimport plotly.offline as pyimport numpy as npimport plotly.graph_objs as go#setting offilneplotly.offline.init_notebook_mode(connected=True)

下面几行代码次要是援用一些库,plotly有在线和离线两种模式,在线模式须要有账号能够云编辑。我选用的离线模式,plotly设置为offline模式就能够间接在notebook外面显示了。

1.制作折线图
N = 100random_x = np.linspace(0,1,N)random_y0 = np.random.randn(N)+5random_y1 = np.random.randn(N)random_y2 = np.random.randn(N)-5#Create tracestrace0 = go.Scatter(    x = random_x,    y = random_y0,    mode = 'markers',    name = 'markers')trace1 = go.Scatter(    x = random_x,    y = random_y1,    mode = 'lines+markers',    name = 'lines+markers')trace2 = go.Scatter(    x = random_x,    y = random_y2,    mode = 'lines',    name = 'lines')data = [trace0,trace1,trace2]py.iplot(data)

随机设置4个参数,一个x轴的数字和三个y轴的随机数据,制作出三种不同类型的图。trace0是markers,trace1是lines和markers,trace3是lines。而后把三种图放在data这个列表外面,调用py.iplot(data)即可。
绘制的图片零碎默认配色也挺难看的~

2.制作散点图
trace1 = go.Scatter(     y = np.random.randn(500),    mode = 'markers',    marker = dict(        size = 16,        color = np.random.randn(500),        colorscale = 'Viridis',        showscale = True    ))data = [trace1]py.iplot(data)

把mode设置为markers就是散点图,而后marker外面设置一组参数,比方色彩的随机范畴,散点的大小,还有图例等等。

3.直方图
trace0 = go.Bar(    x = ['Jan','Feb','Mar','Apr', 'May','Jun',         'Jul','Aug','Sep','Oct','Nov','Dec'],    y = [20,14,25,16,18,22,19,15,12,16,14,17],    name = 'Primary Product',    marker=dict(        color = 'rgb(49,130,189)'    ))trace1 = go.Bar(    x = ['Jan','Feb','Mar','Apr', 'May','Jun',         'Jul','Aug','Sep','Oct','Nov','Dec'],    y = [19,14,22,14,16,19,15,14,10,12,12,16],    name = 'Secondary Product',    marker=dict(        color = 'rgb(204,204,204)'    ))data = [trace0,trace1]py.iplot(data)

Bokeh

条形图

这配色看着还挺舒服的,比 pyecharts 条形图的配色难看一点。

from bokeh.io import show, output_filefrom bokeh.models import ColumnDataSourcefrom bokeh.palettes import Spectral6from bokeh.plotting import figureoutput_file("colormapped_bars.html")#  配置输入文件名fruits = ['Apples', '魅族', 'OPPO', 'VIVO', '小米', '华为'] # 数据counts = [5, 3, 4, 2, 4, 6] # 数据source = ColumnDataSource(data=dict(fruits=fruits, counts=counts, color=Spectral6))p = figure(x_range=fruits, y_range=(0,9), plot_height=250, title="Fruit Counts",           toolbar_location=None, tools="")# 条形图配置项p.vbar(x='fruits', top='counts', width=0.9, color='color', legend="fruits", source=source)p.xgrid.grid_line_color = None # 配置网格线色彩p.legend.orientation = "horizontal" # 图表方向为程度方向p.legend.location = "top_center"show(p) # 展现图表

年度条形图

能够比照不同工夫点的量。

from bokeh.io import show, output_filefrom bokeh.models import ColumnDataSource, FactorRangefrom bokeh.plotting import figureoutput_file("bars.html") # 输入文件名fruits = ['Apple', '魅族', 'OPPO', 'VIVO', '小米', '华为'] # 参数years = ['2015', '2016', '2017'] # 参数data = {'fruits': fruits,        '2015': [2, 1, 4, 3, 2, 4],        '2016': [5, 3, 3, 2, 4, 6],        '2017': [3, 2, 4, 4, 5, 3]}x = [(fruit, year) for fruit in fruits for year in years]counts = sum(zip(data['2015'], data['2016'], data['2017']), ())  source = ColumnDataSource(data=dict(x=x, counts=counts))p = figure(x_range=FactorRange(*x), plot_height=250, title="Fruit Counts by Year",           toolbar_location=None, tools="")p.vbar(x='x', top='counts', width=0.9, source=source)p.y_range.start = 0p.x_range.range_padding = 0.1p.xaxis.major_label_orientation = 1p.xgrid.grid_line_color = Noneshow(p)

饼图

from collections import Counterfrom math import piimport pandas as pdfrom bokeh.io import output_file, showfrom bokeh.palettes import Category20cfrom bokeh.plotting import figurefrom bokeh.transform import cumsumoutput_file("pie.html")x = Counter({    '中国': 157,    '美国': 93,    '日本': 89,    '巴西': 63,    '德国': 44,    '印度': 42,    '意大利': 40,    '澳大利亚': 35,    '法国': 31,    '西班牙': 29})data = pd.DataFrame.from_dict(dict(x), orient='index').reset_index().rename(index=str, columns={0:'value', 'index':'country'})data['angle'] = data['value']/sum(x.values()) * 2*pidata['color'] = Category20c[len(x)]p = figure(plot_height=350, title="Pie Chart", toolbar_location=None,           tools="hover", tooltips="@country: @value")p.wedge(x=0, y=1, radius=0.4,        start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),        line_color="white", fill_color='color', legend='country', source=data)p.axis.axis_label=Nonep.axis.visible=Falsep.grid.grid_line_color = Noneshow(p)

条形图

from bokeh.io import output_file, showfrom bokeh.models import ColumnDataSourcefrom bokeh.palettes import GnBu3, OrRd3from bokeh.plotting import figureoutput_file("stacked_split.html")fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']years = ["2015", "2016", "2017"]exports = {'fruits': fruits,           '2015': [2, 1, 4, 3, 2, 4],           '2016': [5, 3, 4, 2, 4, 6],           '2017': [3, 2, 4, 4, 5, 3]}imports = {'fruits': fruits,           '2015': [-1, 0, -1, -3, -2, -1],           '2016': [-2, -1, -3, -1, -2, -2],           '2017': [-1, -2, -1, 0, -2, -2]}p = figure(y_range=fruits, plot_height=250, x_range=(-16, 16), title="Fruit import/export, by year",           toolbar_location=None)p.hbar_stack(years, y='fruits', height=0.9, color=GnBu3, source=ColumnDataSource(exports),             legend=["%s exports" % x for x in years])p.hbar_stack(years, y='fruits', height=0.9, color=OrRd3, source=ColumnDataSource(imports),             legend=["%s imports" % x for x in years])p.y_range.range_padding = 0.1p.ygrid.grid_line_color = Nonep.legend.location = "top_left"p.axis.minor_tick_line_color = Nonep.outline_line_color = Noneshow(p)

散点图

from bokeh.plotting import figure, output_file, showoutput_file("line.html")p = figure(plot_width=400, plot_height=400)p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=20, color="navy", alpha=0.5)show(p)

六边形图

这两天,马蜂窝刚被发现数据造假,这不,与马蜂窝应应景。

import numpy as npfrom bokeh.io import output_file, showfrom bokeh.plotting import figurefrom bokeh.util.hex import axial_to_cartesianoutput_file("hex_coords.html")q = np.array([0, 0, 0, -1, -1, 1, 1])r = np.array([0, -1, 1, 0, 1, -1, 0])p = figure(plot_width=400, plot_height=400, toolbar_location=None) # p.grid.visible = False # 配置网格是否可见p.hex_tile(q, r, size=1, fill_color=["firebrick"] * 3 + ["navy"] * 4,           line_color="white", alpha=0.5)x, y = axial_to_cartesian(q, r, 1, "pointytop")p.text(x, y, text=["(%d, %d)" % (q, r) for (q, r) in zip(q, r)],       text_baseline="middle", text_align="center")show(p)

环比条形图

这个实现挺厉害的,看了一眼就吸引了我。我在代码中都做了一些正文,心愿对你了解有帮忙。注:圆心为正地方,即直角坐标系中标签为(0,0)的中央。

from collections import OrderedDictfrom math import log, sqrtimport numpy as npimport pandas as pdfrom six.moves import cStringIO as StringIOfrom bokeh.plotting import figure, show, output_fileantibiotics = """bacteria,                        penicillin, streptomycin, neomycin, gram结核分枝杆菌,                      800,        5,            2,        negative沙门氏菌,                         10,         0.8,          0.09,     negative变形杆菌,                         3,          0.1,          0.1,      negative肺炎克雷伯氏菌,                    850,        1.2,          1,        negative布鲁氏菌,                         1,          2,            0.02,     negative铜绿假单胞菌,                     850,        2,            0.4,      negative大肠杆菌,                        100,        0.4,          0.1,      negative产气杆菌,                         870,        1,            1.6,      negative红色葡萄球菌,                     0.007,      0.1,          0.001,    positive溶血性链球菌,                     0.001,      14,           10,       positive草绿色链球菌,                     0.005,      10,           40,       positive肺炎双球菌,                       0.005,      11,           10,       positive"""drug_color = OrderedDict([# 配置两头标签名称与色彩    ("盘尼西林", "#0d3362"),    ("链霉素", "#c64737"),    ("新霉素", "black"),])gram_color = {    "positive": "#aeaeb8",    "negative": "#e69584",}# 读取数据df = pd.read_csv(StringIO(antibiotics),                 skiprows=1,                 skipinitialspace=True,                 engine='python')width = 800height = 800inner_radius = 90outer_radius = 300 - 10minr = sqrt(log(.001 * 1E4))maxr = sqrt(log(1000 * 1E4))a = (outer_radius - inner_radius) / (minr - maxr)b = inner_radius - a * maxrdef rad(mic):    return a * np.sqrt(np.log(mic * 1E4)) + bbig_angle = 2.0 * np.pi / (len(df) + 1)small_angle = big_angle / 7# 整体配置p = figure(plot_width=width, plot_height=height, title="",           x_axis_type=None, y_axis_type=None,           x_range=(-420, 420), y_range=(-420, 420),           min_border=0, outline_line_color="black",           background_fill_color="#f0e1d2")p.xgrid.grid_line_color = Nonep.ygrid.grid_line_color = None# annular wedgesangles = np.pi / 2 - big_angle / 2 - df.index.to_series() * big_angle  #计算角度colors = [gram_color[gram] for gram in df.gram] # 配置色彩p.annular_wedge(    0, 0, inner_radius, outer_radius, -big_angle + angles, angles, color=colors,)# small wedgesp.annular_wedge(0, 0, inner_radius, rad(df.penicillin),                -big_angle + angles + 5 * small_angle, -big_angle + angles + 6 * small_angle,                color=drug_color['盘尼西林'])p.annular_wedge(0, 0, inner_radius, rad(df.streptomycin),                -big_angle + angles + 3 * small_angle, -big_angle + angles + 4 * small_angle,                color=drug_color['链霉素'])p.annular_wedge(0, 0, inner_radius, rad(df.neomycin),                -big_angle + angles + 1 * small_angle, -big_angle + angles + 2 * small_angle,                color=drug_color['新霉素'])# 绘制大圆和标签labels = np.power(10.0, np.arange(-3, 4))radii = a * np.sqrt(np.log(labels * 1E4)) + bp.circle(0, 0, radius=radii, fill_color=None, line_color="white")p.text(0, radii[:-1], [str(r) for r in labels[:-1]],       text_font_size="8pt", text_align="center", text_baseline="middle")# 半径p.annular_wedge(0, 0, inner_radius - 10, outer_radius + 10,                -big_angle + angles, -big_angle + angles, color="black")# 细菌标签xr = radii[0] * np.cos(np.array(-big_angle / 2 + angles))yr = radii[0] * np.sin(np.array(-big_angle / 2 + angles))label_angle = np.array(-big_angle / 2 + angles)label_angle[label_angle < -np.pi / 2] += np.pi  # easier to read labels on the left side# 绘制各个细菌的名字p.text(xr, yr, df.bacteria, angle=label_angle,       text_font_size="9pt", text_align="center", text_baseline="middle")# 绘制圆形,其中数字别离为 x 轴与 y 轴标签p.circle([-40, -40], [-370, -390], color=list(gram_color.values()), radius=5)# 绘制文字p.text([-30, -30], [-370, -390], text=["Gram-" + gr for gr in gram_color.keys()],       text_font_size="7pt", text_align="left", text_baseline="middle")# 绘制矩形,两头标签局部。其中 -40,-40,-40 为三个矩形的 x 轴坐标。18,0,-18 为三个矩形的 y 轴坐标p.rect([-40, -40, -40], [18, 0, -18], width=30, height=13,       color=list(drug_color.values()))# 配置两头标签文字、文字大小、文字对齐形式p.text([-15, -15, -15], [18, 0, -18], text=list(drug_color),       text_font_size="9pt", text_align="left", text_baseline="middle")output_file("burtin.html", title="burtin.py example")show(p)

元素周期表

元素周期表,这个实现好牛逼啊,间隔初三刚开始学化学曾经很边远了,想当年我还是化学课代表呢!因为根本用不到化学了,这里就不实现了。

今日的分享到这里就完结了,如果对数据可视化有更好的倡议,欢送在评论区留言与我交换,最初祝大家学习提高,大礼包详见个人主页简介获取