簡體   English   中英

Matplotlib不同比例的Y軸

[英]Matplotlib Different Scaled Y-Axes

我有一個數據框,下面的數據。

ex_dict = {'revenue': [613663,  1693667,  2145183,  2045065,  2036406,  
1708862,  1068232,  1196899,  2185852,  2165778,  2144738,  2030337,  
1784067],
'abs_percent_diff': [0.22279211315310588,  0.13248909660765254,  
0.12044821447874667,  0.09438674840975962,  0.1193588387687364,  
0.062100921139322744,  0.05875297161175445,  0.06240362963749895,  
0.05085338590212515,  0.034877614941165744,  0.012263947005671703,  
0.029227374323993634,  0.023411816504907524],
'ds': [dt.date(2017,1,1),  dt.date(2017,1,2),  dt.date(2017,1,3),  
dt.date(2017,1,4),  dt.date(2017,1,5),  dt.date(2017,1,6),  
dt.date(2017,1,7),  dt.date(2017,1,8),  dt.date(2017,1,9),  
dt.date(2017,1,10),  dt.date(2017,1,11),  dt.date(2017,1,12),  
dt.date(2017,1,13)], 
'yhat_normal': [501853.9074623253,  1952329.3521464923,  1914575.7673396615,  
1868685.8215084015,  1819261.1068672044,  1608945.031482406,  
1008953.0123101478,  1126595.36037955,  2302965.598289115,  
2244044.9351591542,  2171367.536396199,  2091465.0313570146,  
1826836.562382966]}

df_vis=pd.DataFrame.from_dict(ex_dict)

我想在同一y軸上繪制yhat_normalrevenue ,在y軸上abs_percent_diff ,比例不同。

df_vis = df_vis.set_index('ds')
df_vis[['rev', 'yhat_normal']].plot(figsize=(20, 12))

我可以使用上面的代碼輕松地繪制revyhat_normal圖形,但是我正努力在不同的y軸比例上獲得abs_percent_diff 我嘗試將列轉換為numpy數組並執行此操作,但這看起來很糟糕。

npdate = df_vis.as_matrix(columns= ['ds'])
nppredictions = df_vis.as_matrix(columns= ['yhat_normal'])
npactuals = df_vis.as_matrix(columns= ['rev'])
npmape = df_vis.as_matrix(columns=['abs_percent_diff'])

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()
fig.set_size_inches(20,10)
ax1.plot_date(npdate, nppredictions, ls= '-', color= 'b')
ax1.plot_date(npdate, npactuals, ls='-', color='g')
ax2.plot_date(npdate, npmape, 'r-')

ax1.set_xlabel('X data')
ax1.set_ylabel('Y1 data', color='g')
ax2.set_ylabel('Y2 data', color='b')

plt.show()

這就是我要的。 紅線是abs_percent_diff。 顯然,我是手工繪制的,所以不准確。 在此處輸入圖片說明

我不確定問題是否解決,但您似乎只是想在繪圖區域的底部繪制數據框列之一。

import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt

ex_dict = {'revenue': [613663,  1693667,  2145183,  2045065,  2036406,  
1708862,  1068232,  1196899,  2185852,  2165778,  2144738,  2030337,  
1784067],
'abs_percent_diff': [0.22279211315310588,  0.13248909660765254,  
0.12044821447874667,  0.09438674840975962,  0.1193588387687364,  
0.062100921139322744,  0.05875297161175445,  0.06240362963749895,  
0.05085338590212515,  0.034877614941165744,  0.012263947005671703,  
0.029227374323993634,  0.023411816504907524],
'ds': [dt.date(2017,1,1),  dt.date(2017,1,2),  dt.date(2017,1,3),  
dt.date(2017,1,4),  dt.date(2017,1,5),  dt.date(2017,1,6),  
dt.date(2017,1,7),  dt.date(2017,1,8),  dt.date(2017,1,9),  
dt.date(2017,1,10),  dt.date(2017,1,11),  dt.date(2017,1,12),  
dt.date(2017,1,13)], 
'yhat_normal': [501853.9074623253,  1952329.3521464923,  1914575.7673396615,  
1868685.8215084015,  1819261.1068672044,  1608945.031482406,  
1008953.0123101478,  1126595.36037955,  2302965.598289115,  
2244044.9351591542,  2171367.536396199,  2091465.0313570146,  
1826836.562382966]}

df_vis=pd.DataFrame.from_dict(ex_dict)

df_vis = df_vis.set_index('ds')
ax = df_vis[['revenue','yhat_normal']].plot(figsize=(13, 8))
ax2 = df_vis['abs_percent_diff'].plot(secondary_y=True, ax=ax)
ax2.set_ylim(0,1)


plt.show()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM