13

I'm trying to add text labels next to the data points in a Plotly scatter plot in Python but I get an error.

How can I do that?

Here is my dataframe:

world_rank  university_name country teaching    international   research    citations   income  total_score num_students    student_staff_ratio international_students  female_male_ratio   year
0   1   Harvard University  United States of America    99.7    72.4    98.7    98.8    34.5    96.1    20,152  8.9 25% NaN 2011

Here is my code snippet:

citation = go.Scatter(
                    x = "World Rank" + timesData_df_top_50["world_rank"],  <--- error
                    y = "Citation" + timesData_df_top_50["citations"], <--- error
                    mode = "lines+markers",
                    name = "citations",
                    marker = dict(color = 'rgba(48, 217, 189, 1)'),
                    text= timesData_df_top_50["university_name"])

The error is shown below.

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')
4
  • Could you please share the first few rows of your data frame? Commented Apr 21, 2020 at 11:36
  • Thank you. The error is due to the fact that you are trying to add a string (e.g. "Citation") to a float (e.g. 98.8), what are you trying to achieve exactly? Do you want to display some text next to each point on the scatter plot? Or do you want to change the axes titles? Commented Apr 21, 2020 at 12:41
  • @gflavia I want to display some text next to each point on the scatter plot. Commented Apr 21, 2020 at 16:16
  • Does this answer your question? Plotly: How to annotate figure by a list of strings and a list of coordinates? Commented Apr 21, 2020 at 22:24

1 Answer 1

35

You can include the text labels in the text attribute. To make sure that they are displayed on the scatter plot, set mode='lines+markers+text'. See the Plotly documentation on text and annotations. I included an example below based on your code.

import plotly.graph_objects as go
import pandas as pd

df = pd.DataFrame({'world_rank': [1, 2, 3, 4, 5],
                   'university_name': ['Harvard', 'MIT', 'Stanford', 'Cambridge', 'Oxford'],
                   'citations': [98.8, 98.7, 97.6, 97.5, 96]})

layout = dict(plot_bgcolor='white',
              margin=dict(t=20, l=20, r=20, b=20),
              xaxis=dict(title='World Rank',
                         range=[0.9, 5.5],
                         linecolor='#d9d9d9',
                         showgrid=False,
                         mirror=True),
              yaxis=dict(title='Citations',
                         range=[95.5, 99.5],
                         linecolor='#d9d9d9',
                         showgrid=False,
                         mirror=True))

data = go.Scatter(x=df['world_rank'],
                  y=df['citations'],
                  text=df['university_name'],
                  textposition='top right',
                  textfont=dict(color='#E58606'),
                  mode='lines+markers+text',
                  marker=dict(color='#5D69B1', size=8),
                  line=dict(color='#52BCA3', width=1, dash='dash'),
                  name='citations')

fig = go.Figure(data=data, layout=layout)

fig.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.