使用seaborn

时间:2016-08-04 12:23:23

标签: python seaborn

我用seaborn做了一个情节, 这是我的pltt enter image description here 我想为每一行添加一个标签。 你能帮帮我吗?

import numpy as np
import matplotlib.pylab as plt
import matplotlib.dates as mdates


from matplotlib import style
import pandas as pd
%pylab inline
import seaborn as sns
sns.set_style('darkgrid')
import io
style.use('ggplot')
from datetime import datetime
import time
fig = plt.figure(figsize=(12, 8), dpi=100)
ax1 = fig.add_subplot(111)
x1 = pd.to_datetime(df_no_missing.TIMESTAMP, format="%h:%m")
y1 = df_no_missing.P_ACT_KW
y3 = df_no_missing.P_SOUSCR
yearFmt = mdates.DateFormatter("%H:%M:%S")
ax1.xaxis.set_major_formatter(yearFmt)
ax2 = ax1.twinx()

ax1.plot(x, y1, 'g-')
ax2.plot(x, y2, 'b-')
ax1.plot(x, y3, 'r-')
ax1.set_xlabel('temps')
ax1.set_ylabel('puissance', color='g')
ax2.set_ylabel('dépassement', color='b')
plt.ylim(plt.ylim()[0], 1.0)
plt.show()

提前谢谢你 的修改

我试着提一下:

fig = plt.figure(figsize=(12, 5), dpi=100)
ax1 = fig.add_subplot(111)
x1 = pd.to_datetime(df_no_missing.TIMESTAMP, format="%h:%m")
y1 = df_no_missing.P_ACT_KW
y3 = df_no_missing.P_SOUSCR
yearFmt = mdates.DateFormatter("%H:%M:%S")
ax1.xaxis.set_major_formatter(yearFmt)
ax2 = ax1.twinx()
ax1.plot(x, y1, 'g-', label='label 1')
ax2.plot(x, y2, 'b-', label='label 2')
ax1.plot(x, y3, 'r-', label='label 3')
ax1.plot(0, 0, 'b-', label='label 2')
ax.legend(loc=0)             # add legend in top right corner
ax.grid()                    # show grid lines
ax1.set_xlabel('temps')
ax1.set_ylabel('puissance', color='g')
ax2.set_ylabel('dépassement', color='b')
plt.ylim(plt.ylim()[0], 1.0)
plt.show()

但我收到了这个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\Demonstrator\Anaconda3\lib\site-packages\IPython\core\formatters.py
     调用中的

(self,obj)           337传球           338其他:        - > 339返回打印机(obj)           340#最后寻找特殊的方法名称           341 method = _safe_get_formatter_method(obj,self.print_method)

C:\Users\Demonstrator\Anaconda3\lib\site-packages\IPython\core\pylabtools.py
     

in(fig)           226           227如果' png'格式:        - > 228 png_formatter.for_type(图,lambda fig:print_figure(图,' png',** kwargs))           229如果' retina'格式或' png2x'格式:           230 png_formatter.for_type(图,lambda fig:retina_figure(图,** kwargs))

C:\Users\Demonstrator\Anaconda3\lib\site-packages\IPython\core\pylabtools.py
     print_figure中的

(图,fmt,bbox_inches,** kwargs)           117           118 bytes_io = BytesIO()        - > 119 fig.canvas.print_figure(bytes_io,** kw)           120 data = bytes_io.getvalue()           121如果fmt ==' svg':

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\backend_bases.py
     print_figure中的

(self,filename,dpi,facecolor,edgecolor,   方向,格式,** kwargs)          2178方向=方向,          2179 dryrun = True,        - > 2180 ** kwargs)          2181 renderer = self.figure._cachedRenderer          2182 bbox_inches = self.figure.get_tightbbox(渲染器)

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py
     print_png中的

(self,filename_or_obj,* args,** kwargs)           525           526 def print_png(self,filename_or_obj,* args,** kwargs):        - > 527 FigureCanvasAgg.draw(self)           528 renderer = self.get_renderer()           529 original_dpi = renderer.dpi

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py
     

平局(个体经营)           472           473尝试:        - > 474 self.figure.draw(self.renderer)           475终于:           476 RendererAgg.lock.release()

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\artist.py
     draw_wrapper中的

(艺术家,渲染器,* args,** kwargs)            59 def draw_wrapper(艺术家,渲染器,* args,** kwargs):            60之前(艺术家,渲染器)       ---> 61画(艺术家,渲染者,* args,** kwargs)            62之后(艺术家,渲染器)            63

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\figure.py
     画中的

(自我,渲染器)          1157 dsu.sort(key = itemgetter(0))          1158 for zorder,a,func,args in dsu:        - > 1159 func(* args)          1160          1161 renderer.close_group(' figure')

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\artist.py
     draw_wrapper中的

(艺术家,渲染器,* args,** kwargs)            59 def draw_wrapper(艺术家,渲染器,* args,** kwargs):            60之前(艺术家,渲染器)       ---> 61画(艺术家,渲染者,* args,** kwargs)            62之后(艺术家,渲染器)            63

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\axes\_base.py
     画中的

(self,renderer,inframe)          2322          2323 for zorder,a in dsu:        - > 2324 a.draw(渲染器)          2325          2326 renderer.close_group(' axes')

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\artist.py
     draw_wrapper中的

(艺术家,渲染器,* args,** kwargs)            59 def draw_wrapper(艺术家,渲染器,* args,** kwargs):            60之前(艺术家,渲染器)       ---> 61画(艺术家,渲染者,* args,** kwargs)            62之后(艺术家,渲染器)            63

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\axis.py
     画中的

(自我,渲染器,* args,** kwargs)          1104 renderer.open_group(名称)          1105        - > 1106 ticks_to_draw = self._update_ticks(渲染器)          1107 ticklabelBoxes,ticklabelBoxes2 = self._get_tick_bboxes(ticks_to_draw,          1108渲染器)

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\axis.py
     _update_ticks中的

(self,renderer)           947           948 interval = self.get_view_interval()        - > 949 tick_tups = [t for self.iter_ticks()]           950如果self._smart_bounds:           951#句柄倒置限制

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\axis.py
     

in(.0)           947           948 interval = self.get_view_interval()        - > 949 tick_tups = [t for self.iter_ticks()]           950如果self._smart_bounds:           951#句柄倒置限制

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\axis.py
     

in iter_ticks(self)           890遍历所有主要和次要刻度。           891"""        - > 892 majorLocs = self.major.locator()           893 majorTicks = self.get_major_ticks(len(majorLocs))           894 self.major.formatter.set_locs(majorLocs)

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\dates.py
     致电

(个体经营)          1004 def 通话(自我):          1005'返回刻度线的位置'        - > 1006 self.refresh()          1007返回self._locator()          1008

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\dates.py
     刷新中的

(个体经营)          1024 def刷新(个体积):          1025'根据当前限制刷新内部信息。'        - > 1026 dmin,dmax = self.viewlim_to_dt()          1027 self._locator = self.get_locator(dmin,dmax)          1028

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\dates.py
     

在viewlim_to_dt(个体经营)           768 vmin,vmax = vmax,vmin           769        - > 770返回num2date(vmin,self.tz),num2date(vmax,self.tz)           771           772 def _get_unit(self):

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\dates.py
     num2date(x,tz)中的

          417 tz = _get_rc_timezone()           418如果不是cbook.iterable(x):        - > 419 return _from_ordinalf(x,tz)           420其他:           421 x = np.asarray(x)

C:\Users\Demonstrator\Anaconda3\lib\site-packages\matplotlib\dates.py
     _from_ordinalf(x,tz)中的

          269           270 ix = int(x)        - > 271 dt = datetime.datetime.fromordinal(ix).replace(tzinfo = UTC)           272           273余数=浮点数(x) - ix

ValueError: ordinal must be >= 1





<matplotlib.figure.Figure at 0x1d3b041a400>
 print (df_no_missing.head())
              TIMESTAMP  P_ACT_KW PERIODE_TARIF  P_SOUSCR       SITE  \
145 2015-08-01 23:10:00     248.0            HC     425.0  ST GEREON   
146 2015-08-01 23:20:00     244.0            HC     425.0  ST GEREON   
147 2015-08-01 23:30:00     243.0            HC     425.0  ST GEREON   
148 2015-08-01 23:40:00     238.0            HC     425.0  ST GEREON   
149 2015-08-01 23:50:00     234.0            HC     425.0  ST GEREON   

          TARIF  depassement        date      time  
145  TURPE_HTA5          0.0  2015-08-01  23:10:00  
146  TURPE_HTA5          0.0  2015-08-01  23:20:00  
147  TURPE_HTA5          0.0  2015-08-01  23:30:00  
148  TURPE_HTA5          0.0  2015-08-01  23:40:00  
149  TURPE_HTA5          0.0  2015-08-01  23:50:00

1 个答案:

答案 0 :(得分:0)

您可以为每个子地图轴指定label名称,并使用plt.legend将相应的图例添加到中间右上角。

fig = sns.plt.figure(figsize=(12, 5), dpi=100)
ax1 = fig.add_subplot(111)
x1 = pd.to_datetime(df_no_missing.TIMESTAMP)
y1 = df_no_missing.P_ACT_KW
y2 = df_no_missing.depassement
y3 = df_no_missing.P_SOUSCR
yearFmt = mdates.DateFormatter("%H:%M:%S")
ax1.xaxis.set_major_formatter(yearFmt)
ax1.plot(x1, y1, 'g-', label='p_act_kw')
ax1.plot(x1, y3, 'r-', label='p_souscr')
ax2 = ax1.twinx()
ax2.plot(x1, y2, 'b-', label='depassement')
h1, l1 = ax1.get_legend_handles_labels()
h2, l2 = ax2.get_legend_handles_labels()
ax1.legend(h1+h2, l1+l2, loc='center right')
ax1.set_xlabel('temps')
ax1.set_ylabel('puissance', color='g')
ax2.set_ylabel('dépassement', color='b')
sns.plt.ylim(plt.ylim()[0], 1.0)
sns.plt.show()

enter image description here