Cách vẽ nhiều biểu đồ trong Python bằng vòng lặp for

Một trong những ứng dụng tốt nhất của vòng lặp là tạo nhiều biểu đồ một cách nhanh chóng và dễ dàng. Hãy sử dụng một vòng lặp để tạo 4 ô biểu diễn dữ liệu từ một bài kiểm tra gồm 4 câu hỏi. Dữ liệu được biểu diễn dưới dạng ma trận có 100 hàng (đại diện cho 100 người khác nhau) và 4 cột biểu thị điểm số cho các câu hỏi khác nhau. Dữ liệu được lưu trữ trong gói

par(mfrow = c(2, 2))  # Set up a 2 x 2 plotting space

# Create the loop.vector (all the columns)
loop.vector <- 1:4

for (i in loop.vector) { # Loop over loop.vector

  # store data in column.i as x
  x <- examscores[,i]
  
  # Plot histogram of x
  hist(x,
       main = paste("Question", i),
       xlab = "Scores",
       xlim = c(0, 100))
}
0 trong một đối tượng có tên là
par(mfrow = c(2, 2))  # Set up a 2 x 2 plotting space

# Create the loop.vector (all the columns)
loop.vector <- 1:4

for (i in loop.vector) { # Loop over loop.vector

  # store data in column.i as x
  x <- examscores[,i]
  
  # Plot histogram of x
  hist(x,
       main = paste("Question", i),
       xlab = "Scores",
       xlim = c(0, 100))
}
1. Đây là giao diện của một vài hàng đầu tiên của dữ liệu

# First few rows of the examscores data
head(examscores)
##    a  b  c  d
## 1 43 31 68 34
## 2 61 27 56 39
## 3 37 41 74 46
## 4 54 36 62 41
## 5 56 34 82 40
## 6 73 29 79 35

Bây giờ, chúng tôi sẽ lặp qua các cột và tạo biểu đồ dữ liệu trong mỗi cột. Đầu tiên, tôi sẽ thiết lập không gian vẽ sơ đồ 2 x 2 với

par(mfrow = c(2, 2))  # Set up a 2 x 2 plotting space

# Create the loop.vector (all the columns)
loop.vector <- 1:4

for (i in loop.vector) { # Loop over loop.vector

  # store data in column.i as x
  x <- examscores[,i]
  
  # Plot histogram of x
  hist(x,
       main = paste("Question", i),
       xlab = "Scores",
       xlim = c(0, 100))
}
2 (Nếu bạn chưa từng xem
par(mfrow = c(2, 2))  # Set up a 2 x 2 plotting space

# Create the loop.vector (all the columns)
loop.vector <- 1:4

for (i in loop.vector) { # Loop over loop.vector

  # store data in column.i as x
  x <- examscores[,i]
  
  # Plot histogram of x
  hist(x,
       main = paste("Question", i),
       xlab = "Scores",
       xlim = c(0, 100))
}
2 trước đây, chỉ cần biết rằng nó cho phép bạn đặt nhiều ô cạnh nhau). Tiếp theo, tôi sẽ định nghĩa
par(mfrow = c(2, 2))  # Set up a 2 x 2 plotting space

# Create the loop.vector (all the columns)
loop.vector <- 1:4

for (i in loop.vector) { # Loop over loop.vector

  # store data in column.i as x
  x <- examscores[,i]
  
  # Plot histogram of x
  hist(x,
       main = paste("Question", i),
       xlab = "Scores",
       xlim = c(0, 100))
}
4 là
par(mfrow = c(2, 2))  # Set up a 2 x 2 plotting space

# Create the loop.vector (all the columns)
loop.vector <- 1:4

for (i in loop.vector) { # Loop over loop.vector

  # store data in column.i as x
  x <- examscores[,i]
  
  # Plot histogram of x
  hist(x,
       main = paste("Question", i),
       xlab = "Scores",
       xlim = c(0, 100))
}
5 và
par(mfrow = c(2, 2))  # Set up a 2 x 2 plotting space

# Create the loop.vector (all the columns)
loop.vector <- 1:4

for (i in loop.vector) { # Loop over loop.vector

  # store data in column.i as x
  x <- examscores[,i]
  
  # Plot histogram of x
  hist(x,
       main = paste("Question", i),
       xlab = "Scores",
       xlim = c(0, 100))
}
6 là các số nguyên từ 1 đến 4 với
par(mfrow = c(2, 2))  # Set up a 2 x 2 plotting space

# Create the loop.vector (all the columns)
loop.vector <- 1:4

for (i in loop.vector) { # Loop over loop.vector

  # store data in column.i as x
  x <- examscores[,i]
  
  # Plot histogram of x
  hist(x,
       main = paste("Question", i),
       xlab = "Scores",
       xlim = c(0, 100))
}
7. Trong
par(mfrow = c(2, 2))  # Set up a 2 x 2 plotting space

# Create the loop.vector (all the columns)
loop.vector <- 1:4

for (i in loop.vector) { # Loop over loop.vector

  # store data in column.i as x
  x <- examscores[,i]
  
  # Plot histogram of x
  hist(x,
       main = paste("Question", i),
       xlab = "Scores",
       xlim = c(0, 100))
}
0, tôi đã lưu trữ dữ liệu trong cột
par(mfrow = c(2, 2))  # Set up a 2 x 2 plotting space

# Create the loop.vector (all the columns)
loop.vector <- 1:4

for (i in loop.vector) { # Loop over loop.vector

  # store data in column.i as x
  x <- examscores[,i]
  
  # Plot histogram of x
  hist(x,
       main = paste("Question", i),
       xlab = "Scores",
       xlim = c(0, 100))
}
5 dưới dạng một vectơ mới
par(mfrow = c(2, 2))  # Set up a 2 x 2 plotting space

# Create the loop.vector (all the columns)
loop.vector <- 1:4

for (i in loop.vector) { # Loop over loop.vector

  # store data in column.i as x
  x <- examscores[,i]
  
  # Plot histogram of x
  hist(x,
       main = paste("Question", i),
       xlab = "Scores",
       xlim = c(0, 100))
}
2. Cuối cùng, tôi đã tạo một biểu đồ của đối tượng
par(mfrow = c(2, 2))  # Set up a 2 x 2 plotting space

# Create the loop.vector (all the columns)
loop.vector <- 1:4

for (i in loop.vector) { # Loop over loop.vector

  # store data in column.i as x
  x <- examscores[,i]
  
  # Plot histogram of x
  hist(x,
       main = paste("Question", i),
       xlab = "Scores",
       xlim = c(0, 100))
}
2

Chọn một trang web để nhận nội dung đã dịch nếu có và xem các sự kiện và ưu đãi tại địa phương. Dựa trên vị trí của bạn, chúng tôi khuyên bạn nên chọn.

Bạn cũng có thể chọn một trang web từ danh sách sau

Làm thế nào để có được hiệu suất trang web tốt nhất

Chọn trang Trung Quốc (bằng tiếng Trung hoặc tiếng Anh) để có hiệu suất trang tốt nhất. Các trang web quốc gia khác của MathWorks không được tối ưu hóa cho các lượt truy cập từ vị trí của bạn

Tôi đã quản lý để làm điều đó trong Python. Sử dụng cách tiếp cận tương tự, tôi đã thử thực hiện trong R. Thật không may, biểu đồ đường xuất hiện ba biểu đồ riêng biệt. Dưới đây là những nỗ lực của tôi

con trăn

import numpy as np
import matplotlib.pyplot as plt
interest_rates = [.12, .15, .2]
years = 10
amount = np.empty(years + 1)
for i in interest_rates:
    amount[0] = 100
    for year in range(years):
        amount[year + 1] = amount[year]*(1 + i)
    plt.plot(amount, label = f'$\\alpha = {i}$')
plt.legend()
plt.show()

Cách vẽ nhiều biểu đồ trong Python bằng vòng lặp for

R

interest_rates <- c(.12, .15, .2)
years <- 10
amount <- vector("double", years + 1)

for (i in interest_rates){
  amount[1] <- 100
  for (year in seq(years)){
    amount[year + 1] = amount[year] * (1 + i)
  }
  plot(amount, type = "line")
}
#> Warning in plot.xy(xy, type, ...): plot type 'line' will be truncated to first
#> character

Cách vẽ nhiều biểu đồ trong Python bằng vòng lặp for

#> Warning in plot.xy(xy, type, ...): plot type 'line' will be truncated to first
#> character

Cách vẽ nhiều biểu đồ trong Python bằng vòng lặp for

#> Warning in plot.xy(xy, type, ...): plot type 'line' will be truncated to first
#> character

Cách vẽ nhiều biểu đồ trong Python bằng vòng lặp for

Được tạo vào ngày 2021-08-14 bởi gói reprex (v2. 0. 0)

Cách vẽ nhiều biểu đồ trong Python bằng vòng lặp for
budugulo

  • Tôi đã đặt thủ công
    par(mfrow = c(2, 2))  # Set up a 2 x 2 plotting space
    
    # Create the loop.vector (all the columns)
    loop.vector <- 1:4
    
    for (i in loop.vector) { # Loop over loop.vector
    
      # store data in column.i as x
      x <- examscores[,i]
      
      # Plot histogram of x
      hist(x,
           main = paste("Question", i),
           xlab = "Scores",
           xlim = c(0, 100))
    }
    9. Làm cách nào để đặt nó thành
    par(mfrow = c(2, 2))  # Set up a 2 x 2 plotting space
    
    # Create the loop.vector (all the columns)
    loop.vector <- 1:4
    
    for (i in loop.vector) { # Loop over loop.vector
    
      # store data in column.i as x
      x <- examscores[,i]
      
      # Plot histogram of x
      hist(x,
           main = paste("Question", i),
           xlab = "Scores",
           xlim = c(0, 100))
    }
    0?
  • Làm cách nào để tôi có được các dòng màu và chú giải (như biểu đồ Python ở trên)?

Đây là một cách để làm những điều đó. Nhưng tôi sẽ không khuyên bạn nên làm theo cách này

Mã liên quan
# Setup
interest_rates <- c(0.12, 0.15, 0.2)
years <- 10
amount <- vector("double", years + 1)

# Limit calculation
y_min <- 100
y_max <- 100 * (1 + max(interest_rates))^years
y_range <- c(y_min - 50, y_max + 50)

# Empty plot
plot(NULL, xlab = "Year", ylab = "Amount", xlim = c(0, years), ylim = y_range)

# Comparative lines
for (i in seq_along(interest_rates)) {
  amount[1] <- 100
  for (year in seq(years)) {
    amount[year + 1] <- amount[year] * (1 + interest_rates[i])
  }
  lines(x = 0:10, y = amount, type = "l", lty = 1, col = i + 1)
}

# Legends
legend("topleft", legend = paste0("alpha = ", interest_rates), lty = 1, col = 1 + seq_along(interest_rates))

Cách vẽ nhiều biểu đồ trong Python bằng vòng lặp for

Đây là một cách khác chỉ sử dụng đồ họa cơ bản

# Setup
interest_rates <- c(0.12, 0.15, 0.2)
no_years <- 10

# Calculation
initial_amount <- 100
time_frame <- 0:no_years
cumulative_amounts <- vapply(interest_rates, \(r) {
  initial_amount * (1 + r)^time_frame
}, numeric(length(time_frame)))

# Plot
matplot(time_frame, cumulative_amounts, type = "l", lty = 1, col = 1 + seq_along(interest_rates), xlab = "Year", ylab = "Amount", xlim = c(0, no_years), ylim = range(cumulative_amounts) + c(-50, 50))
legend("topleft", legend = interest_rates, lty = 1, col = 1 + seq_along(interest_rates), title = expression(alpha))

Cách vẽ nhiều biểu đồ trong Python bằng vòng lặp for

Hi vọng điêu nay co ich