0

I am improving my data visualization skills. One of the top recommendations is use the colors to improve the readability of plots. Well, I am picking the plots on the great book Storytelling with Data https://www.storytellingwithdata.com/ and coding them. The following one is on my desk now.

Example Plot with text with many colors

Using matplotlib to put this information mark with the green blocks may be a nightmare. I believe HTML will be easier for this task. On notebooks, this will be perfect, however, if I need to export this to a presentation, for example, I will need to use some print screen to finish my job. I try to use the magic %%capture to save the whole cell output into an image or a HTML. After some searches, I think this is impossible. Do you have some ideas on how I can handle this? Thanks a lot!

GregOliveira
  • 151
  • 10

2 Answers2

0

First, let me say that this site is not intended to be a code service. There is no presentation of the code you are working on. I am not affiliated with Plotly, and I created the graphs in Plotly because I think Plotly has elements that are a good example of a library where HTML can be used as an annotation. You are welcome to create your own to suit your own purposes.

df = df.replace('\%','', regex=True).astype(int)

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Bar(
    y=df.index,
    x=df['Bottom box'],
    name='Bottom box',
    orientation='h',
    marker=dict(
        color='rgb(196, 74, 70)',
        line=dict(color='rgb(196, 74, 70)', width=1)
    )
))
fig.add_trace(go.Bar(
    y=df.index,
    x=df['Middle'],
    name='Middle',
    orientation='h',
    marker=dict(
        color='rgb(178, 178, 178)',
        line=dict(color='rgb(178, 178, 178)', width=1)
    )
))
fig.add_trace(go.Bar(
    y=df.index,
    x=df['Top box'],
    name='Top box',
    orientation='h',
    marker=dict(
        color='rgb(81, 172, 201)',
        line=dict(color='rgb(81, 172, 201)', width=1)
    )
))
# bar labels
fig.add_trace(go.Scatter(
    y=df.index,
    x=[5,5,5,5],
    mode='text',
    text=['{}%'.format(x) for x in df['Bottom box']],
    textfont=dict(
        color='white',
        size=16),
))

fig.add_trace(go.Scatter(
    y=df.index,
    x=[95,95,95,95],
    mode='text',
    text=['{}%'.format(x) for x in df['Top box']],
    textfont=dict(
        color='white',
        size=16),
))

# title
fig.add_annotation(
    x=-0.15,
    y=1.25,
    xref='paper',
    yref='paper',
    text='Resultados da Pesquisa: Equipe X',
    font=dict(
        color='rgb(178, 178, 178)',
        size=24,
    ),
    showarrow=False,
)
# legend text
legend_txt = ('<span style="color:rgb(196, 74, 70);font-size:14px;">Discordo totalmente </span>|'
+'<span style="color:rgb(178, 178, 178);font-size:14px;"> Discordo | Neutro | Concordo |</span>'
+'<span style="color:rgb(81, 172, 201);font-size:14px;"> Concordo totalmente</span><br>'
+'<span style="color:rgb(178, 178, 178);font-size:14px;"> Porcentagem do Total</span>')
    
fig.add_annotation(
    x=-0.01,
    y=1.15,
    xref='paper',
    yref='paper',
    text=legend_txt,
    showarrow=False,
)

# right text
right_text = ('<span style="color:rgb(81, 172, 201);font-size:14px;"> Item A da presquisa </span><br>'
+'<span style="color:"black";font-size:14px;">com classification mais<br>alta para asequipe X<span>')

fig.add_annotation(
    x=1.25,
    y=0.98,
    xref='paper',
    yref='paper',
    text=right_text,
    showarrow=False,
)

# right text
right_text2 = ('<span style="rgb(178, 178, 178);font-size:14px;"> A insatisfacao foi <br>'
+'maior para o </span><span style="color:rgb(196, 74, 70);font-size:14px;">item <br>D da pesquisa</span>')

fig.add_annotation(
    x=1.20,
    y=0.05,
    xref='paper',
    yref='paper',
    text=right_text2,
    showarrow=False,
)

fig.update_layout(autosize=True,
                  height=600,
                  width=800,
                  barmode='stack',
                  template='plotly_white',
                  showlegend=False,
                  margin=dict(l=0,r=150,t=120,b=0)
                 )
fig.update_yaxes(showgrid=False,
                 autorange="reversed",
                 showline=True,
                 linecolor='white',
                 tickfont=dict(
                     size=16,
                     family='sans serif',
                 ))
fig.update_xaxes(
    side='top',
    linecolor='rgb(178, 178, 178)',
    ticks='inside',
    showgrid=False,
    showline=True,
    tickvals=np.arange(0,101,20),
    ticktext=['{}%'.format(x) for x in np.arange(0,101,20)],
    scaleanchor='x2',
)

fig.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • Thanks! I am not seeking to receive the whole code, but I am really happy to see it and I will study it. Returning to my question, I just think about to export my own result. I did not present my code because anything that I tried and searched got wrong (%%capture). – GregOliveira Jul 31 '22 at 23:15
  • See [Interactive HTML Export in Python](https://plotly.com/python/interactive-html-export/) for instructions on how to output the graph. [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – r-beginners Aug 01 '22 at 00:33
0

After some more researches, I got that, by now, it is not possible to accomplish what I want. I could save the plots as images, create a html file and use a package or API to render the html into a image, but this comes with other problems that I want do avoid. This question (HTML to IMAGE using Python) is about a related problem and, on certain way, (not)solved my question.

GregOliveira
  • 151
  • 10