I’m encountering an issue with a size-based graph I’m creating using Plotly Express. I’m using the provided code to generate a scattermapbox figure with dots representing different sentiment types. The goal is to assign colors to the dots based on the maximum sentiment type present at each location.
Here’s the relevant code snippet I’m using:
#maximum value for EMPLOYMENT
# Create dictionaries to store the maximum values for each sentiment type at each location
max_values = {}
# Iterate over the infpos, infneg, and infneu lists
for item in emppos + empneg + empneu:
location = (item[1], item[2])
sentiment = item[0]
value = item[3]
# Check if the location is already in the dictionary
if location in max_values:
# Update the maximum value for the corresponding sentiment type
if value > max_values[location][1]:
max_values[location] = (sentiment, value)
else:
# Add the location and its sentiment type with the initial value as maximum
max_values[location] = (sentiment, value)
# Create lists to store the latitude, longitude, sentiment, magnitude, and color for each location
latitudes = []
longitudes = []
sentiments = []
magnitudes = []
colors = []
dot_sizes = []
# Iterate over the maximum values dictionary
for location, (sentiment, value) in max_values.items():
latitudes.append(location[0])
longitudes.append(location[1])
sentiments.append(sentiment)
magnitudes.append(value)
# Find the maximum sentiment type
max_sentiment = max(sentiments, key=lambda x: magnitudes[sentiments.index(x)])
# Calculate the dot size based on the magnitude
dot_sizes.append(value / 2)
# Assign the color based on the sentiment type
if sentiment == 'emppos':
colors.append('green' if sentiment == max_sentiment else 'lightgreen')
elif sentiment == 'empneg':
colors.append('red' if sentiment == max_sentiment else 'pink')
elif sentiment == 'empneu':
colors.append('orange' if sentiment == max_sentiment else 'lightorange')
# Create a scattermapbox figure with all the locations, colors, and hover information
fig70 = go.Figure(
go.Scattermapbox(
lat=latitudes,
lon=longitudes,
mode='markers',
marker=dict(
size=dot_sizes,
color='blue',
opacity=0.8
),
text=[f"{sentiment}<br>Magnitude: {magnitude}" for sentiment, magnitude in zip(sentiments, magnitudes)],
hoverinfo='text'
)
)
fig70.update_layout(
mapbox=dict(
style='stamen-toner',
center=dict(lat=30.418333, lon=70.360278),
zoom=3.8
),
title='<b>MAXIMUM SENTIMENT VALUES</b>',
title_x=0.5,
title_y=0.95
)
graph68JSON = json.dumps(fig70, cls=plotly.utils.PlotlyJSONEncoder)
The issue I’m facing is that all the dots are being displayed in black color or any one color(e.g blue) which i specify in “go.scattermapbox(marker=dict(color=‘blue’)”, regardless of the maximum sentiment type. I want the dots to be displayed in different colors based on the maximum sentiment type at each location (e.g., green for ‘infpos’, red for ‘infneg’, and yellow for ‘infneu’).
I would greatly appreciate any guidance or suggestions on how to resolve this issue. Thank you!