|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- Coding:utf-8 -*- |
| 3 | +'''Plot two curves, then use EventCollections to mark the locations of the x |
| 4 | +and y data points on the respective axes for each curve''' |
| 5 | + |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +from matplotlib.collections import EventCollection |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +# create random data |
| 11 | +np.random.seed(50) |
| 12 | +xdata = np.random.random([2, 10]) |
| 13 | + |
| 14 | +# split the data into two parts |
| 15 | +xdata1 = xdata[0, :] |
| 16 | +xdata2 = xdata[1, :] |
| 17 | + |
| 18 | +# sort the data so it makes clean curves |
| 19 | +xdata1.sort() |
| 20 | +xdata2.sort() |
| 21 | + |
| 22 | +# create some y data points |
| 23 | +ydata1 = xdata1 ** 2 |
| 24 | +ydata2 = 1 - xdata2 ** 3 |
| 25 | + |
| 26 | +# plot the data |
| 27 | +fig = plt.figure() |
| 28 | +ax = fig.add_subplot(1, 1, 1) |
| 29 | +ax.plot(xdata1, ydata1, 'r', xdata2, ydata2, 'b') |
| 30 | + |
| 31 | +# create the events marking the x data points |
| 32 | +xevents1 = EventCollection(xdata1, color=[1, 0, 0], linelength=0.05) |
| 33 | +xevents2 = EventCollection(xdata2, color=[0, 0, 1], linelength=0.05) |
| 34 | + |
| 35 | +# create the events marking the y data points |
| 36 | +yevents1 = EventCollection(ydata1, color=[1, 0, 0], linelength=0.05, |
| 37 | + orientation='vertical') |
| 38 | +yevents2 = EventCollection(ydata2, color=[0, 0, 1], linelength=0.05, |
| 39 | + orientation='vertical') |
| 40 | + |
| 41 | +# add the events to the axis |
| 42 | +ax.add_collection(xevents1) |
| 43 | +ax.add_collection(xevents2) |
| 44 | +ax.add_collection(yevents1) |
| 45 | +ax.add_collection(yevents2) |
| 46 | + |
| 47 | +# set the limits |
| 48 | +ax.set_xlim([0, 1]) |
| 49 | +ax.set_ylim([0, 1]) |
| 50 | + |
| 51 | +ax.set_title('line plot with data points') |
| 52 | + |
| 53 | +# display the plot |
| 54 | +fig.show() |
0 commit comments