python使用pyplot繪制橫軸為時間的圖
1. 導(dǎo)入環(huán)境
import numpy as np import matplotlib.pyplot as plt from IPython import display from datetime import datetime from datetime import date
2. 繪圖方法
def myplot(x, y, label=None, xlimit=None, size=(9, 3),fileName=None): display.set_matplotlib_formats('svg') if len(x) == len(y): plt.figure(figsize=size) if xlimit and isinstance(xlimit, tuple): plt.xlim(xlimit) plt.plot(x, y, label=label) if label and isinstance(label, str): plt.legend() if fileName: plt.savefig(fileName) plt.show() else: raise ValueError("x 和 y 的長度不一致!")
3. 繪圖
原始橫坐標(biāo)數(shù)組是一個字符串型的,無法直接用于plot(x, y)
中的x
time[0:10] array([['2019-01-01 00:14:00'], ['2019-01-01 00:29:00'], ['2019-01-01 00:44:00'], ['2019-01-01 00:59:00'], ['2019-01-01 01:14:00'], ['2019-01-01 01:29:00'], ['2019-01-01 01:44:00'], ['2019-01-01 01:59:00'], ['2019-01-01 02:14:00'], ['2019-01-01 02:29:00']], dtype='<U19')
將字符串的時間轉(zhuǎn)換成date
對象
x_time= [datetime.strptime(d, '%Y-%m-%d %H:%M:%S') for d in time]
繪圖
myplot(x_time, y_num, label='car_num', xlimit=(date(2019, 1, 1), date(2019, 1, 22) ), size=(12, 3), fileName='my_dataset-car-num.svg')
4.自定義x軸日期顯示格式
如果想自定義坐標(biāo)軸顯示格式,可以更改一下繪圖方法,通過DateFormatter
來實現(xiàn)。
from matplotlib.dates import DateFormatter def myplot(x, y, label=None, xlimit=None, size=(9, 3),fileName=None): display.set_matplotlib_formats('svg') if len(x) == len(y): plt.figure(figsize=size) if xlimit and isinstance(xlimit, tuple): plt.xlim(xlimit) plt.plot(x, y, label=label) if label and isinstance(label, str): plt.legend() if fileName: plt.savefig(fileName) # ======= 以下是新增代碼 ax = plt.gca() formatter = DateFormatter('%H:%M') ax.xaxis.set_major_formatter(formatter) # 設(shè)置時間顯示格式 # ============== plt.show() else: raise ValueError("x 和 y 的長度不一致!")
效果如下:
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用python進(jìn)行PostgreSQL數(shù)據(jù)庫連接全過程
這篇文章主要介紹了使用python進(jìn)行PostgreSQL數(shù)據(jù)庫連接的相關(guān)資料,包括安裝psycopg2模塊、使用PyCharm進(jìn)行圖形化連接、代碼連接數(shù)據(jù)庫的方法、以及如何執(zhí)行DML和DQL操作,需要的朋友可以參考下2025-03-03從零學(xué)python系列之從文件讀取和保存數(shù)據(jù)
在Python一般都是運用內(nèi)置函數(shù)open()與文件進(jìn)行交互,下面說說具體用法2014-05-05Python TensorFlow 2.6獲取MNIST數(shù)據(jù)的示例代碼
這篇文章主要介紹了Python TensorFlow 2.6獲取MNIST數(shù)據(jù)的的相關(guān)示例,文中有詳細(xì)的代碼示例供大家參考,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04在Lighttpd服務(wù)器中運行Django應(yīng)用的方法
這篇文章主要介紹了在Lighttpd服務(wù)器中運行Django應(yīng)用的方法,本文所采用的是最流行的FastCGI模塊,包括同時運行多個Django應(yīng)用的方法,需要的朋友可以參考下2015-07-07Pycharm安裝第三方庫時Non-zero exit code錯誤解決辦法
這篇文章主要介紹了Pycharm安裝第三方庫時Non-zero exit code錯誤解決辦法,最好的解決辦法可以通過“Pycharm”左下角的“Terminal”,在pycharm內(nèi)使用pip安裝,以安裝“requests”為例,需要的朋友可以參考下2023-01-01python3連接mysql獲取ansible動態(tài)inventory腳本
Ansible Inventory 是包含靜態(tài) Inventory 和動態(tài) Inventory 兩部分的,靜態(tài) Inventory 指的是在文件中指定的主機(jī)和組,動態(tài) Inventory 指通過外部腳本獲取主機(jī)列表。這篇文章主要介紹了python3連接mysql獲取ansible動態(tài)inventory腳本,需要的朋友可以參考下2020-01-01python爬蟲之利用Selenium+Requests爬取拉勾網(wǎng)
這篇文章主要介紹了python爬蟲之利用Selenium+Requests爬取拉勾網(wǎng),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python爬蟲的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04