Search code examples
python-3.xdata-visualizationdiagramdendrogram

Python - Visualizing data in a 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.


Solution

  • You can use graphviz. But you need to extract your own blocks/nodes

    Example:

    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)
    

    data diagram python graphviz