Math Problem Statement

import numpy as np import matplotlib.pyplot as plt from pylab import mpl mpl.rcParams["font.sans-serif"] = ["SimHei"] plt.rcParams['axes.unicode_minus'] = False# 用来正常显示负号 import numpy as np

生成实际值(28个点,假设呈现周期性波动)

np.random.seed(42) x = np.arange(28) actual_values = np.sin(x * 0.5) + np.random.normal(0, 0.1, 28) + 45

生成4个模型的预测值

模型1:预测效果最好(与实际值较为接近)

model_1 = actual_values + np.random.normal(0, 0.05, 28)

模型2:总体较好,但有细节误差

model_2 = actual_values + np.random.normal(0, 0.1, 28)

模型3:总体较好,但有细节误差

model_3 = actual_values + np.random.normal(0, 0.12, 28)

模型4:总体较好,但有细节误差

model_4 = actual_values + np.random.normal(0, 0.15, 28)

绘制图像

plt.figure(figsize=(10, 6)) plt.plot(x, actual_values, label='实际值', color='black', linewidth=2, marker='o') plt.plot(x, model_1, label='模型1(预测较好)', color='blue', linestyle='--', marker='x') plt.plot(x, model_2, label='模型2', color='green', linestyle='--', marker='s') plt.plot(x, model_3, label='模型3', color='red', linestyle='--', marker='^') plt.plot(x, model_4, label='模型4', color='orange', linestyle='--', marker='d')

添加标题和图例

plt.title('时序数据预测结果比较') plt.xlabel('时间点') plt.ylabel('数值') plt.legend() plt.grid(True) plt.show()请修改程序,x轴每隔4个坐标点,依次设置为第一天,第二天,直至第七天;y轴44.0对应4400,45.0对应4800

Solution

To modify the program so that the x-axis displays days from "第一天" (Day 1) to "第七天" (Day 7) and the y-axis transforms the values such that 44.0 corresponds to 4400 and 45.0 corresponds to 4800, you can adjust the xticks on the x-axis and apply a scaling factor of 100 to the y-axis values. Here's how you can modify your code:

Updated Code:

python
import numpy as np import matplotlib.pyplot as plt from pylab import mpl mpl.rcParams["font.sans-serif"] = ["SimHei"] plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 # 生成实际值(28个点,假设呈现周期性波动) np.random.seed(42) x = np.arange(28) actual_values = np.sin(x * 0.5) + np.random.normal(0, 0.1, 28) + 45 # 生成4个模型的预测值 model_1 = actual_values + np.random.normal(0, 0.05, 28) model_2 = actual_values + np.random.normal(0, 0.1, 28) model_3 = actual_values + np.random.normal(0, 0.12, 28) model_4 = actual_values + np.random.normal(0, 0.15, 28) # 将y轴数值乘以100,使得44.0对应4400,45.0对应4800 actual_values_scaled = actual_values * 100 model_1_scaled = model_1 * 100 model_2_scaled = model_2 * 100 model_3_scaled = model_3 * 100 model_4_scaled = model_4 * 100 # 绘制图像 plt.figure(figsize=(10, 6)) plt.plot(x, actual_values_scaled, label='实际值', color='black', linewidth=2, marker='o') plt.plot(x, model_1_scaled, label='模型1(预测较好)', color='blue', linestyle='--', marker='x') plt.plot(x, model_2_scaled, label='模型2', color='green', linestyle='--', marker='s') plt.plot(x, model_3_scaled, label='模型3', color='red', linestyle='--', marker='^') plt.plot(x, model_4_scaled, label='模型4', color='orange', linestyle='--', marker='d') # 设置x轴刻度为每隔4个点显示一次,表示“第1天”到“第7天” day_labels = ['第一天', '第二天', '第三天', '第四天', '第五天', '第六天', '第七天'] plt.xticks(np.arange(0, 28, 4), day_labels) # 添加标题和图例 plt.title('时序数据预测结果比较') plt.xlabel('时间点') plt.ylabel('数值 (乘以100)') plt.legend() plt.grid(True) plt.show()

Explanation of Changes:

  1. Scaling the y-axis: The y-values (actual_values, model_1, etc.) are multiplied by 100 to achieve the transformation where 44.0 corresponds to 4400 and 45.0 to 4800.
  2. Custom x-axis labels: The xticks function is used to set the x-axis labels to display "第一天" to "第七天" every 4 time points. Since you have 28 data points, this works well, as you get 7 labels (1 label per 4 points).

Let me know if you need further clarification or more adjustments!


Do you want details about how xticks and yticks transformations work, or have any questions?

Here are five additional related questions to explore:

  1. How can you adjust the intervals between x-ticks or y-ticks to show different time spans?
  2. Can you set different colors or styles for each model for better comparison?
  3. How would you modify the code if the data is not evenly spaced (e.g., irregular time intervals)?
  4. What changes are required if you want to plot this with a logarithmic scale on the y-axis?
  5. How can you save this plot as an image file with high resolution?

Tip: Always label your axes and include legends to make the plot easier to interpret, especially in comparative analyses.

Ask a new question for Free

By Image

Drop file here or Click Here to upload

Math Problem Analysis

Mathematical Concepts

Data Visualization
Time Series Analysis
Matplotlib Plotting

Formulas

-

Theorems

-

Suitable Grade Level

Grades 10-12