添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
爱搭讪的蚂蚁  ·  Compare schemas false ...·  5 天前    · 
路过的大象  ·  Apache OpenOffice ...·  5 天前    · 
想出国的豆腐  ·  PostgreSQL Python: ...·  2 天前    · 
谦逊的楼梯  ·  Introduction to plottable·  3 小时前    · 
睿智的钢笔  ·  Plots in plottable·  3 小时前    · 
另类的甜瓜  ·  Paypal module return ...·  3 月前    · 
从容的排球  ·  UNPKG - @types/codemirror·  6 月前    · 
温暖的小马驹  ·  Tridion Docs ...·  6 月前    · 
谦逊的红茶  ·  Search - Government.se·  1 年前    · 

Introduction to plottable


Plottable is a python library for plotting nice table outputs . You can custom style, colors, add images and even more with a light python syntax!
In this post, we'll go over the most basic features of this library. For more advanced customizations, see How to customize colors in plottable and How to add an image in plottable .
Since there is 3 ways to custom your table with plottable , we'll briefly go over each one.

Libraries

First, you need to install plottable with the following command: pip install plottable . Plottable is almost the single python library made especially for creating nice output tables. According to the main author, it's inspired from the R packages gt and reactable .

We'll also need the following libraries:

  • pandas for creating a dataframe with our data
  • matplotlib for customizing the table and display it
  • import matplotlib.pyplot as plt
    import pandas as pd
    from plottable import Table

    Dataset

    Tables are often built from a pandas dataframe . Let's create one using the DataFrame() function.

    This table is our starting point of the tutorial. Let's make it look good!

    data = {'Score': [8, 6, 10, 3, 9],
            'Value': [50.42, 17.01, 42.90, 6.83, 92.06],
            'Comment': ['Nice', 'Cool', 'Fun', 'Bad', 'Okay']}
    df = pd.DataFrame(data)

    Most simple plottable output

    Now let's see how to create the default plottable output . The syntax is quite similar to matplotlib , since we need to initiate a figure, create a Table() and display the result with show() .

    # Init a figure 
    fig, ax = plt.subplots(figsize=(5, 5))
    # Create the Table() object
    tab = Table(df)
    # Display the output
    plt.show()

    First method: column definition

    This method consists in changing the properties of interest for a given column . By specifying the name of the column to be changed, we can specify a number of parameters.

    We'll also need to import another Class from plottable: ColumnDefinition

    In this case, we'll:

  • change the title of the Score column
  • change position and weight of the texts
  • from plottable import ColumnDefinition
    # Init a figure 
    fig, ax = plt.subplots(figsize=(5, 5))
    # Define the things to change
    column_definitions = [ColumnDefinition(name='Score', # name of the column to change
                                           title='Score custom', # new title for the column
                                           textprops={"ha": "left", "weight": "bold"}, # properties to apply
    # Create the table object with the column definitions parameter
    tab = Table(df, column_definitions=column_definitions)
    # Display the output
    plt.show()

    Second method: keywords

    If you want to apply exactly the same changes to each column, the keyword method may be just what you're looking for.

    In a similar way to the previous method, we give which properties we want to change, but this time we put them directly in the Table() argument, and they will automatically apply to all columns.

    In this case, we'll:

  • change some text properties
  • add a footer line
  • remove lines between rows
  • # Init a figure 
    fig, ax = plt.subplots(figsize=(5, 5))
    # Create the table object with the column definitions parameter
    tab = Table(df, textprops={"ha": "left", "weight": "bold"}, # text properties
                row_dividers=False, # remove lines between rows
                footer_divider=True, # add a reference line at the end
    # Display the output
    plt.show()

    Third method: accessing rows and cols

    In this method, we first define the Table() , then apply any modifications we wish to the rows and columns.

    # Init a figure 
    fig, ax = plt.subplots(figsize=(5, 5))
    # Create the table object with the column definitions parameter
    tab = Table(df)
    # Apply modifications to the Score col and the second (idx=1) row
    tab.columns["Score"].set_facecolor('lightblue')
    tab.rows[1].set_fontcolor('green')
    # Display the output
    plt.show()

    Going further

    This post explains the basic features in the plottable library.

    You might be interested in:

  • How to customize colors in plottable
  • How to add an image in plottable
  •