0

I have a dataframe called df that looks like this:

Qname    X    Y    Magnitude
Bob      5    19    10
Tom      6    20    20
Jim      3    30    30

I would like to make a visual text plot of the data. I want to plot the Qnames on a figure with their coordinates set = X,Y and a s=Size.

I have tried:

fig = plt.figure()
ax = fig.add_axes((0,0,1,1))
X = df.X
Y = df.Y
S = df.magnitude
Name = df.Qname
ax.text(X, Y, Name, size=S, color='red', rotation=0, alpha=1.0, ha='center', va='center')
fig.show()

However nothing is showing up on my plot. Any help is greatly appreciated.

Jeremy
  • 779
  • 3
  • 9
  • 14

1 Answers1

1

This should get you started. Matplotlib does not handle the text placement for you so you will probably need to play around with this.

import pandas as pd
import matplotlib.pyplot as plt

# replace this with your existing code to read the dataframe
df = pd.read_clipboard()

plt.scatter(df.X, df.Y, s=df.Magnitude)


# annotate the plot
# unfortunately you have to iterate over your points
# see http://stackoverflow.com/q/5147112/553404

for idx, row in df.iterrows():
    # see http://stackoverflow.com/q/5147112/553404
    # for better annotation options
    plt.annotate(row['Qname'], xy=(row['X'], row['Y']))

plt.show()
YXD
  • 31,741
  • 15
  • 75
  • 115