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')
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.
Adding Text to Figures Standalone text annotations can be added to figures using fig. add_annotation() , with or without arrows, and they can be positioned absolutely within the figure, or they can be positioned relative to the axes of 2d or 3d cartesian subplots i.e. in data coordinates.
An annotation is a text element that can be placed anywhere in the plot. It can be positioned with respect to relative coordinates in the plot or with respect to the actual data coordinates of the graph. Annotations can be shown with or without an arrow. align. Code: fig.update_annotations(align=<VALUE>)
Hover Labels One of the most deceptively-powerful features of interactive visualization using Plotly is the ability for the user to reveal more information about a data point by moving their mouse cursor over the point and having a hover label appear. There are three hover modes available in Plotly.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With