admin管理员组

文章数量:1406918

The code below successfully produces the body of the diagram that I am trying to create.

Help needed: how can I add the title boxes "Codes", "Secondary Codes" and "Themes" which sit at the top of each section?


Things I've tried:

  • Adding label to the nodes: but this places the label above the individual lines and not at the top of the section

  • Creating new nodes just for the titles but it all falls apart when I try to connect them with the edge.


from graphviz import Digraph

# rankdir: sets direction of graph layout /
dot = Digraph(graph_attr={'rankdir':'LR'})

# Themes

# Set attributes to force orthogonal lines
dot.attr(splines='ortho')

# Set the gap between the nodes
dot.attr(nodesep='0.1')

dot.node('T1', 'Drivers', shape='ellipse', style='filled', color='darkred', fontcolor='white')

dot.node('T2', 'Challenges &\nInitial Concerns', shape='ellipse', style='filled', color='darkorange', fontcolor='white')

dot.node('T3', 'Benefits', shape='ellipse', style='filled', color='darkgreen', fontcolor='white')


# Add secondary codes and link to themes v2

dot.node('Financial Challenges', shape='box', style='rounded', pos='0,1!', width='2', height='0.5', fixedsize='true')
dot.edge('Financial Challenges', 'T1', arrowhead='none')
         
dot.node('Commercial Challenges', shape='box', style='rounded', pos='1,1!', width='2', height='0.5', fixedsize='true')
dot.edge('Commercial Challenges', 'T1', arrowhead='none')

dot.node('Lack of Human Capital', shape='box', style='rounded', pos='3,1!', width='2', height='0.5', fixedsize='true')
dot.edge('Lack of Human Capital', 'T1', arrowhead='none')
           
dot.node('Open Innovation', shape='box', style='rounded', pos='0,1!', width='2', height='0.5', fixedsize='true')
dot.edge('Open Innovation', 'T2', arrowhead='none')

dot.node('Reputational Damage', shape='box', style='rounded', pos='1,1!', width='2', height='0.5', fixedsize='true')
dot.edge('Reputational Damage', 'T2', arrowhead='none')

dot.node('Structure Contrast', shape='box', style='rounded', pos='2,1!', width='2', height='0.5', fixedsize='true')
dot.edge('Structure Contrast', 'T2', arrowhead='none')
         
dot.node('Human Capital', shape='box', style='rounded', pos='0,1!', width='2', height='0.5', fixedsize='true')
dot.edge('Human Capital', 'T3', arrowhead='none')

dot.node('Social Capital', shape='box', style='rounded', pos='1,1!', width='2', height='0.5', fixedsize='true')
dot.edge('Social Capital', 'T3', arrowhead='none')

dot.node('Credibility & \n Investor Trust', shape='box', style='rounded', pos='2', width='2', height='0.6', fixedsize='true')
dot.edge('Credibility & \n Investor Trust', 'T3', arrowhead='none')


# Create dictionary of the 'codes'
# Key = string; Val = secondary code to link to
# Use \l to left align the content on line break

codes = {
    '• Lack of resources \l• Lack of investment funding \l': 'Financial Challenges',
    '• Lack of investor trust \l• Brand association \l': 'Commercial Challenges',
    '• Lack of sector insights \l• Mentoring \l': 'Lack of Human Capital',
    '• Intellectual property \l• Idea sharing \l• Power dynamic \l': 'Open Innovation',
    '• Public perception of oil and gas company \l': 'Reputational Damage',
    '• Pace of business \l• Decision-making process \l': 'Structure Contrast',
    '• Specialised knowledge and expertise through mentoring \l• Technical peer review with experts \l': 'Human Capital',
    '• Access to global networks \l• Knowledge spillover between cohort members \l': 'Social Capital',
    '• Brand association \l': 'Credibility & \n Investor Trust'
}

# Add codes and link to the secondary_codes

for code_string, secondary_code in codes.items():
    dot.node(code_string, shape='box', width='4.5', height='1', fixedsize='true')
    dot.edge(code_string, secondary_code, arrowhead='none')

# Scrappy way to set the colors on the secondary codes

dot.node('Financial Challenges', color='red')
dot.node('Commercial Challenges', color='red')
dot.node('Lack of Human Capital', color='red')

dot.node('Open Innovation', color='orange')
dot.node('Reputational Damage', color='orange')
dot.node('Structure Contrast', color='orange')

dot.node('Human Capital', color='green')
dot.node('Social Capital', color='green')
dot.node('Credibility & \n Investor Trust', color='green')

# Render
dot.render('coding_tree', format='png', cleanup=True)

本文标签: graphvizHow to have title boxes for each sectionStack Overflow