I am struggling with creating a scatter plot of points based on a Pandas dataframe, with individual labels, markers and colors for each point. There are two issues with the code below, which are described in the comments. For some reason matplotlib doesn't enjoy the way I'm passing markers and labels, but has no problem with the color column.
How should the values be passed?
import pandas as pd
import math
import matplotlib.pyplot as plt
# Read CSV
# Distance,Bearing,Depth,Name,Description,Done,Color,Marker
# 630,90,250,Foo,Bar,FALSE,#777,o
df = pd.read_csv('data.csv')
OFFSET = 100
# Create a few columns from calculations
# This code works as expected
df['h'] = round((df['Distance']**2 - (df['Depth']-OFFSET)**2).apply(math.sqrt)).astype(int)
df['dir'] = (df['Bearing'] - 180) % 360
df['x'] = (df['dir'].apply(math.radians).apply(math.sin) * df['h']).astype(float)
df['y'] = (df['dir'].apply(math.radians).apply(math.cos) * df['h']).astype(float)
# Check that everything looks OK (it does)
print(df.head())
# Problem #1: This works...
plt.scatter(x=df['x'], y=df['y'], c=df['Color'], marker='o')
# ...but this doesn't
# (TypeError: unhashable type: 'Series')
plt.scatter(x=df['x'], y=df['y'], c=df['Color'], marker=df['Marker'])
# Problem #2: Error when adding labels
# (ValueError: The truth value of a Series is ambiguous,
# Use a.empty, a.bool(), a.item(), a.any() or a.all().)
plt.text(x=df['x'], y=df['y'], s=df['Name'])
plt.show()
However, the following works as intended but seems like a large detour:
x = df['x'].tolist()
y = df['y'].tolist()
color = df['Color'].tolist()
marker = df['Marker'].tolist()
label = df['Name'].tolist()
for x, y, c, m, l in zip(x, y, color, marker, label):
plt.scatter(x, y, c=c, marker=m)
plt.text(x, y, l)