我试图在一个图中绘制2个情节。所以只需要2个子图并调整图形大小并找到合适的图形。目前我正在使用此代码:
import matplotlib.pyplot as plt
import pandas as pd
#import numpy as np
##### import data #####
df=pd.read_csv('C:\\Users\Kevin\Documents\Afstudeer\Measurements/1st_plot.txt',sep=',',decimal='.',header=None)
df.columns=['Vx','Vy','undefined','Laser_signal']
fig, ax = plt.subplots(figsize=(8, 5))
ax1=fig.add_subplot(121)
ax1.plot(df['Vx'],df['Vy'],label='plot')
plt.xlabel(r'$V_x$')
plt.ylabel(r'$V_y$')
ax2=fig.add_subplot(122)
ax1.scatter(df['Vx'],df['Vy'],label='data_points')
plt.xlabel(r'$V_x$')
plt.ylabel(r'$V_y$')
plt.subplots_adjust(left=.2, bottom=.45, right=.8, top=.95,
wspace=.3, hspace=.4)
所以它的最后一个代码令我感到困惑。当我做情节时,我得到这样的东西:
以下是我的示例数据:
-1.725953467,0.109343505,-10.433363664,0.159675246
-1.725953467,0.110607445,-10.433363664,0.159675246
-1.729140157,0.110607445,-10.433363664,0.159675246
-1.722766777,0.10839555,-10.433363664,0.159675246
-1.727865481,0.11534722,-10.433363664,0.159359499
-1.726272136,0.112503355,-10.433363664,0.159675246
-1.731689509,0.120086995,-10.433363664,0.159359499
-1.727228143,0.117559115,-10.433363664,0.159359499
-1.729140157,0.11977101,-10.433363664,0.159675246
-1.730096164,0.121350935,-10.433363664,0.159675246
-1.729458826,0.122614875,-10.433363664,0.159043752
-1.735832206,0.12482677,-10.433363664,0.159359499
-1.728821488,0.121350935,-10.433363664,0.159675246
-1.733920192,0.124510785,-10.433363664,0.159359499
-1.731052171,0.12166692,-10.433363664,0.159675246
-1.739018896,0.12735465,-10.433363664,0.159043752
-1.738062889,0.12861859,-10.433363664,0.159043752
-1.738700227,0.133358365,-10.433363664,0.159043752
-1.73455753,0.12988253,-10.433363664,0.159043752
-1.743161593,0.144101855,-10.433363664,0.159043752
正如您将从代码中看到的那样,我只获得前2列。我期待一个图中的2个子图。那么为什么我会在这些图之间得到这些上下线?
答案 0 :(得分:2)
两行
fig, ax = plt.subplots(figsize=(8, 5))
ax1=fig.add_subplot(121)
以某种方式相互排斥。您可以通过
创建子图fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 5))
或者您通过
创建它们fig = plt.figure()
ax1=fig.add_subplot(121)
ax2=fig.add_subplot(122)
之后更好地使用轴处理ax1
和ax2
来设置任何属性,例如
ax1.set_xlabel(r'$V_x$')
而不是plt.xlabel
。