使用numpy,matplotlib和OS(渲染probelm)的2D图的配色栏?

发布于 2025-01-31 19:11:45 字数 1588 浏览 2 评论 0原文

我有多个.plx文件,这些文件包含两个格式为字符串的数字(1.plx,2.plx ...) 我设法修改了一个代码以加载数据,将其转换为浮子并使用适当的配色栏绘制它,但是我无法解决两个问题:

  1. 线的颜色不会更新
  2. 渲染线(可能是)由于重复))

我想尝试通过绘制numpy矩阵来避免渲染问题,因此我想:

  1. 将数据
  2. 存储加载到numpy矩阵中(以外的循环以外,以便我可以做其他数据处理工作)
  3. 创建2D 与配色栏一起绘制的

我的尝试和结果是

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import os
IdVg = [IdVg for IdVg in os.listdir() if IdVg.endswith(".plx")]

n_lines = 20

steps = np.linspace(0.1, 50, 20)

norm = mpl.colors.Normalize(vmin=steps.min(), vmax=steps.max())
cmap = mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.BuPu)
cmap.set_array([])

for i in IdVg:
    x, y = np.loadtxt(i, delimiter=' ', unpack=True, skiprows= 1, dtype=str)
    x = x.astype(np.float64)
    y = y.astype(np.float64)
    for z, ai in enumerate(steps.T): # Problem here, I want to store x, y values in a 40XN matrix
                                     # (x1, y1, x2, y2...x20, y20) and find a way to plot them
                                     # using Matplotlib and numpy
        plt.plot(x, y, c=cmap.to_rgba(z+1))
plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
plt.xlabel('$V_{GS}$ (V)', fontsize=14)
plt.ylabel('$I_{DS}$ (A)', fontsize=14)
plt.tick_params(axis='both', labelsize='12')
plt.grid(True, which="both", ls="-")
plt.colorbar(cmap, ticks=steps)
plt.show()

结果:谢谢!

I have multiple .plx files that contain two column of numbers formatted as strings (1.plx , 2.plx...)
I managed to modify a code to load the data, convert it to floats, and plot it with the appropriate colorbar, but there are two issues I couldn't solve:

  1. The color of the lines does not update
  2. The lines rendering looks wrong (probably due to duplicates)

I want to try to avoid that rendering problem by plotting a numpy matrix, so I want to :

  1. Load the data
  2. store it in a numpy matrix (outside the loop so that I can do other data processing stuff)
  3. create a 2D plot with the colorbar

Here is my attempt and the result:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import os
IdVg = [IdVg for IdVg in os.listdir() if IdVg.endswith(".plx")]

n_lines = 20

steps = np.linspace(0.1, 50, 20)

norm = mpl.colors.Normalize(vmin=steps.min(), vmax=steps.max())
cmap = mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.BuPu)
cmap.set_array([])

for i in IdVg:
    x, y = np.loadtxt(i, delimiter=' ', unpack=True, skiprows= 1, dtype=str)
    x = x.astype(np.float64)
    y = y.astype(np.float64)
    for z, ai in enumerate(steps.T): # Problem here, I want to store x, y values in a 40XN matrix
                                     # (x1, y1, x2, y2...x20, y20) and find a way to plot them
                                     # using Matplotlib and numpy
        plt.plot(x, y, c=cmap.to_rgba(z+1))
plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
plt.xlabel('$V_{GS}$ (V)', fontsize=14)
plt.ylabel('$I_{DS}$ (A)', fontsize=14)
plt.tick_params(axis='both', labelsize='12')
plt.grid(True, which="both", ls="-")
plt.colorbar(cmap, ticks=steps)
plt.show()

Thanks !

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你手 2025-02-07 19:11:45

由于您没有提供数据,因此我将生成自己的数据。我假设您想获得以下结果:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import os

n_lines = 20
steps = np.linspace(0.1, 50, 20)

norm = mpl.colors.Normalize(vmin=steps.min(), vmax=steps.max())
norm_steps = norm(steps)
cmap = mpl.cm.BuPu

plt.figure()
x = np.linspace(0, np.pi / 2)
for i in range(n_lines):
    y = i / n_lines * np.sin(x)
    plt.plot(x, y, c=cmap(norm_steps[i]))
plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
plt.xlabel('$V_{GS}$ (V)', fontsize=14)
plt.ylabel('$I_{DS}$ (A)', fontsize=14)
plt.tick_params(axis='both', labelsize='12')
plt.grid(True, which="both", ls="-")
plt.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.BuPu), ticks=steps)
plt.show()

显然,您必须将colormap更改为较低值中更可读的东西!

Since you didn't provide data, I'm going to generate my own. I assume you want to obtain the following result:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import os

n_lines = 20
steps = np.linspace(0.1, 50, 20)

norm = mpl.colors.Normalize(vmin=steps.min(), vmax=steps.max())
norm_steps = norm(steps)
cmap = mpl.cm.BuPu

plt.figure()
x = np.linspace(0, np.pi / 2)
for i in range(n_lines):
    y = i / n_lines * np.sin(x)
    plt.plot(x, y, c=cmap(norm_steps[i]))
plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
plt.xlabel('$V_{GS}$ (V)', fontsize=14)
plt.ylabel('$I_{DS}$ (A)', fontsize=14)
plt.tick_params(axis='both', labelsize='12')
plt.grid(True, which="both", ls="-")
plt.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.BuPu), ticks=steps)
plt.show()

Obviously, you would have to change the colormap to something more readable in the lower values!

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文