【问题标题】:Python - Visualizing data in a diagramPython - 在图表中可视化数据
【发布时间】:2018-03-22 23:26:06
【问题描述】:

我有一个包含两列的数据:产品和类别。请参阅下面的数据示例:

import pandas as pd
df = pd.DataFrame({'Product': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'Category': ['Text', 'Text2', 'Text3', 'Text4', 'Text', 'Text2', 'Text3', 'Text4'],
                   'Value': [80, 10, 5, 5, 5, 3, 2, 0]}) 

我想在图表中可视化这些数据:

这里的“Total”是整个数据框的总值,“A”和“B”框是每个产品的总值,然后每个产品和类别的值在最右边的框中.

我对 Python 中可用的可视化包不是很熟悉。是否存在执行这些类型的可视化的包。

【问题讨论】:

标签: python-3.x data-visualization diagram dendrogram


【解决方案1】:

您可以使用graphviz。但是您需要提取自己的块/节点

例子:

from graphviz import Graph

g = Graph()
g.attr(rankdir='RL')

T = df['Value'].sum()
g.node('1', 'Total = ' + str(T), shape='square')

A = df.loc[df.Product == 'A', ['Category', 'Value']].to_string(index=False)
g.node('2', A, shape='square')

B = df.loc[df.Product == 'B', ['Category', 'Value']].to_string(index=False)
g.node('3', B, shape='square')

g.edges(['21', '31'])

g.render(view=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多