EventCollection
Use EventCollections
to mark the locations of the x
and y
data points on the respective axes for each curve.
import matplotlib.pyplot as plt
from matplotlib.collections import EventCollection
import numpy as np
np.random.seed(10)
# create random data
xdata = np.random.random([2, 10]) # 2 x 10 matrix with each element in (0, 1)
# split x
x1, x2 = xdata[0, :], xdata[1, :]
# sort
x1.sort()
x2.sort()
# y data points
y1, y2 = x1**2, 1-x2**3
# plot
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(x1, y1, color='tab:blue')
ax.plot(x2, y2, color='tab:orange')
# events marking x
xe1 = EventCollection(x1, color='tab:blue', linelength=0.05)
xe2 = EventCollection(x2, color='tab:orange', linelength=0.05)
# events marking y
ye1 = EventCollection(y1, color='tab:blue', linelength=0.05, orientation='vertical')
ye2 = EventCollection(y2, color='tab:orange', linelength=0.05, orientation='vertical')
# add events
ax.add_collection(xe1)
ax.add_collection(xe2)
ax.add_collection(ye1)
ax.add_collection(ye2)
# set limits
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.set_title('line plot with data points')
plt.show()
Boxplot Demo
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(10)
# fake data
spread = np.random.rand(50)*100
center = np.ones(25)*50
flier_high = np.random.rand(10)*100+100
flier_low = np.random.rand(10)*(-100)
data = np.concatenate((spread, center, flier_high, flier_low))
fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3,sharey=True)
ax1.set_title('Basic Plot')
ax1.boxplot(data)
ax2.set_title('Notched boxes')
ax2.boxplot(data, notch=True)
green_diamond = dict(markerfacecolor='g', marker='D')
ax3.set_title('Outlier Symbols')
ax3.boxplot(data, flierprops=green_diamond)
plt.show()