PYB l3 python in data analytics (matplotlib)

33 2 0
PYB l3 python in data analytics (matplotlib)

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Microsoft PowerPoint PYB L3 Python in Data Analytics pptx PYTHON IN DATA ANALYTICS Lesson 3 Matplotlib MR LE TRUNG TH UC Matplotlib  Matplotlib là một thư viện trực quan hoá dữ liệu phổ biến trong Py.

PYTHON IN DATA ANALYTICS Lesson Matplotlib M R L E T RU NG T H U C Matplotlib  Matplotlib thư viện trực quan hoá liệu phổ biến Python  Nó vẽ nhiều loại đồ thị khác nhau, hữu ích làm việc với NumPy CODEFUN.COM.VN  Gồm phần là: Figure & Axes  Figure: cửa sổ chứa tất thứ vẽ Nó chứa nhiều thành phần độc lập như: Axes khác nhau, tiêu đề, thích…  Axes: khu vực chứa liệu vẽ lên tiêu đề, thích gắn với CODEFUN.COM.VN CODEFUN.COM.VN  Import thư viện Matplotlib: CODEFUN.COM.VN plt.figure(): tạo figure plt.plot(): vẽ đồ thị với giá trị trục x so với trục y plt.xlabel(): đặt tên cho trục x plt.ylabel(): đặt tên cho trục y plt.title(): đặt tên cho figure plt.grid(): hiển thị đường lưới plt.legend(): hiển thị thích cho đồ thị plt.savefig(): lưu hình máy plt.show(): hiển thị hình vẽ plt.clf(): xố hình (hữu ích có nhiều figure chương trình) CODEFUN.COM.VN  Vẽ đồ thị hàm số 𝑦 = 𝑥 CODEFUN.COM.VN  Matplotlib cịn cho vẽ nhiều hình Figure, sử dụng phương thức subplot() gồm có thơng số cụ thể:  nrows: số lượng hàng Figure  ncols: số lượng cột Figure  plot_number: vị trí plot Figure CODEFUN.COM.VN Figure  Thành phần figure axes Một figure chứa nhiều axes Hay nói cách khác, figure khung chứa, axes thật nơi hình vẽ lên  Như vậy, khơng có cách gọi kiểu như:  f̶̶i̶g̶.̶p̶l̶o̶t̶(̶)  f̶i̶ ̶g̶.̶x̶l̶a̶b̶e̶l̶()̶  CODEFUN.COM.VN  Khởi tạo đối tượng figure: CODEFUN.COM.VN 10  Chỉnh lại trục CODEFUN.COM.VN 19 Vẽ đồ thị rời rạc Vẽ đồ thị kiểu điểm: Trong nhiều trường hợp, muốn hiển thị điểm rời rạc thay đường liên tục, việc chuyển đường thẳng sang điểm sử dụng thêm marker plot()  Các marker thông dụng: CODEFUN.COM.VN 20 CODEFUN.COM.VN 21  Vẽ đồ thị rời rạc:  Có thể vừa vẽ điểm, vừa đường cách sử dụng cách phương pháp sau:  Phương pháp 1: Nối điểm CODEFUN.COM.VN 22  Các loại linestyle: CODEFUN.COM.VN 23  Phương pháp 2: Vẽ vài điểm đường CODEFUN.COM.VN 24 Một số loại biểu đồ khác  Biểu đồ pie (tỷ lệ)  plt.pie() import matplotlib.pyplot as plotter # The slice names of a population distribution pie chart pieLabels = 'Asia', 'Africa', 'Europe', 'North America', 'South America' # Population data populationShare = [59.69, 16, 9.94, 7.79, 5.68] figureObject, axesObject = plotter.subplots() # Draw the pie chart axesObject.pie(populationShare, labels=pieLabels, autopct='%1.2f', startangle=90) # Aspect ratio - equal means pie is a circle axesObject.axis('equal') plotter.show() CODEFUN.COM.VN 25 import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal")) recipe = ["375 g flour", "75 g sugar", "250 g butter", "300 g berries"] data = [float(x.split()[0]) for x in recipe] ingredients = [x.split()[-1] for x in recipe] def func(pct, allvals): absolute = int(pct/100.*np.sum(allvals)) return "{:.1f}%\n({:d} g)".format(pct, absolute) wedges, texts, autotexts = ax.pie(data, autopct=lambda pct: func(pct, data), textprops=dict(color="w")) ax.legend(wedges, ingredients, title="Ingredients", loc="center left", bbox_to_anchor=(1, 0, 0.5, 1)) plt.setp(autotexts, size=8, weight="bold") ax.set_title("Matplotlib_A pie chart") plt.show() CODEFUN.COM.VN 26 import numpy as np import matplotlib.pyplot as plotter  Biểu đồ cột:  plt.bar() numberOfYears = # Number of categories, variables averageReturns = (13, 15, 15, 14, 12) # Frequency of the variable standardDeviation = (3, 2, 2, 3, 1) # Error figureObejct, axesObject = plotter.subplots() # X Axis categories categories = np.arange(numberOfYears) # Customize bar properties barWidth = 0.4 barOpacity = 0.5 errorConfig = {'ecolor': '0.2'} # Draw the bar chart depicting annual stock market returns with standard deviation barChart = plotter.bar(categories, averageReturns, barWidth, alpha=barOpacity, color='green', yerr=standardDeviation, error_kw=errorConfig, label='Returns') # Fill the bar chart with information plotter.xlabel('Year') plotter.ylabel('Average returns from Stock Market %') plotter.title('Annual Stock Market Returns %') plotter.xticks(categories, ('2012', '2013', '2014', '2015', '2016')) plotter.legend() plotter.tight_layout() plotter.show() CODEFUN.COM.VN 27 Thực hành  Cho file liệu Kinh doanh sales_dataset.csv Hãy kết hợp numpy, pandas matplotlib để xử lý tập sau 1) Vẽ đồ thị doanh thu (profit) theo tháng Cho kết hình dưới: CODEFUN.COM.VN 28 2) Tính tổng lợi nhuận hiển thị theo biểu đồ điểm có thuộc tính hình CODEFUN.COM.VN 29 3) Đọc hiển thị tổng số sản phẩm loại bán theo dòng, hiển thị nhiều biểu đồ figure CODEFUN.COM.VN 30 4) Biểu diễn số lượng bán sản phẩm toothpaste tháng theo biểu đồ dạng điểm (sử dụng marker color tùy thích) CODEFUN.COM.VN 31 5) Đọc liệu sản phẩm face cream facewash, so sánh số lượng bán tháng theo biểu đồ cột CODEFUN.COM.VN 32 6) Vẽ biểu đồ tỷ lệ thống kê theo loại sản phẩm bán năm CODEFUN.COM.VN 33 ... "250 g butter", "300 g berries"] data = [float(x.split()[0]) for x in recipe] ingredients = [x.split()[-1] for x in recipe] def func(pct, allvals): absolute = int(pct/100.*np.sum(allvals)) return... absolute) wedges, texts, autotexts = ax.pie (data, autopct=lambda pct: func(pct, data) , textprops=dict(color="w")) ax.legend(wedges, ingredients, title="Ingredients", loc="center left", bbox_to_anchor=(1,... fig>) CODEFUN.COM.VN 17 Chỉnh trục toạ độ Figure  Hay gọi Spines Ticks đồ thị  Axis spine - đường ghi nhận ranh giới khu vực liệu  Spine đường kết nối dấu ticks trục ghi nhận ranh giới khu vực

Ngày đăng: 11/09/2022, 23:01

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan