当前位置:   article > 正文

Python搞定表格可视化!

python制作表格界面

分享一个Python工具plottable,轻松制作高度个性化的表格,底层为Matplotlib。

例如这样的,fce9d5d35cf37f094ab99481a74f207b.png或者这样的,94468c071f310138cf83b44476d7e434.png第一张图详细代码:

  1. # 导入相关包
  2. from pathlib import Path
  3. import matplotlib
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import pandas as pd
  7. from matplotlib.colors import LinearSegmentedColormap
  8. from plottable import ColumnDefinition, Table
  9. from plottable.cmap import normed_cmap
  10. from plottable.formatters import decimal_to_percent
  11. from plottable.plots import circled_image
  12. plt.rcParams["font.family"] = ["DejaVu Sans"]
  13. plt.rcParams["savefig.bbox"] = "tight"
  14. # demo数据准备
  15. cols = [
  16.     "team",
  17.     "points",
  18.     "group",
  19.     "spi",
  20.     "global_o",
  21.     "global_d",
  22.     "group_1",
  23.     "group_2",
  24.     "group_3",
  25.     "make_round_of_16",
  26.     "make_quarters",
  27.     "make_semis",
  28.     "make_final",
  29.     "win_league",
  30. ]
  31. df = pd.read_csv(
  32.     "data/wwc_forecasts.csv",
  33.     usecols=cols,
  34. )
  35. colnames = [
  36.     "Team",
  37.     "Points",
  38.     "Group",
  39.     "SPI",
  40.     "OFF",
  41.     "DEF",
  42.     "1st Place",
  43.     "2nd Place",
  44.     "3rd Place",
  45.     "Make Rd Of 16",
  46.     "Make Quarters",
  47.     "Make Semis",
  48.     "Make Finals",
  49.     "Win World Cup",
  50. ]
  51. col_to_name = dict(zip(cols, colnames))
  52. flag_paths = list(Path("country_flags").glob("*.png"))
  53. country_to_flagpath = {p.stem: p for p in flag_paths}
  54. df[["spi""global_o""global_d"]] = df[["spi""global_o",
  55.                                           "global_d"]].round(1)
  56. df = df.rename(col_to_name, axis=1)
  57. df = df.drop("Points", axis=1)
  58. df.insert(0"Flag", df["Team"].apply(lambda x: country_to_flagpath.get(x)))
  59. df = df.set_index("Team")
  60. # colormap准备
  61. cmap = LinearSegmentedColormap.from_list(
  62.     name="bugw",
  63.     colors=["#ffffff""#f2fbd2""#c9ecb4""#93d3ab""#35b0ab"],
  64.     N=256)
  65. team_rating_cols = ["SPI""OFF""DEF"]
  66. group_stage_cols = ["1st Place""2nd Place""3rd Place"]
  67. knockout_stage_cols = list(df.columns[-5:])
  68. # table列个性化list,例如列名、列宽、字体、磅值等等
  69. col_defs = ([
  70.     ColumnDefinition(
  71.         name="Flag",
  72.         title="Region",
  73.         textprops={"ha""center"},
  74.         width=0.5,
  75.         plot_fn=circled_image,
  76.     ),
  77.     ColumnDefinition(
  78.         name="Team",
  79.         textprops={
  80.             "ha""left",
  81.             "weight""bold"
  82.         },
  83.         width=1.5,
  84.     ),
  85.     ColumnDefinition(
  86.         name="Group",
  87.         textprops={"ha""center"},
  88.         width=0.75,
  89.     ),
  90.     ColumnDefinition(
  91.         name="SPI",
  92.         group="Team Rating",
  93.         textprops={"ha""center"},
  94.         width=0.75,
  95.     ),
  96.     ColumnDefinition(
  97.         name="OFF",
  98.         width=0.75,
  99.         textprops={
  100.             "ha""center",
  101.             "bbox": {
  102.                 "boxstyle""circle",
  103.                 "pad"0.35
  104.             },
  105.         },
  106.         cmap=normed_cmap(df["OFF"], cmap=matplotlib.cm.Blues, num_stds=2.5),
  107.         group="Team Rating",
  108.     ),
  109.     ColumnDefinition(
  110.         name="DEF",
  111.         width=0.75,
  112.         textprops={
  113.             "ha""center",
  114.             "bbox": {
  115.                 "boxstyle""circle",
  116.                 "pad"0.35
  117.             },
  118.         },
  119.         cmap=normed_cmap(df["DEF"], cmap=matplotlib.cm.Greens, num_stds=2.5),
  120.         group="Team Rating",
  121.     ),
  122. ] + [
  123.     ColumnDefinition(
  124.         name=group_stage_cols[0],
  125.         title=group_stage_cols[0].replace(" ""\n"1),
  126.         formatter=decimal_to_percent,
  127.         group="Group Stage Chances",
  128.         border="left",
  129.     )
  130. ] + [
  131.     ColumnDefinition(
  132.         name=col,
  133.         title=col.replace(" ""\n"1),
  134.         formatter=decimal_to_percent,
  135.         group="Group Stage Chances",
  136.     ) for col in group_stage_cols[1:]
  137. ] + [
  138.     ColumnDefinition(
  139.         name=knockout_stage_cols[0],
  140.         title=knockout_stage_cols[0].replace(" ""\n"1),
  141.         formatter=decimal_to_percent,
  142.         cmap=cmap,
  143.         group="Knockout Stage Chances",
  144.         border="left",
  145.     )
  146. ] + [
  147.     ColumnDefinition(
  148.         name=col,
  149.         title=col.replace(" ""\n"1),
  150.         formatter=decimal_to_percent,
  151.         cmap=cmap,
  152.         group="Knockout Stage Chances",
  153.     ) for col in knockout_stage_cols[1:]
  154. ])
  155. # plottable的Table方法制作表格
  156. fig, ax = plt.subplots(figsize=(2022))
  157. table = Table(
  158.     df,
  159.     column_definitions=col_defs,
  160.     row_dividers=True,
  161.     footer_divider=True,
  162.     ax=ax,
  163.     textprops={
  164.         "fontsize"14
  165.     },
  166.     row_divider_kw={
  167.         "linewidth"1,
  168.         "linestyle": (0, (15))
  169.     },
  170.     col_label_divider_kw={
  171.         "linewidth"1,
  172.         "linestyle""-"
  173.     },
  174.     column_border_kw={
  175.         "linewidth"1,
  176.         "linestyle""-"
  177.     },
  178. ).autoset_fontcolors(colnames=["OFF""DEF"])

-------- End --------

推荐声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】

推荐阅读
相关标签