python - matplotlib 轴值未排序
问题描述
我正在使用plt.scatter
一组值,这是我得到的输出:
为什么我在 x 和 y 上都得到这些凌乱的轴?他们为什么乱?我希望有一个从 0 开始并以无限结束的轴,而不是增加和减少。
如果您需要,这是我的代码:
lines = []
XsR = []
YsR = []
XsL = []
YsL = []
with open('tracking_test.txt') as f:
lines = f.readlines()
for i in range (len(lines)):
lines[i] = re.sub(r"[()]", " ", lines[i])
lines[i] = lines[i].split(',')
if i%2 == 0:
XsR.append(lines[i][0])
YsR.append(lines[i][1])
else:
XsL.append(lines[i][0])
YsL.append(lines[i][1])
x = np.array(XsR)
y = np.array(YsR)
plt.scatter(x, y)
x = np.array(XsL)
y = np.array(YsL)
plt.scatter(x, y)
yabs_max = abs(max(ax.get_ylim(), key=abs))
ax.set_ylim(ymin=-yabs_max, ymax=yabs_max)
plt.axhline(y=0.0, color='b', linestyle='-')
plt.axvline(x=0.0, color = 'b', linestyle = '-')
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(12, 9)
plt.show()
该文件包含以下内容:
(140.7, 217.0, 0.0)
(-230.6, 241.4, 0.0)
(0.0, 0.0, 0.0)
(0.0, 0.0, 0.0)
(0.0, 0.0, 0.0)
(0.0, 0.0, 0.0)
(229.6, 119.5, 0.0)
(0.0, 0.0, 0.0)
(232.0, 120.5, 0.0)
(0.0, 0.0, 0.0)
(241.7, 113.7, 0.0)
(-90.6, 172.4, 0.0)
(189.1, 143.0, 0.0)
(-150.3, 145.2, 0.0)
(189.1, 143.0, 0.0)
(-124.7, 182.6, 0.0)
(32.6, 15.3, 0.0)
(-39.5, 109.2, 0.0)
(32.6, 15.3, 0.0)
(-286.6, 33.6, 0.0)
(32.6, 15.3, 0.0)
(-286.6, 33.6, 0.0)
(32.6, 15.3, 0.0)
(-286.6, 33.6, 0.0)
解决方案
使用现有代码
dtype
在创建数组时指定是使用现有代码的最佳选择。- 鉴于
x
andy
是 numpy 数组(例如x = np.array(['1', '2', '3'])
:x.astype(float)
也可以转换类型,但最适合您可以访问数组,而不是创建数组的步骤。- 同时,
float(x)
将导致TypeError
.
# convert the lists to arrays and set the dtype
XsR = np.array(XsR, dtype=float)
YsR = np.array(YsR, dtype=float)
XsL = np.array(XsL, dtype=float)
YsL = np.array(YsL, dtype=float)
fig, ax = plt.subplots(figsize=(12, 9))
ax.scatter(XsR, YsR)
ax.scatter(XsL, YsL)
yabs_max = abs(max(ax.get_ylim(), key=abs))
ax.set_ylim(ymin=-yabs_max, ymax=yabs_max)
ax.axhline(y=0.0, color='b', linestyle='-')
ax.axvline(x=0.0, color = 'b', linestyle = '-')
使用pandas
- 给定您的数据,最简洁的解决方案是使用
pandas
读取数据.read_csv()
,但将分隔符设置为 a 以外的其他内容,
。- 这使得从文件中
pandas
读取tuple
到单个列
- 这使得从文件中
- 用于
ast.literal_eval
将字符串字面量的列转换为字面量,在本例中为 atuple
offloats
- 将
tuple
分成单独的列,然后用pandas.DataFrame.plot
. - 这将代码从 29 行减少到仅 11 行代码。
- 使用
pandas v1.2.4
和matplotlib v3.3.4
pandas
作为依赖项导入matplotlib
,所以使用下面的代码,如图所示,不需要matplotlib
单独导入。
import pandas as pd
from ast import literal_eval
# load the data from your file; as shown in the OP, there's no header, so the column will be named 0
# convert the column with ast.literal_eval
df = pd.read_csv('test.csv', sep=';', header=None, converters={0: literal_eval})
# display(df.head(2))
0
0 (140.7, 217.0, 0.0)
1 (-230.6, 241.4, 0.0)
# split the column into separate columns
df[['x', 'y', 'z']] = pd.DataFrame(df[0].tolist(), index= df.index)
# display(df.head(2))
0 x y z
0 (140.7, 217.0, 0.0) 140.7 217.0 0.0
1 (-230.6, 241.4, 0.0) -230.6 241.4 0.0
# split the data by odd and even indices
mask = df.index % 2 == 0
df_sR = df[mask]
df_sL = df[~mask]
# plot the dataframe
ax = df_sR.plot(kind='scatter', x='x', y='y', figsize=(12, 9), c='blue')
df_sL.plot(kind='scatter', x='x', y='y', ax=ax, c='orange')
yabs_max = abs(max(ax.get_ylim(), key=abs))
ax.set_ylim(ymin=-yabs_max, ymax=yabs_max)
ax.axhline(y=0.0, color='b', linestyle='-')
ax.axvline(x=0.0, color = 'b', linestyle = '-')
推荐阅读
- c++ - 在 Windows 10 上合成语音:找不到“Scenario_SynthesizeText.gh”文件
- graphql - 赛普拉斯中的“查找”是否等待 GraphQL 请求完成?
- c# - 如何获得派生方法来更改其基变量或对象之一,然后从类外部调用更改后的对象?
- python - ImportError:无法加载需要“tk”交互式框架的后端“TkAgg”,因为“无头”当前正在运行
- java - 如何修复组合框中的模糊项目
- python - 如何在不使用主键字段的情况下实现与同一张表的关系?
- swift - 如何将数据从集合视图单元拉到不同的视图控制器?
- python - 如何解决在 python3.7 中安装 pycsv 的错误?
- reactjs - 只有当它等于 action.id 时,如何告诉 redux 设置某个值
- c# - 有没有更好的方法来使 asp.net mvc 应用程序中使用的 redis 缓存失效?