无法使用matplotlib生成3行图表。

huangapple go评论91阅读模式
英文:

Can't generate 3 line graph using matplotlib

问题

  1. 我不打算拖延会直接点出来这是我的代码
  2. ```python
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. def fifo_page_replacement(num_frames, reference_string):
  6. frame_list = [] # 用于存储当前分配的页面帧的列表
  7. fault_count = 0 # 页面错误计数器
  8. for page in reference_string:
  9. if page not in frame_list:
  10. fault_count += 1
  11. if len(frame_list) == num_frames:
  12. # 从页面帧中移除最旧的页面
  13. frame_list.pop(0)
  14. # 将新页面添加到页面帧中
  15. frame_list.append(page)
  16. return fault_count
  17. # 示例数据
  18. num_frames = 3 # 要测试的页面帧数
  19. reference_string = [
  20. 1,
  21. 2,
  22. 3,
  23. 4,
  24. 2,
  25. 1,
  26. 5,
  27. 6,
  28. 2,
  29. 1,
  30. 2,
  31. 3,
  32. 7,
  33. 6,
  34. 3,
  35. 2,
  36. 1,
  37. 2,
  38. 3,
  39. 6,
  40. ] # 引用字符串
  41. # 生成 x 轴数值
  42. x_values_1 = np.arange(1, num_frames + 1)
  43. x_values_2 = np.arange(1, num_frames + 2)
  44. x_values_3 = np.arange(1, num_frames + 3)
  45. # 使用 FIFO 算法计算给定页面帧数的页面错误
  46. page_faults_1 = [fifo_page_replacement(frame, reference_string) for frame in x_values_1]
  47. page_faults_2 = [fifo_page_replacement(frame, reference_string) for frame in x_values_2]
  48. page_faults_3 = [fifo_page_replacement(frame, reference_string) for frame in x_values_3]
  49. # 绘图
  50. plt.plot(x_values_1, page_faults_1, label="Line 1", marker="o")
  51. plt.plot(x_values_2, page_faults_2, label="Line 2", marker="o")
  52. plt.plot(x_values_3, page_faults_3, label="Line 3", marker="o")
  53. plt.xlabel("页面帧数")
  54. plt.ylabel("页面错误数")
  55. plt.title("FIFO 页面替换算法")
  56. plt.grid(True)
  57. plt.legend()
  58. plt.show()

我想要一个包含三条线的图表,基本上是在一个单一图表中绘制三个不同的曲线。fifo_page_replacement 函数返回了预期的值。此外,我不希望为三个不同的曲线绘制不同的图表,只需要一个图表。我期望上述结果。

  1. <details>
  2. <summary>英文:</summary>
  3. I am not going to stretch this, will come straight to the point, this is my code:

import matplotlib.pyplot as plt
import numpy as np

def fifo_page_replacement(num_frames, reference_string):
frame_list = [] # List to store the currently allocated page frames
fault_count = 0 # Counter for page faults

  1. for page in reference_string:
  2. if page not in frame_list:
  3. fault_count += 1
  4. if len(frame_list) == num_frames:
  5. # Remove the oldest page from the page frames
  6. frame_list.pop(0)
  7. # Add the new page to the page frames
  8. frame_list.append(page)
  9. return fault_count

Sample data

Sample data

num_frames = 3 # Number of page frames to test
reference_string = [
1,
2,
3,
4,
2,
1,
5,
6,
2,
1,
2,
3,
7,
6,
3,
2,
1,
2,
3,
6,
] # Reference string

Generate x-axis values

x_values_1 = np.arange(1, num_frames + 1)
x_values_2 = np.arange(1, num_frames + 2)
x_values_3 = np.arange(1, num_frames + 3)

Calculate page faults using FIFO algorithm for the given number of frames

page_faults_1 = [fifo_page_replacement(frame, reference_string) for frame in x_values_1]
page_faults_2 = [fifo_page_replacement(frame, reference_string) for frame in x_values_2]
page_faults_3 = [fifo_page_replacement(frame, reference_string) for frame in x_values_3]

Plotting

plt.plot(x_values_1, page_faults_1, label="Line 1", marker="o")
plt.plot(x_values_2, page_faults_2, label="Line 2", marker="o")
plt.plot(x_values_3, page_faults_3, label="Line 3", marker="o")

plt.xlabel("Number of Page Frames")
plt.ylabel("Number of Page Faults")
plt.title("FIFO Page Replacement Algorithm")
plt.grid(True)
plt.legend()

plt.show()

  1. I want a three line graph, basically three different plots in a single graph. `fifo_paging_algorithm` is returning values as expected. Also I don&#39;t want different graphs for three different plots, just one graph.
  2. I am expecting the above results.
  3. </details>
  4. # 答案1
  5. **得分**: 0
  6. 你的代码运行正常,但数据有问题,因此只显示第3行,遮盖了其他是第3行的子集的数据。如果给`matplotlib`不同的数据,你的代码就能正常工作。
  7. 将你的代码更改为生成以下数据:

page_faults_1 = [fifo_page_replacement(frame, reference_string) for frame in x_values_1]
page_faults_2 = [fifo_page_replacement(frame, reference_string) * 2 for frame in x_values_2]
page_faults_3 = [fifo_page_replacement(frame, reference_string) * 3 for frame in x_values_3]

  1. 可以生成所需的三条线并在一个图中绘制出来。
  2. <details>
  3. <summary>英文:</summary>
  4. Your code is working fine, your data is bad and thus you only get line 3 which shadows the other data which are subsets of line 3. If you give `matplotlib` distinct data your code works just fine.
  5. Changing your code to generate the data to this:
  6. page_faults_1 = [fifo_page_replacement(frame, reference_string) for frame in x_values_1]
  7. page_faults_2 = [fifo_page_replacement(frame, reference_string) * 2 for frame in x_values_2]
  8. page_faults_3 = [fifo_page_replacement(frame, reference_string) * 3 for frame in x_values_3]
  9. Produces the desired 3 lines in one plot:
  10. [![enter image description here][1]][1]
  11. [1]: https://i.stack.imgur.com/rTmQJ.png
  12. </details>

huangapple
  • 本文由 发表于 2023年6月18日 23:36:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定