How to make plotly text bold using scatter

I’m trying to make a graph using plotly library and I want to make some texts in bold here’s the code used :slight_smile:

import plotly.express as px
import pandas as pd
data = {
    "lib_acte":["test 98lop1", "test9665 opp1", "test QSDFR1", "test ABBE1", "testtest21","test23"],
    "x":[12.6, 10.8, -1, -15.2, -10.4, 1.6],
    "y":[15, 5, 44, -11, -35, -19],
    "circle_size":[375, 112.5, 60,210, 202.5, 195],
    "color":["green", "green", "green", "red", "red", "red"],
    "textfont":["normal", "normal", "normal", "bold", "bold", "bold"],
}

#load data into a DataFrame object:
df = pd.DataFrame(data)

fig = px.scatter(
        df,
        x="x", 
        y="y", 
        color="color",
        size='circle_size',
        text="lib_acte",
        hover_name="lib_acte",
        color_discrete_map={"red": "red", "green": "green"},
        title="chart"
      )
fig.update_traces(textposition='middle right', textfont_size=14, textfont_color='black', textfont_family="Inter", hoverinfo="skip")
newnames = {'red':'red title', 'green': 'green title'}

fig.update_layout(
        {
            
            'yaxis': {
                "range": [-200, 200],
                'zerolinewidth': 2, 
                "zerolinecolor": "red",
                "tick0": -200,
                "dtick":45,
            },
            'xaxis': {
                "range": [-200, 200],
                'zerolinewidth': 2, 
                "zerolinecolor": "gray",
                "tick0": -200,
                "dtick": 45,
                #  "scaleanchor": 'y'
            },
           
            "height": 800,
        }
    )
fig.add_scatter(
        x=[0, 0, -200, -200],
        y=[0, 200, 200, 0],
        fill="toself",
        fillcolor="gray",
        zorder=-1,
        mode="markers",
        marker_color="rgba(0,0,0,0)",
        showlegend=False,
        hoverinfo="skip"
    )
fig.add_scatter(
        x=[0, 0, 200, 200],
        y=[0, -200, -200, 0],
        fill="toself",
        fillcolor="yellow",
        zorder=-1,
        mode="markers",
        marker_color="rgba(0,0,0,0)",
        showlegend=False,
        hoverinfo="skip"
    )
fig.update_layout(
   
            paper_bgcolor="#F1F2F6",
        )
fig.show()

and here’s the output :

what I’m trying to do is to make “test ABBE1”, “testtest21”,“test23” in bold on the graph, could anyone please help how to do that ?

thanks

If you want to decorate individual labels, you should graph the data frame row by row. First, create it in a graph object. Create a unique list of labels and a list of whether to display a legend or not. Loop through it with df.itertuples(). Each is individually specified using marker + text mode. The hover template and legend are set to show or hide.

Keep in mind, however, that this technique is only effective for small amounts of data, and large amounts of data will result in poor performance and huge file sizes.

import plotly.graph_objects as go

labels = df['lib_acte'].unique()
legends = [True, False, False, True, False, False]

fig = go.Figure()

for row,boolean in zip(df.itertuples(), legends):
    label = row.lib_acte
    fig.add_trace(go.Scatter(
        x=[row.x],
        y=[row.y],
        mode='markers+text',
        marker=dict(size=row.circle_size/10, sizemode='area', sizeref=0.9375, color=row.color, line_width=1, line_color='white'),
        text=f'<b>{row.lib_acte}</b>' if label in ["test ABBE1","testtest21","test23"] else row.lib_acte,
        hovertemplate=f'<b>%{row.lib_acte}</b><br>color={row.color}<br>'
        + 'x=%{row.x}<br>y=%{row.y}<br>circle_size={row.circle_size}<br>'
        + 'lib_acte={row.lib_acte}<extra></extra>',
        name=row.color,
        showlegend=boolean,
    ))

fig.update_traces(textposition='middle right', textfont_size=14, textfont_color='black', textfont_family="Inter", hoverinfo="skip")
newnames = {'red':'red title', 'green': 'green title'}

fig.update_layout(
        {
            
            'yaxis': {
                "range": [-200, 200],
                'zerolinewidth': 2, 
                "zerolinecolor": "red",
                "tick0": -200,
                "dtick":45,
            },
            'xaxis': {
                "range": [-200, 200],
                'zerolinewidth': 2, 
                "zerolinecolor": "gray",
                "tick0": -200,
                "dtick": 45,
                #  "scaleanchor": 'y'
            },
           
            "height": 800,
        }
    )
fig.add_scatter(
        x=[0, 0, -200, -200],
        y=[0, 200, 200, 0],
        fill="toself",
        fillcolor="gray",
        zorder=-1,
        mode="markers",
        marker_color="rgba(0,0,0,0)",
        showlegend=False,
        hoverinfo="skip"
    )
fig.add_scatter(
        x=[0, 0, 200, 200],
        y=[0, -200, -200, 0],
        fill="toself",
        fillcolor="yellow",
        zorder=-1,
        mode="markers",
        marker_color="rgba(0,0,0,0)",
        showlegend=False,
        hoverinfo="skip"
    )
fig.update_layout(paper_bgcolor="#F1F2F6",)

fig.show()

1 Like