preface

There are many third-party libraries for data visualization. Here I recommend three: Pygal, Bokeh, Plotly

recommended

There are many data visualization libraries. Here are a few commonly used ones:

  • Matplotlib

  • Pygal

  • Bokeh

  • Seaborn

  • Ggplot

  • Plotly

  • Pyechart

Pygal

Pygal website (www.pygal.org/en/stable/)

Install the PyGal module

Installation of the Pygal module is as simple as typing a single PIP command

1 pip install pygal
Copy the code

Installation completed:

Introduction to the PyGal module

Pygal is a third-party library for Python. Its main function is data visualization, which is to convert numbers into charts to present. It provides chart styles such as bar chart, line chart, pie chart, radar chart……

A histogram
Single column bar chart
import pygal

view = pygal.Bar()
# chart of
view.title = 'Bar chart'
# Add data
view.add('numbers'[0.2.4.6.8.10])
# View in the browser
#view.render_in_browser()
# Save as view.svg(or JPG)
view.render_to_file('view.svg')
Copy the code

Effect:Note: SVG images may appear in full black when opened in the system’s built-in image viewer. Try opening them using Google’s browser

Multi-column bar chart
# Add data
view.add('numbers'[0.2.4.6.8.10])
view.add('numbers_2'[0.1.3.5.7.9])
Copy the code

Stacked bar chart
view = pygal.StackedBar()
Copy the code

Horizontal bar chart
view = pygal.HorizontalStackedBar()
Copy the code

The line chart

Simple line chart
import pygal

view = pygal.Line()
# chart of
view.title = 'Line chart'
# Add data
view.add('numbers'[0.2.4.6.8.10])
view.add('numbers_2'[0.1.3.5.7.9])
# View in the browser
#view.render_in_browser()
# Save as view.svg(or JPG)
view.render_to_file('view.svg')
Copy the code

Effect:

Longitudinal broken line chart
view = pygal.HorizontalLine()
Copy the code

Stack line chart
view = pygal.StackedLine(fill=True)
Copy the code

The pie chart

Simple pie chart
import pygal

view = pygal.Pie()
# chart of
view.title = 'Pie chart'
# Add data
view.add('A'.31)
view.add('B'.55)
view.add('C'.14)
# Save as view.svg(or JPG)
view.render_to_file('view.svg')
Copy the code

Effect:

Multistage pie chart
# Add data
view.add('A'[31.25])
view.add('B'[55.38])
view.add('C'[14.37])
Copy the code

Ring charts
View = pygal.Pie(inner_radius=0.4)Copy the code

Round figure
view = pygal.Pie(half_pie=True)
Copy the code

Radar map

Basic radar map

import pygal

view = pygal.Radar()
# chart of
view.title = 'Radar chart'
# Add data (can be any)
view.add('A'[31.56.34.67.34])
view.add('B'[23.18.57.45.35])
view.add('C'[14.45.76.34.76])
# Save as view.svg(or JPG)
view.render_to_file('view.svg')
Copy the code

Effect:

plotly

Plot. ly/python/#fin…

Plotly is a powerful online data analysis and visualization platform that allows you to plot bar, scatter, pie, and histogram charts. It also supports online editing, and many apis in python, javascript, MATLAB, R, and many other languages. It’s also easy to use in Python, just use PIP install plotly. Best recommended to use in the Jupyter Notebook, the Pycharm operation is not very convenient. Using Plotly, you can draw a lot of high-quality graphics that match Tableau’s:

Here is an attempt to do line chart, scatter diagram and histogram, the code is as follows: first guide to the library

from plotly.graph_objs import Scatter,Layout
import plotly
import plotly.offline as py
import numpy as np
import plotly.graph_objs as go

#setting offilne
plotly.offline.init_notebook_mode(connected=True)
Copy the code

The above lines of code refer to libraries. Plotly is available in both online and offline modes. In online mode, you need an account to edit it in the cloud. I chose offline mode, and if plotly is set to offline mode, it can be displayed directly in the Notebook.

1. Make line charts
N = 100
random_x = np.linspace(0.1,N)
random_y0 = np.random.randn(N)+5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N)-5

#Create traces
trace0 = 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)

Copy the code

Randomly set four parameters, one X-axis number and three Y-axis random data, to produce three different types of graphs. Trace0 is marked marked, trace1 is marked marked lines and markers,trace3 is marked marked lines. Then put the three graphs in the data list and call py.iplot(data). The default color scheme of the drawing picture system is also very nice ~

2. Make a scatter diagram
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)
Copy the code

Set mode to markers, which is the scatter map, and then set a set of parameters in marker, such as the random range of colors, the size of the scatter, the legend, and so on.

3. The histogram
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 (49130189).
    )
)
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 (204204204).
    )
)
data = [trace0,trace1]
py.iplot(data)
Copy the code

Bokeh

The bar chart

It’s a comfortable color scheme, a little nicer than pyecharts bar charts.

from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral6
from bokeh.plotting import figure
output_file("colormapped_bars.html")Configure the output file name
fruits = ['Apples'.'the meizu'.'OPPO'.'VIVO'.'millet'.'huawei'] # data
counts = [5.3.4.2.4.6] # data
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="")# Bar chart configuration item
p.vbar(x='fruits', top='counts', width=0.9, color='color', legend="fruits", source=source)
p.xgrid.grid_line_color = None Configures the grid line color
p.legend.orientation = "horizontal" # Chart direction is horizontal
p.legend.location = "top_center"
show(p) # Display chart
Copy the code

Annual bar chart

You can compare quantities at different points in time.

from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.plotting import figure
output_file("bars.html") # output file name
fruits = ['Apple'.'the meizu'.'OPPO'.'VIVO'.'millet'.'huawei'] # parameters
years = ['2015'.'2016'.'2017'] # parameters
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 = 0
p.x_range.range_padding = 0.1
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None
show(p)
Copy the code

The pie chart

from collections import Counter
from math import pi
import pandas as pd
from bokeh.io import output_file, show
from bokeh.palettes import Category20c
from bokeh.plotting import figure
from bokeh.transform import cumsum
output_file("pie.html")
x = Counter({
    'China': 157.'the United States': 93.'Japan': 89.'Brazil': 63.'Germany': 44.'India': 42.'Italy': 40.'Australia': 35.'France': 31.'Spain': 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*pi
data['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=None
p.axis.visible=False
p.grid.grid_line_color = None
show(p)
Copy the code

The bar chart

from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource
from bokeh.palettes import GnBu3, OrRd3
from bokeh.plotting import figure
output_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.1
p.ygrid.grid_line_color = None
p.legend.location = "top_left"
p.axis.minor_tick_line_color = None
p.outline_line_color = None
show(p)
Copy the code

A scatter diagram

from bokeh.plotting import figure, output_file, show
output_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)
Copy the code

hexagon

These two days, the hornet’s nest has just been found to falsify data, this, and the hornet’s nest should be appropriate.

import numpy as np
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.util.hex import axial_to_cartesian
output_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 Configure whether the grid is visible
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)
Copy the code

Compared to the bar chart

This implementation is pretty impressive, and it just caught my eye. I have made some comments in the code, and I hope that helps you understand. Note: the center of the circle is the center, that is, where the label is (0,0) in the cartesian coordinate system.

from collections import OrderedDict
from math import log, sqrt
import numpy as np
import pandas as pd
from six.moves import cStringIO as StringIO
from bokeh.plotting import figure, show, output_file

antibiotics = """ Penicillin, Streptomycin, Neomycin, Gram mycobacterium tuberculosis, 800, 5, 2, Negative Salmonella, 10, 0.8, 0.09, Negative Proteus, Klebsiella pneumoniae, 850, 1.2, 1, negative Brucella, 1, 2, 0.02, Negative Pseudomonas aeruginosa, 850, 2, 0.4 100, 0.4, 0.1, 0.1, 870, 1, 1.6, negative Staphylococcus albicans, 0.007, 0.1, 0.001, 14, 10, positive Pneumococci, 0.005, 11, 10, positive """

drug_color = OrderedDict([Configure the middle tag name and color
    ("Penicillin"."#0d3362"),
    ("Streptomycin"."#c64737"),
    ("Neomycin"."black"),
])
gram_color = {
    "positive": "#aeaeb8"."negative": "#e69584",}# Read data
df = pd.read_csv(StringIO(antibiotics),
                 skiprows=1,
                 skipinitialspace=True,
                 engine='python')
width = 800
height = 800
inner_radius = 90
outer_radius = 300 - 10

minr = sqrt(log(001. * 1E4))
maxr = sqrt(log(1000 * 1E4))
a = (outer_radius - inner_radius) / (minr - maxr)
b = inner_radius - a * maxr


def rad(mic) :
    return a * np.sqrt(np.log(mic * 1E4)) + b
big_angle = 2.0 * np.pi / (len(df) + 1)
small_angle = big_angle / 7
# Overall configuration
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 = None
p.ygrid.grid_line_color = None
# annular wedges
angles = np.pi / 2 - big_angle / 2 - df.index.to_series() * big_angle  # Calculate Angle
colors = [gram_color[gram] for gram in df.gram] # Set color
p.annular_wedge(
    0.0, inner_radius, outer_radius, -big_angle + angles, angles, color=colors,
)

# small wedges
p.annular_wedge(0.0, inner_radius, rad(df.penicillin),
                -big_angle + angles + 5 * small_angle, -big_angle + angles + 6 * small_angle,
                color=drug_color['Penicillin'])
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['Streptomycin'])
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['neomycin'])
# Draw a large circle and tag
labels = np.power(10.0, np.arange(-3.4))
radii = a * np.sqrt(np.log(labels * 1E4)) + b
p.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")
# radius
p.annular_wedge(0.0, inner_radius - 10, outer_radius + 10,
                -big_angle + angles, -big_angle + angles, color="black")
# bacterial Tag
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
# Draw the names of each bacterium
p.text(xr, yr, df.bacteria, angle=label_angle,
       text_font_size="9pt", text_align="center", text_baseline="middle")
Draw a circle where the numbers are the X-axis and Y-axis labels
p.circle([-40, -40], [...370, -390], color=list(gram_color.values()), radius=5)
# Draw text
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")
Draw the rectangle with the middle TAB part. Where -40, -40 and -40 are the X-axis coordinates of the three rectangles. 18, 0, and -18 are the y coordinates of the three rectangles
p.rect([-40, -40, -40], [18.0, -18], width=30, height=13,
       color=list(drug_color.values()))
Configure the middle label text, text size, and text alignment
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)
Copy the code

The periodic table of elements

Periodic table of elements, this realization is good cow force ah, from the first three just began to learn chemistry has been very far away, I thought that year or chemistry class representative! I’m not going to do it here because I don’t have to do much chemistry.

This is the end of today’s sharing, if you have better suggestions for data visualization, please leave a message and communicate with me in the comments section. Finally, I wish you all progress in your study. See the introduction of Python gift package on the main page for access.