trực quan hóa tập dữ liệu cho chúng ta cái nhìn tổng thể về dữ liệu. Nó đưa ra các mô tả thống kê khác nhau về dữ liệu Vì vậy, đây là công thức về cách chúng ta có thể tạo biểu đồ BAR bằng cách sử dụng DataFrame của gấu trúc. Bước 1 - Nhập thư việnimport pandas as pd import matplotlib.pyplot as plt import numpy as np Chúng tôi chỉ nhập gấu trúc, numpy và matplotlib. pyplot cần thiết Bước 2 - Tạo DataFrameChúng tôi đã tạo Từ điển và chuyển nó qua pd. DataFrame để tạo khung dữ liệu với các tính năng khác nhau. raw_data = {"first_name": ["Jason", "Molly", "Tina", "Jake", "Amy"], "pre_score": [4, 24, 31, 2, 3], "mid_score": [25, 94, 57, 62, 70], "post_score": [5, 43, 23, 23, 51]} df = pd.DataFrame(raw_data, columns = ["first_name", "pre_score", "mid_score", "post_score"]) print(df) Bước 3 - Tạo biểu đồ thanhChúng tôi đã thực hiện các bước khác nhau để vẽ biểu đồ thanh. Đầu tiên, chúng tôi đã gán nhãn cho thanh, sau đó là vị trí y và ngang của thanh. Biểu đồ thanh được vẽ bởi hàm plt. tắm và cuối cùng dán nhãn trục x, y và đồ thị. Molly = df.ix[1, 1:] Tina = df.ix[2, 1:] bar_labels = ["Pre Score", "Mid Score", "Post Score"] plt.figure(figsize=(8,6)) y_pos = np.arange(len(Molly)) y_pos = [x for x in y_pos] plt.yticks(y_pos, bar_labels, fontsize=10) plt.barh(y_pos, Molly, align="center", alpha=0.4, color="#263F13") plt.barh(y_pos, -Tina, align="center", alpha=0.4, color="#77A61D") plt.xlabel("Tina"s Score: Light Green. Molly"s Score: Dark Green") plt.title("Comparison of Molly and Tina"s Score") plt.ylim([-1,len(Molly)+0.1]) plt.xlim([-max(Tina)-10, max(Tina)+10]) plt.grid(); plt.show() Vì vậy, đầu ra là Python là một ngôn ngữ tuyệt vời để thực hiện phân tích dữ liệu, chủ yếu là do hệ sinh thái tuyệt vời của các gói python tập trung vào dữ liệu. Pandas là một trong những gói đó và giúp việc nhập và phân tích dữ liệu dễ dàng hơn nhiều Pandas DataFrame.plot.bar() vẽ biểu đồ theo chiều dọc ở dạng thanh hình chữ nhật cú pháp. Khung dữ liệu. kịch bản. thanh(x=Không, y=Không, **kwds) Thông số. x. (nhãn hoặc vị trí, tùy chọn) Cho phép vẽ biểu đồ của một cột so với cột khác. Nếu không được chỉ định, chỉ mục của DataFrame được sử dụng. y. (nhãn hoặc vị trí, tùy chọn) Cho phép vẽ biểu đồ của một cột so với cột khác. Nếu không được chỉ định, tất cả các cột số được sử dụng. **kwds. Đối số từ khóa bổ sung trả lại. matplotlib. trục. Trục hoặc np. ndarray của họ
Ví dụ 1. Sử dụng DataFrame.plot.bar() để vẽ biểu đồ theo chiều dọc ở dạng thanh hình chữ nhật
# importing matplotlib
import matplotlib.pyplot
________số 8 # importing pandas as pd
import raw_data = {"first_name": ["Jason", "Molly", "Tina", "Jake", "Amy"], "pre_score": [4, 24, 31, 2, 3], "mid_score": [25, 94, 57, 62, 70], "post_score": [5, 43, 23, 23, 51]} df = pd.DataFrame(raw_data, columns = ["first_name", "pre_score", "mid_score", "post_score"]) print(df) 1
________số 8 DataFrame.plot.bar() 0
import DataFrame.plot.bar() 2
________số 8 DataFrame.plot.bar() 4
DataFrame.plot.bar() 5DataFrame.plot.bar() 6 DataFrame.plot.bar() 7DataFrame.plot.bar() 8DataFrame.plot.bar() 9# importing matplotlib 0# importing matplotlib 1DataFrame.plot.bar() 6# importing matplotlib 3# importing matplotlib 4DataFrame.plot.bar() 9# importing matplotlib 6DataFrame.plot.bar() 9# importing matplotlib 8# importing matplotlib 9
________số 8 import 1import 2
Bây giờ chúng ta sẽ sử dụng hàm DataFrame.plot.bar() để vẽ biểu đồ theo chiều dọc ở dạng thanh hình chữ nhật
import 4
import 5
Đầu ra.
Ví dụ #2. Sử dụng DataFrame.plot.bar() để vẽ biểu đồ theo chiều dọc ở dạng thanh hình chữ nhật.
# importing matplotlib
import matplotlib.pyplot
________số 8 # importing pandas as pd
import raw_data = {"first_name": ["Jason", "Molly", "Tina", "Jake", "Amy"], "pre_score": [4, 24, 31, 2, 3], "mid_score": [25, 94, 57, 62, 70], "post_score": [5, 43, 23, 23, 51]} df = pd.DataFrame(raw_data, columns = ["first_name", "pre_score", "mid_score", "post_score"]) print(df) 1
________số 8 DataFrame.plot.bar() 0
import DataFrame.plot.bar() 2
________số 8 matplotlib.pyplot 9
DataFrame.plot.bar() 5DataFrame.plot.bar() 6 DataFrame.plot.bar() 7DataFrame.plot.bar() 8DataFrame.plot.bar() 9DataFrame.plot.bar() 8 6
7 8DataFrame.plot.bar() 6# importing matplotlib 3# importing matplotlib 4DataFrame.plot.bar() 9# importing matplotlib 6DataFrame.plot.bar() 9# importing matplotlib 8DataFrame.plot.bar() 9# importing pandas as pd 7DataFrame.plot.bar() 9# importing pandas as pd 9DataFrame.plot.bar() 9import 1DataFrame.plot.bar() 9import 3DataFrame.plot.bar() 9import 5DataFrame.plot.bar() 9import 7DataFrame.plot.bar() 9import 9# importing matplotlib 9 |