Computer >> Máy Tính >  >> Lập trình >> Python

Python Pandas - Tạo một Bar Plot và tạo kiểu cho các thanh trong Seaborn

Bar Plot trong Seaborn được sử dụng để hiển thị các ước tính điểm và khoảng tin cậy dưới dạng các thanh hình chữ nhật. Seaborn.barplot () được sử dụng. Tạo kiểu cho các thanh bằng màu mặt , độ rộng dòng edgecolor tham số.

Giả sử sau đây là tập dữ liệu của chúng tôi ở dạng tệp CSV −Cricketers2.csv

Đầu tiên, hãy nhập các thư viện được yêu cầu -

import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt

Tải dữ liệu từ tệp CSV vào Pandas DataFrame -

dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\Cricketers2.csv")

Thiết kế các thanh -

sb.barplot(x=dataFrame["Role"], y=dataFrame["Matches"], facecolor=(1, 1, 0, 0), linewidth=4, edgecolor=sb.color_palette("dark", 2))

Ví dụ

Sau đây là mã -

import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt

# Load data from a CSV file into a Pandas DataFrame
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\Cricketers2.csv")

# plotting bar plot with Role and Matches column
# designing the bars
sb.barplot(x=dataFrame["Role"], y=dataFrame["Matches"], facecolor=(1, 1, 0, 0), linewidth=4, edgecolor=sb.color_palette("dark", 2))

# display
plt.show()

Đầu ra

Điều này sẽ tạo ra kết quả sau -

Python Pandas - Tạo một Bar Plot và tạo kiểu cho các thanh trong Seaborn