Label python data points on plot
How about print (x, y)
at once.
from matplotlib import pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
A = -0.75, -0.25, 0, 0.25, 0.5, 0.75, 1.0
B = 0.73, 0.97, 1.0, 0.97, 0.88, 0.73, 0.54
ax.plot(A,B)
for xy in zip(A, B): # <--
ax.annotate('(%s, %s)' % xy, xy=xy, textcoords='data') # <--
ax.grid()
plt.show()
Adding data labels to line graph in Matplotlib
In your code temp_labels
is a list of lines, so x
is a line object, which cannot be used to index a list, as indicated by the error. Start from here:
import matplotlib.pyplot as plt
dates = [10,11,12]
temp = [10,14,12]
plt.plot(dates,temp)
for x, y in zip(dates, temp):
label = y
plt.annotate(label, (x, y),
xycoords="data",
textcoords="offset points",
xytext=(0, 10), ha="center")
plt.show()
Labelling a data point in matplotlib using csv data
An example of annotate
use with your data:
fig, ax = plt.subplots(figsize=(15, 10))
x = df['pass_att'].values
y = df['pass_cmp'].values
t = df['player'].values
ax.scatter(x, y)
ax.set_xlabel('Pass Att')
ax.set_ylabel('Pass Cmp')
ax.set_title('Pass Att vs Pass Cmp')
for i, txt in enumerate(t):
ax.annotate(txt, xy=(x[i], y[i]), xytext=(x[i], y[i]+0.5), ha='center')
plt.show()
Scatter plot with different text at each data point
I'm not aware of any plotting method which takes arrays or lists but you could use annotate()
while iterating over the values in n
.
import matplotlib.pyplot as plt
y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
z = [0.15, 0.3, 0.45, 0.6, 0.75]
n = [58, 651, 393, 203, 123]
fig, ax = plt.subplots()
ax.scatter(z, y)
for i, txt in enumerate(n):
ax.annotate(txt, (z[i], y[i]))
There are a lot of formatting options for annotate()
, see the matplotlib website:
Python Matplotlib scatter plot labeling at plot points
Try this:
col_names=['City','Lat','Long']
df = pd.read_clipboard(sep=',', names=col_names)
def label_point(x, y, val, ax):
a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)
for i, point in a.iterrows():
ax.text(point['x']+.02, point['y'], str(point['val']))
ax = df.plot.scatter('Lat', 'Long', figsize=(12,8))
label_point(df['Lat'], df['Long'], df['City'], ax)
Output:
I am unable to label the data points on the graph using matplotlib
You can add these two additional lines after plotting the scatter plots. They will add the text to the first and last entries.
You can do additional things like background box, etc. if required. You can check matplotlib documentation and examples here
offset=0.05
week4.annotate(country[0], (happy[0]+offset, satis[0]+offset), color='red', weight='bold')
week4.annotate(country.iat[-1], (happy.iat[-1]+offset, satis.iat[-1]+offset), color='blue', weight='bold')
Output graph
Labeling points in matplotlib scatterplot
You can do that:
import matplotlib.pyplot as plt
points = [[3,9],[4,8],[5,4]]
for i in range(len(points)):
x = points[i][0]
y = points[i][1]
plt.plot(x, y, 'bo')
plt.text(x * (1 + 0.01), y * (1 + 0.01) , i, fontsize=12)
plt.xlim((0, 10))
plt.ylim((0, 10))
plt.show()
Related Topics
How to Make a Tkinter Window Jump to the Front
How to Check If Type of a Variable Is String
How to Check If There Are Duplicates in a Flat List
Pandas Select from Dataframe Using Startswith
Why Is My Pygame Display Not Responding While Waiting for Input
How to Convert This List of Dictionaries to a CSV File
How to Build a Numpy Array from a Generator
Pandas Cannot Open an Excel (.Xlsx) File
Python MySQLdb: Library Not Loaded: Libmysqlclient.18.Dylib
Indexerror: Tuple Index Out of Range When Using Py2Exe
How to Remove All Characters After a Specific Character in Python
Is the Time-Complexity of Iterative String Append Actually O(N^2), or O(N)
Calculating Direction of the Player to Shoot Pygame
Numpy Matrix Vector Multiplication