matplotlib 设定坐标的上下限:matplotlib.pyplot.xlim

《Python编程:从入门到实践》袁国忠译 , 第16章 第318页

运行该页的代码得到的图形和书上的图不一样,可能和matplotlib的版本有关。

书上的图是这样:


而直接运行该页程序得到的图是这样:


这时候需要加上设定x轴范围的语句:plt.xlim(dates[0] , dates[-1] )。

整个程序如下:

import csv
import matplotlib.pyplot as plt
from datetime import datetime


filename = 'sitka_weather_07-2014.csv'
with open(filename) as f:
    reader0 = csv.reader(f)
    head = next(reader0)
    #print ( head )
    highs = []
    dates = []
    for row in reader0:
        highs.append( int( row[1]) )
        date = datetime.strptime (  row[0] , "%Y-%m-%d" )
        dates.append( date )
        
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, highs, c='red')
# 设置图形的格式
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.xlim(dates[0] , dates[-1] )
#plt.show()
plt.savefig('fig/july.png', bbox_inches='tight') 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值