0

matplotlib 中有几个关于 x 值的 Q/A,它显示当 x 值是 int 或 float 时,matploblit 以 x 的正确顺序绘制图形。例如,在字符类型中,绘图按顺序显示 x 值

1 15 17 2 21 7 etc

但是当它变成int时,它变成了

1 2 7 15 17 21 etc

在人类秩序中。如果 x 值与字符和数字混合,例如

NN8 NN10 NN15 NN20 NN22 etc

情节将按顺序显示

NN10 NN15 NN20 NN22 NN8 etc

有没有办法在不删除 x 值中的 'NN' 的情况下以人类顺序或 x 列表中的现有顺序修复 x 值的顺序。

更详细地说,xvalues 是目录名,在 linux 函数中使用 grep 排序,结果在 linux 终端中显示如下,可以保存在文本文件中。

joonho@login:~/NDataNpowN$ get_TEFrmse NN 2 | sort -n -t N -k 3
NN7 0.3311
NN8 0.3221
NN9 0.2457
NN10 0.2462
NN12 0.2607
NN14 0.2635

不加排序,linux shell也按机器顺序显示如

NN10 0.2462
NN12 0.2607
NN14 0.2635
NN7 0.3311
NN8 0.3221
NN9 0.2457
4

1 回答 1

1

正如我所说,pandas 会使这项任务比处理基本 Python 列表等更容易:

import matplotlib.pyplot as plt
import pandas as pd

#imports the text file assuming that your data are separated by space, as in your example above
df = pd.read_csv("test.txt", delim_whitespace=True, names=["X", "Y"])
#extracting the number in a separate column, assuming you do not have terms like NN1B3X5
df["N"] = df.X.str.replace(r"\D", "", regex=True).astype(int)
#this step is only necessary, if your file is not pre-sorted by Linux
df = df.sort_values(by="N")

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 6))

#categorical plotting
df.plot(x="X", y="Y", ax=ax1)
ax1.set_title("Evenly spaced")

#numerical plotting
df.plot(x="N", y="Y", ax=ax2)
ax2.set_xticks(df.N)
ax2.set_xticklabels(df.X)
ax2.set_title("Numerical spacing")

plt.show()

样本输出: 在此处输入图像描述

既然您问是否有非熊猫解决方案 - 当然。Pandas 让一些事情变得更加方便。在这种情况下,我将恢复为 numpy。Numpy 是一个 matplotlib 依赖项,因此与 pandas 相比,它必须安装,如果您使用 matplotlib:

import matplotlib.pyplot as plt
import numpy as np
import re

#read file as strings
arr = np.genfromtxt("test.txt", dtype="U15")
#remove trailing strings
Xnums = np.asarray([re.sub(r"\D", "", i) for i in arr[:, 0]], dtype=int)
#sort array 
arr = arr[np.argsort(Xnums)]
#extract x-values as strings...
Xstr = arr[:, 0]
#...and y-values as float
Yvals = arr[:, 1].astype(float)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 6))

#categorical plotting
ax1.plot(Xstr, Yvals)
ax1.set_title("Evenly spaced")

#numerical plotting
ax2.plot(np.sort(Xnums), Yvals)
ax2.set_xticks(np.sort(Xnums))
ax2.set_xticklabels(Xstr)
ax2.set_title("Numerical spacing")

plt.show()

样本输出:

在此处输入图像描述

于 2021-02-18T09:42:28.373 回答