Trong ví dụ Jupyter Notebook của chúng tôi, việc chạy ô sẽ tạo ra hình ngay bên dưới mã. Con số này cũng được bao gồm trong tài liệu Notebook để xem trong tương lai. Tuy nhiên, các môi trường Python khác như phiên Python tương tác bắt đầu từ thiết bị đầu cuối hoặc tập lệnh Python được thực thi thông qua dòng lệnh yêu cầu một lệnh bổ sung để hiển thị hình
Hướng dẫn
time = [0, 1, 2, 3]
position = [0, 100, 200, 300]
plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
9 hiển thị một hìnhplt.show[]
Lệnh này cũng có thể được sử dụng trong Notebook - ví dụ: để hiển thị nhiều hình nếu một số hình được tạo bởi một ô
Vẽ dữ liệu trực tiếp từ một plt.show[]
2
plt.show[]
- Chúng ta cũng có thể vẽ biểu đồ dữ liệu Pandas
- Điều này ngầm sử dụng
- Trước khi vẽ đồ thị, chúng tôi chuyển đổi các tiêu đề cột từ kiểu dữ liệu
4 thànhplt.show[]
5, vì chúng đại diện cho các giá trị sốplt.show[]
import pandas as pd
data = pd.read_csv['data/gapminder_gdp_oceania.csv', index_col='country']
# Extract year from last 4 characters of each column name
# The current column names are structured as 'gdpPercap_[year]',
# so we want to keep the [year] part only for clarity when plotting GDP vs. years
# To do this we use strip[], which removes from the string the characters stated in the argument
# This method works on strings, so we call str before strip[]
years = data.columns.str.strip['gdpPercap_']
# Convert year values to integers, saving results back to dataframe
data.columns = years.astype[int]
data.loc['Australia'].plot[]
Chọn và biến đổi dữ liệu, sau đó vẽ đồ thị
- Theo mặc định, biểu đồ với các hàng là trục X
- Chúng ta có thể chuyển đổi dữ liệu để vẽ nhiều chuỗi
data.T.plot[]
plt.ylabel['GDP per capita']
Nhiều kiểu cốt truyện có sẵn
- Ví dụ: thực hiện biểu đồ thanh bằng cách sử dụng phong cách lạ mắt hơn
plt.style.use['ggplot']
data.T.plot[kind='bar']
plt.ylabel['GDP per capita']
Dữ liệu cũng có thể được vẽ bằng cách gọi trực tiếp hàm time = [0, 1, 2, 3]
position = [0, 100, 200, 300]
plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
9 plt.show[]
8
time = [0, 1, 2, 3]
position = [0, 100, 200, 300]
plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
plt.show[]
- Lệnh là
9plt.show[]
- Màu sắc và định dạng của điểm đánh dấu cũng có thể được chỉ định làm đối số tùy chọn bổ sung e. g. ,
0 là đường màu xanh dương,import pandas as pd data = pd.read_csv['data/gapminder_gdp_oceania.csv', index_col='country'] # Extract year from last 4 characters of each column name # The current column names are structured as 'gdpPercap_[year]', # so we want to keep the [year] part only for clarity when plotting GDP vs. years # To do this we use strip[], which removes from the string the characters stated in the argument # This method works on strings, so we call str before strip[] years = data.columns.str.strip['gdpPercap_'] # Convert year values to integers, saving results back to dataframe data.columns = years.astype[int] data.loc['Australia'].plot[]
1 là đường nét đứt màu xanh lụcimport pandas as pd data = pd.read_csv['data/gapminder_gdp_oceania.csv', index_col='country'] # Extract year from last 4 characters of each column name # The current column names are structured as 'gdpPercap_[year]', # so we want to keep the [year] part only for clarity when plotting GDP vs. years # To do this we use strip[], which removes from the string the characters stated in the argument # This method works on strings, so we call str before strip[] years = data.columns.str.strip['gdpPercap_'] # Convert year values to integers, saving results back to dataframe data.columns = years.astype[int] data.loc['Australia'].plot[]
Nhận dữ liệu Úc từ dataframe
plt.show[]
3Có thể vẽ nhiều bộ dữ liệu cùng nhau
plt.show[]
4Thêm một huyền thoại
Thông thường, khi vẽ nhiều bộ dữ liệu trên cùng một hình, mong muốn có chú thích mô tả dữ liệu
Điều này có thể được thực hiện trong
9 trong hai giai đoạntime = [0, 1, 2, 3] position = [0, 100, 200, 300] plt.plot[time, position] plt.xlabel['Time [hr]'] plt.ylabel['Position [km]']
- Cung cấp nhãn cho từng tập dữ liệu trong hình
6plt.show[]
- Hướng dẫn
9 tạo huyền thoạitime = [0, 1, 2, 3] position = [0, 100, 200, 300] plt.plot[time, position] plt.xlabel['Time [hr]'] plt.ylabel['Position [km]']
8plt.show[]
Theo mặc định, matplotlib sẽ cố gắng đặt chú giải ở vị trí phù hợp. Nếu bạn muốn chỉ định một vị trí, điều này có thể được thực hiện với đối số
4, e. g để đặt chú thích ở góc trên bên trái của cốt truyện, chỉ địnhimport pandas as pd data = pd.read_csv['data/gapminder_gdp_oceania.csv', index_col='country'] # Extract year from last 4 characters of each column name # The current column names are structured as 'gdpPercap_[year]', # so we want to keep the [year] part only for clarity when plotting GDP vs. years # To do this we use strip[], which removes from the string the characters stated in the argument # This method works on strings, so we call str before strip[] years = data.columns.str.strip['gdpPercap_'] # Convert year values to integers, saving results back to dataframe data.columns = years.astype[int] data.loc['Australia'].plot[]
5import pandas as pd data = pd.read_csv['data/gapminder_gdp_oceania.csv', index_col='country'] # Extract year from last 4 characters of each column name # The current column names are structured as 'gdpPercap_[year]', # so we want to keep the [year] part only for clarity when plotting GDP vs. years # To do this we use strip[], which removes from the string the characters stated in the argument # This method works on strings, so we call str before strip[] years = data.columns.str.strip['gdpPercap_'] # Convert year values to integers, saving results back to dataframe data.columns = years.astype[int] data.loc['Australia'].plot[]
- Vẽ một biểu đồ phân tán tương quan với GDP của Úc và New Zealand
- Sử dụng
6 hoặcimport pandas as pd data = pd.read_csv['data/gapminder_gdp_oceania.csv', index_col='country'] # Extract year from last 4 characters of each column name # The current column names are structured as 'gdpPercap_[year]', # so we want to keep the [year] part only for clarity when plotting GDP vs. years # To do this we use strip[], which removes from the string the characters stated in the argument # This method works on strings, so we call str before strip[] years = data.columns.str.strip['gdpPercap_'] # Convert year values to integers, saving results back to dataframe data.columns = years.astype[int] data.loc['Australia'].plot[]
7import pandas as pd data = pd.read_csv['data/gapminder_gdp_oceania.csv', index_col='country'] # Extract year from last 4 characters of each column name # The current column names are structured as 'gdpPercap_[year]', # so we want to keep the [year] part only for clarity when plotting GDP vs. years # To do this we use strip[], which removes from the string the characters stated in the argument # This method works on strings, so we call str before strip[] years = data.columns.str.strip['gdpPercap_'] # Convert year values to integers, saving results back to dataframe data.columns = years.astype[int] data.loc['Australia'].plot[]
time = [0, 1, 2, 3]
position = [0, 100, 200, 300]
plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
0time = [0, 1, 2, 3]
position = [0, 100, 200, 300]
plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
1cực tiểu và cực đại
Điền vào chỗ trống dưới đây để vẽ biểu đồ GDP bình quân đầu người tối thiểu theo thời gian cho tất cả các quốc gia ở Châu Âu. Sửa đổi nó một lần nữa để vẽ GDP bình quân đầu người tối đa theo thời gian cho Châu Âu
2time = [0, 1, 2, 3] position = [0, 100, 200, 300] plt.plot[time, position] plt.xlabel['Time [hr]'] plt.ylabel['Position [km]']
Dung dịch
3time = [0, 1, 2, 3] position = [0, 100, 200, 300] plt.plot[time, position] plt.xlabel['Time [hr]'] plt.ylabel['Position [km]']
tương quan
Sửa đổi ví dụ trong ghi chú để tạo biểu đồ phân tán thể hiện mối quan hệ giữa GDP bình quân đầu người tối thiểu và tối đa giữa các quốc gia ở Châu Á cho mỗi năm trong bộ dữ liệu. Bạn thấy mối quan hệ nào [nếu có]?
Dung dịch
4time = [0, 1, 2, 3] position = [0, 100, 200, 300] plt.plot[time, position] plt.xlabel['Time [hr]'] plt.ylabel['Position [km]']
Không có mối tương quan cụ thể nào có thể được nhìn thấy giữa giá trị gdp tối thiểu và tối đa hàng năm. Có vẻ như vận mệnh của các nước châu Á không thăng trầm cùng nhau
Bạn có thể lưu ý rằng độ biến thiên ở mức tối đa cao hơn nhiều so với mức tối thiểu. Hãy xem các chỉ số tối đa và tối đa
5time = [0, 1, 2, 3] position = [0, 100, 200, 300] plt.plot[time, position] plt.xlabel['Time [hr]'] plt.ylabel['Position [km]']
Dung dịch
Có vẻ như sự thay đổi trong giá trị này là do sự sụt giảm mạnh sau năm 1972. Một số địa chính trị chơi có lẽ?
Tương quan nhiều hơn
Chương trình ngắn này tạo ra một biểu đồ thể hiện mối tương quan giữa GDP và tuổi thọ cho năm 2007, chuẩn hóa kích thước điểm đánh dấu theo dân số
6time = [0, 1, 2, 3] position = [0, 100, 200, 300] plt.plot[time, position] plt.xlabel['Time [hr]'] plt.ylabel['Position [km]']
Sử dụng trợ giúp trực tuyến và các tài nguyên khác, giải thích ý nghĩa của từng đối số với
8plt.show[]
Dung dịch
Một nơi tốt để xem là tài liệu về chức năng cốt truyện - help[data_all. kịch bản]
loại - Như đã thấy, điều này xác định loại cốt truyện sẽ được vẽ
x và y - Tên cột hoặc chỉ mục xác định dữ liệu nào sẽ được đặt trên trục x và y của biểu đồ
s - Chi tiết về điều này có thể được tìm thấy trong tài liệu của plt. tiêu tan. Một số hoặc một giá trị cho mỗi điểm dữ liệu. Xác định kích thước của các điểm được vẽ
Lưu cốt truyện của bạn vào một tập tin
Nếu bạn hài lòng với cốt truyện bạn thấy, bạn có thể muốn lưu nó vào một tệp, có lẽ để đưa nó vào một ấn phẩm. Có một chức năng trong matplotlib. mô-đun pyplot thực hiện điều này. tiết kiệm. Gọi chức năng này, e. g. với
7time = [0, 1, 2, 3] position = [0, 100, 200, 300] plt.plot[time, position] plt.xlabel['Time [hr]'] plt.ylabel['Position [km]']
sẽ lưu hình hiện tại vào tệp
9. Định dạng tệp sẽ tự động được suy ra từ phần mở rộng tên tệp [các định dạng khác là pdf, ps, eps và svg]import pandas as pd data = pd.read_csv['data/gapminder_gdp_oceania.csv', index_col='country'] # Extract year from last 4 characters of each column name # The current column names are structured as 'gdpPercap_[year]', # so we want to keep the [year] part only for clarity when plotting GDP vs. years # To do this we use strip[], which removes from the string the characters stated in the argument # This method works on strings, so we call str before strip[] years = data.columns.str.strip['gdpPercap_'] # Convert year values to integers, saving results back to dataframe data.columns = years.astype[int] data.loc['Australia'].plot[]
Lưu ý rằng các hàm trong
0 đề cập đến một biến số toàn cục và sau khi một số đã được hiển thị trên màn hình [e. g. vớidata.T.plot[] plt.ylabel['GDP per capita']
1] matplotlib sẽ làm cho biến này tham chiếu đến một hình trống mới. Do đó, hãy đảm bảo rằng bạn gọidata.T.plot[] plt.ylabel['GDP per capita']
2 trước khi biểu đồ được hiển thị trên màn hình, nếu không, bạn có thể tìm thấy một tệp có biểu đồ trốngdata.T.plot[] plt.ylabel['GDP per capita']
Khi sử dụng khung dữ liệu, dữ liệu thường được tạo và được vẽ trên màn hình trong một dòng và
2 dường như không phải là một cách tiếp cận khả thi. Một khả năng để lưu hình vào tệp là sau đódata.T.plot[] plt.ylabel['GDP per capita']
- lưu một tham chiếu đến hình hiện tại trong một biến cục bộ [với
4]data.T.plot[] plt.ylabel['GDP per capita']
- gọi phương thức lớp
5 từ biến đódata.T.plot[] plt.ylabel['GDP per capita']
8time = [0, 1, 2, 3] position = [0, 100, 200, 300] plt.plot[time, position] plt.xlabel['Time [hr]'] plt.ylabel['Position [km]']
Làm cho các lô của bạn có thể truy cập được
Bất cứ khi nào bạn tạo các sơ đồ để đưa vào một bài báo hoặc một bài thuyết trình, bạn có thể thực hiện một số điều để đảm bảo rằng mọi người có thể hiểu được các sơ đồ của bạn