Python - Visualizing data in a diagram

时间:2018-03-22 23:26:06

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

I have a data with two columns: Product and Category. See below for an example of the data:

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]}) 

I would like to visualize this data in a diagram:

desired_viz

Here the "Total" is the total value of the entire data frame, "A" & "B" boxes are the total value for each product, and then the values for each product & category are in the right-most box.

I'm not very familiar with the viz packages available in Python. Is there a package that exists that does these types of visualizations.

1 个答案:

答案 0 :(得分: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)

结果:

enter image description here