Python X轴散点图MatplotLib上的限制范围

Python X轴散点图MatplotLib上的限制范围,python,pandas,matplotlib,Python,Pandas,Matplotlib,我有一个基于时间索引的数据框,另一列表示在该时间发生车祸时受伤行人的数量。我尝试使用Matplotlib来绘制数据,并且已经成功地绘制了数据,但是范围太大了。我有一天的数据,并希望使用计算的最小值和最大值相应地设置x轴限制。我已尝试使用plt.set_xlim,但出现错误: AttributeError:“PathCollection”对象没有属性“set\u xlim” 如何正确设置限制 无限制图 您可以创建轴,然后使用ax.xlim() 使用plt.xlim(最小值,最大值)。@IIya

我有一个基于时间索引的数据框,另一列表示在该时间发生车祸时受伤行人的数量。我尝试使用Matplotlib来绘制数据,并且已经成功地绘制了数据,但是范围太大了。我有一天的数据,并希望使用计算的最小值和最大值相应地设置x轴限制。我已尝试使用plt.set_xlim,但出现错误:

AttributeError:“PathCollection”对象没有属性“set\u xlim”

如何正确设置限制

无限制图


您可以创建轴,然后使用
ax.xlim()


使用
plt.xlim(最小值,最大值)

@IIya V.Schurov 111是什么意思?它创建了一个“子图形矩阵”,包含一行一列。请参见此处的详细信息:@iiiya V.Schurov I使用命令获得“keyrerror:time”:df1.plot(x='time',y='number'u of_walkers\u infected',kind='scatter')您仍然可以使用
plt.scatter
而不是
df1.plot
。例如,您可以尝试在代码中使用
plt.xlim()
而不是
fig.set\u xlim()
df = test_request_pandas()
df1 = df[['time','number_of_pedestrians_injured']]
df1.set_index(['time'],inplace=True)
df1 = df1.astype(float)
min = df1.index.min()
max = df1.index.max()
fig = plt.scatter(df1.index.to_pydatetime(), df1['number_of_pedestrians_injured'])
#fig.set_xlim([min, max]) // causing me issues
plt.show()
import pandas as pd
df = pd.DataFrame({'x':[1,2,3], 'y':[3,4,5]})
fig = plt.figure()
ax = plt.subplot(111)
df.plot(x='x',y='y',ax=ax, kind='scatter')
ax.set_xlim(2,3)