Làm thế nào để bạn vẽ hai biến trên biểu đồ trong python?

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ình

plt.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

  • 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
    plt.show[]
    
    4 thành
    plt.show[]
    
    5, vì chúng đại diện cho các giá trị số

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

  • Lệnh là
    plt.show[]
    
    9
  • 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. ,
    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[]
    
    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ục

Nhận dữ liệu Úc từ dataframe

plt.show[]
3

Có thể vẽ nhiều bộ dữ liệu cùng nhau

plt.show[]
4

Thê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

time = [0, 1, 2, 3]
position = [0, 100, 200, 300]

plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
9 trong hai giai đoạn

  • Cung cấp nhãn cho từng tập dữ liệu trong hình

plt.show[]
6

  • 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 tạo huyền thoại

plt.show[]
8

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ố

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[]
4, e. g để đặt chú thích ở góc trên bên trái của cốt truyện, chỉ định
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[]
5

  • Vẽ một biểu đồ phân tán tương quan với GDP của Úc và New Zealand
  • Sử 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[]
    
    6 hoặc
    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[]
    
    7

time = [0, 1, 2, 3]
position = [0, 100, 200, 300]

plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
0

time = [0, 1, 2, 3]
position = [0, 100, 200, 300]

plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
1

cự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

time = [0, 1, 2, 3]
position = [0, 100, 200, 300]

plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
2

Dung dịch

time = [0, 1, 2, 3]
position = [0, 100, 200, 300]

plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
3

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

time = [0, 1, 2, 3]
position = [0, 100, 200, 300]

plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
4

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

time = [0, 1, 2, 3]
position = [0, 100, 200, 300]

plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
5

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ố

time = [0, 1, 2, 3]
position = [0, 100, 200, 300]

plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
6

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

plt.show[]
8

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

time = [0, 1, 2, 3]
position = [0, 100, 200, 300]

plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
7

sẽ lưu hình hiện tại vào tệp

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[]
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]

Lưu ý rằng các hàm trong

data.T.plot[]
plt.ylabel['GDP per capita']
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ới
data.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ọi
data.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ống

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à

data.T.plot[]
plt.ylabel['GDP per capita']
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 đó

  • lưu một tham chiếu đến hình hiện tại trong một biến cục bộ [với
    data.T.plot[]
    plt.ylabel['GDP per capita']
    
    4]
  • gọi phương thức lớp
    data.T.plot[]
    plt.ylabel['GDP per capita']
    
    5 từ biến đó

time = [0, 1, 2, 3]
position = [0, 100, 200, 300]

plt.plot[time, position]
plt.xlabel['Time [hr]']
plt.ylabel['Position [km]']
8

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

Chủ Đề