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

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

Can't generate 3 line graph using matplotlib

问题

我不打算拖延会直接点出来这是我的代码

```python
import matplotlib.pyplot as plt
import numpy as np

def fifo_page_replacement(num_frames, reference_string):
    frame_list = []  # 用于存储当前分配的页面帧的列表
    fault_count = 0  # 页面错误计数器

    for page in reference_string:
        if page not in frame_list:
            fault_count += 1

            if len(frame_list) == num_frames:
                # 从页面帧中移除最旧的页面
                frame_list.pop(0)

            # 将新页面添加到页面帧中
            frame_list.append(page)

    return fault_count

# 示例数据
num_frames = 3  # 要测试的页面帧数
reference_string = [
    1,
    2,
    3,
    4,
    2,
    1,
    5,
    6,
    2,
    1,
    2,
    3,
    7,
    6,
    3,
    2,
    1,
    2,
    3,
    6,
]  # 引用字符串

# 生成 x 轴数值
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)

# 使用 FIFO 算法计算给定页面帧数的页面错误
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]

# 绘图
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("页面帧数")
plt.ylabel("页面错误数")
plt.title("FIFO 页面替换算法")
plt.grid(True)
plt.legend()

plt.show()

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


<details>
<summary>英文:</summary>
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

for page in reference_string:
if page not in frame_list:
fault_count += 1
if len(frame_list) == num_frames:
# Remove the oldest page from the page frames
frame_list.pop(0)
# Add the new page to the page frames
frame_list.append(page)
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()

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.
I am expecting the above results.
</details>
# 答案1
**得分**: 0
你的代码运行正常,但数据有问题,因此只显示第3行,遮盖了其他是第3行的子集的数据。如果给`matplotlib`不同的数据,你的代码就能正常工作。
将你的代码更改为生成以下数据:

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]


可以生成所需的三条线并在一个图中绘制出来。
<details>
<summary>英文:</summary>
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.
Changing your code to generate the data to this:
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]
Produces the desired 3 lines in one plot:
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/rTmQJ.png
</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:

确定