import numpy as np
import matplotlib
from dsutil.plotting import add_grid
from platform import python_version
python_version(), np.__version__,matplotlib.__version__
('3.6.9', '1.19.0', '3.2.2')
np.random.seed(42)
import matplotlib.pyplot as plt
import numpy as np
plt.clf()
# using some dummy data for this example
xs = np.arange(0,10,1)
ys = np.random.normal(loc=2.0, scale=0.8, size=10)
plt.plot(xs,ys)
# text start at point (2,4)
plt.scatter([2],[4])
plt.text(2,4,'This text starts at point (2,4)')
plt.scatter([8],[3])
plt.text(8,3,'This text ends at point (8,3)',horizontalalignment='right')
plt.xticks(np.arange(0,10,1))
plt.yticks(np.arange(0,5,0.5))
plt.show()
import matplotlib.pyplot as plt
plt.clf()
# using some dummy data for this example
xs = np.arange(0,10,1)
ys = np.random.normal(loc=3, scale=0.4, size=10)
# 'bo-' means blue color, round points, solid lines
plt.plot(xs,ys,'bo-')
# zip joins x and y coordinates in pairs
for x,y in zip(xs,ys):
label = "{:.2f}".format(y)
plt.annotate(label, # this is the text
(x,y), # this is the point to label
textcoords="offset points", # how to position the text
xytext=(0,10), # distance from text to points (x,y)
ha='center') # horizontal alignemtn can be left, right or center
plt.xticks(np.arange(0,10,1))
plt.yticks(np.arange(0,7,0.5))
plt.show()
import matplotlib.pyplot as plt
import numpy as np
plt.clf()
# using some dummy data for this example
xs = np.arange(0,10,1)
ys = np.random.normal(loc=3, scale=0.4, size=10)
plt.bar(xs,ys)
# zip joins x and y coordinates in pairs
for x,y in zip(xs,ys):
label = "{:.2f}".format(y)
plt.annotate(label, # this is the text
(x,y), # this is the point to label
textcoords="offset points", # how to position the text
xytext=(0,10), # distance from text to points (x,y)
ha='center') # horizontal alignemtn can be left, right or center
plt.xticks(np.arange(0,10,1))
plt.yticks(np.arange(0,5,0.5))
plt.show()
import matplotlib.pyplot as plt
import numpy as np
plt.clf()
# using some dummy data for this example
xs = np.random.randint( 0, 10, size=10)
ys = np.random.randint(-5, 5, size=10)
plt.scatter(xs,ys)
# zip joins x and y coordinates in pairs
for x,y in zip(xs,ys):
label = f"({x},{y})"
plt.annotate(label, # this is the text
(x,y), # this is the point to label
textcoords="offset points", # how to position the text
xytext=(0,10), # distance from text to points (x,y)
ha='center') # horizontal alignment can be left, right or center
plt.xticks(np.arange(0,10,1))
plt.yticks(np.arange(-6,6,1))
add_grid()
plt.show()
import matplotlib.pyplot as plt
import numpy as np
plt.clf()
np.random.seed(42)
x = np.linspace(0.0,100,50)
y = np.random.uniform(low=0,high=10,size=50)
fig, (ax1, ax2) = plt.subplots(1,2)
ax1.bar(x,y)
# add text to the axes on the left only
ax1.annotate('Some test in axes ax1',
(40, 8),
color='red')
ax2.bar(x,y)
ax2.annotate('More text in axes ax2',
(40, 8),
color='black',
size=16)
plt.gcf().set_size_inches(10,5)
<Figure size 432x288 with 0 Axes>
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
# generate sample data for this example
xs = [1,2,3,4,5,6,7,8,9,10,11,12]
ys = np.random.normal(loc=3.0,size=12)
labels = ['jan','feb','mar','apr','may','jun','jul','aug','sept','oct','nov','dec']
# plot
plt.bar(xs,ys)
# apply custom tick labels
plt.xticks(xs,labels)
# this is the x-axis label you want to write the text for
string_label = "feb"
# figure out which x-axis tick corresponds to that
label_index = labels.index(string_label)
xs_index = xs[label_index]
# use xs_index to position the text
plt.annotate("foo-bar",
(xs_index,4),
color='orange',
size=15)
# helpers for visualization
plt.plot([2,2],[0,4], '--', color='grey', alpha=0.5)
plt.plot([0,2],[4,4],'--',color='grey', alpha=0.5)
plt.plot([2,2],[4,4],'o--',alpha=0.5)
plt.show()