如何在matplotlib中更改x軸,以便沒有空白?因此,目前正在學(xué)習(xí)如何導(dǎo)入數(shù)據(jù)并在matplotlib中使用它,而且我遇到了麻煩,即使我有這本書中的確切代碼。這就是圖的樣子,但我的問題是,在x軸的開始和結(jié)束之間沒有空白處。以下是代碼:import csvfrom matplotlib import pyplot as pltfrom datetime import datetime# Get dates and high temperatures from
file.filename = 'sitka_weather_07-2014.csv'with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
#for index, column_header in enumerate(header_row):
#print(index, column_header)
dates, highs = [], []
for row in reader:
current_date = datetime.strptime(row[0], "%Y-%m-%d")
dates.append(current_date)
high = int(row[1])
highs.append(high)# Plot data. fig = plt.figure(dpi=128, figsize=(10,6))plt.plot(dates, highs, c='red')
# Format plot.plt.title("Daily high temperatures, July 2014", fontsize=24)
plt.xlabel('', fontsize=16)fig.autofmt_xdate()plt.ylabel("Temperature (F)", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)plt.show()
3 回答

Cats萌萌
TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊
0.05
0
plt.margins(x=0)
ax.margins(x=0)
plt.rcParams['axes.xmargin'] = 0
y
plt.xlim(..)
ax.set_xlim(..)

守著星空守著你
TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊
大于0的邊距是新引入到matplotlib 2.0中的,所以在以前的版本中,問題總是相反的:“我如何在其中添加一些空白?”我想你永遠(yuǎn)不會(huì)取悅所有人。)我更新了答案,包括一些更多的信息。
添加回答
舉報(bào)
0/150
提交
取消