Bản đồ màu Python trên hình cầu

Vẽ mũi tên trong Matplotlib thường khó hơn nhiều so với bạn mong đợi. Mặc dù có sẵn hàm plt.arrow() nhưng tôi không khuyên bạn nên sử dụng nó; . Thay vào đó, tôi khuyên bạn nên sử dụng hàm plt.annotate(). Hàm này tạo một số văn bản và mũi tên, và các mũi tên có thể được chỉ định rất linh hoạt

Ở đây, chúng tôi sẽ sử dụng annotate với một số tùy chọn của nó (Hình 4-71)

In[7]: %matplotlib inline fig, ax = plt.subplots() x = np.linspace(0, 20, 1000) ax.plot(x, np.cos(x)) ax.axis('equal') ax.annotate('local maximum', xy=(6.28, 1), xytext=(10, 4), arrowprops=dict(facecolor='black', shrink=0.05)) ax.annotate('local minimum', xy=(5 * np.pi, -1), xytext=(2, -6), arrowprops=dict(arrowstyle="->", connectionstyle="angle3,angleA=0,angleB=-90"));

Hình 4-71. Ví dụ về chú thích

Kiểu mũi tên được điều khiển thông qua từ điển arrowprops, có sẵn nhiều tùy chọn. Các tùy chọn này được ghi lại khá đầy đủ trong tài liệu trực tuyến của Matplotlib, vì vậy thay vì lặp lại chúng ở đây, tôi sẽ trình bày nhanh một số khả năng. Hãy minh họa một số tùy chọn khả thi bằng cách sử dụng biểu đồ tỷ lệ sinh ở phần trước (Hình 4-72)

In[8]: fig, ax = plt.subplots(figsize=(12, 4)) births_by_date.plot(ax=ax) # Add labels to the plot ax.annotate("New Year's Day", xy=('2012-1-1', 4100), xycoords='data', xytext=(50, -30), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=-0.2")) ax.annotate("Independence Day", xy=('2012-7-4', 4250), xycoords='data', bbox=dict(boxstyle="round", fc="none", ec="gray"), xytext=(10, -40), textcoords='offset points', ha='center', arrowprops=dict(arrowstyle="->")) ax.annotate('Labor Day', xy=('2012-9-4', 4850), xycoords='data', ha='center', xytext=(0, -20), textcoords='offset points') ax.annotate('', xy=('2012-9-1', 4850), xytext=('2012-9-7', 4850), xycoords='data', textcoords='data', arrowprops={'arrowstyle': '|-|,widthA=0.2,widthB=0.2', }) ax.annotate('Halloween', xy=('2012-10-31', 4600), xycoords='data', xytext=(-80, -40), textcoords='offset points', arrowprops=dict(arrowstyle="fancy", fc="0.6", ec="none", connectionstyle="angle3,angleA=0,angleB=-90")) ax.annotate('Thanksgiving', xy=('2012-11-25', 4500), xycoords='data', xytext=(-120, -60), textcoords='offset points', bbox=dict(boxstyle="round4,pad=.5", fc="0.9"), arrowprops=dict(arrowstyle="->", connectionstyle="angle,angleA=0,angleB=80,rad=20")) ax.annotate('Christmas', xy=('2012-12-25', 3850), xycoords='data', xytext=(-30, 0), textcoords='offset points', size=13, ha='right', va="center", bbox=dict(boxstyle="round", alpha=0.1), arrowprops=dict(arrowstyle="wedge,tail_width=0.5", alpha=0.1)); # Label the axes ax.set(title='USA births by day of year (1969-1988)', ylabel='average daily births') # Format the x axis with centered month labels ax.xaxis.set_major_locator(mpl.dates.MonthLocator()) ax.xaxis.set_minor_locator(mpl.dates.MonthLocator(bymonthday=15)) ax.xaxis.set_major_formatter(plt.NullFormatter()) ax.xaxis.set_minor_formatter(mpl.dates.DateFormatter('%h')); ax.set_ylim(3600, 5400);

Hình 4-72. Chú thích tỷ lệ sinh trung bình theo ngày

Bạn sẽ nhận thấy rằng thông số kỹ thuật của các mũi tên và hộp văn bản rất chi tiết. điều này mang lại cho bạn sức mạnh để tạo ra gần như bất kỳ kiểu mũi tên nào bạn muốn. Thật không may, điều đó cũng có nghĩa là các loại tính năng này thường phải được điều chỉnh thủ công, một quá trình có thể rất tốn thời gian khi sản xuất đồ họa chất lượng xuất bản. Cuối cùng, tôi sẽ lưu ý rằng sự kết hợp các phong cách trước đây hoàn toàn không phải là phương pháp hay nhất để trình bày dữ liệu, mà được đưa vào như một minh họa cho một số tùy chọn có sẵn

Có thể tìm thấy thêm các cuộc thảo luận và ví dụ về các kiểu chú thích và mũi tên có sẵn trong thư viện Matplotlib, cụ thể là http. // matplotlib. org/ví dụ/pylab_examples/annotation_demo2. html

Chủ đề