现在我想通过Python绘制一个期货交易分时图,因为分时图的特点是昨天的夜间交易数据从晚上21:00到23:00开始,全天的交易时间是不连续的。上午的前半部分是9:00-10:15,上午的后半部分是10:30-11:30,下午的时段是1:30-3:00,所以整个时间线是一个不连续的时间数据,Y轴数据是市场的价格。
当简单地绘制价格图时,它是一条连续曲线,如下所示。
当时间作为X轴数据加入时,它是一条不连续的价格曲线,如下图所示。
我想要的是价格曲线是连续的,但是它对应于价格的时间,所以让我们看看是否有一个好的方法来做到这一点。
源代码太大,包含数据,方便您帮忙调试,谢谢!- #coding=utf-8
- from __future__ import division
- import matplotlib.pyplot as plt
- import pandas as pd
-
- duan ="--------------------------" #在控制台断行区别的
-
- if __name__ == "__main__":
-
-
-
- nump_array_date = ['20170808210000' ,'20170808210100' ,'20170808210200' ,'20170808210300'
- ,'20170808210400' ,'20170808210500' ,'20170808210600' ,'20170808210700'
- ,'20170808210800' ,'20170808210900' ,'20170808211000' ,'20170808211100'
- ,'20170808211200' ,'20170808211300' ,'20170808211400' ,'20170808211500'
- ,'20170808211600' ,'20170808211700' ,'20170808211800' ,'20170808211900'
- ,'20170808212000' ,'20170808212100' ,'20170808212200' ,'20170808212300'
- ,'20170808212400' ,'20170808212500' ,'20170808212600' ,'20170808212700'
- ,'20170808212800' ,'20170808212900' ,'20170808213000' ,'20170808213100'
- ,'20170808213200' ,'20170808213300' ,'20170808213400' ,'20170808213500'
- ,'20170808213600' ,'20170808213700' ,'20170808213800' ,'20170808213900'
- ,'20170808214000' ,'20170808214100' ,'20170808214200' ,'20170808214300'
- ,'20170808214400' ,'20170808214500' ,'20170808214600' ,'20170808214700'
- ,'20170808214800' ,'20170808214900' ,'20170808215000' ,'20170808215100'
- ,'20170808215200' ,'20170808215300' ,'20170808215400' ,'20170808215500'
- ,'20170808215600' ,'20170808215700' ,'20170808215800' ,'20170808215900'
- ,'20170808220000' ,'20170808220100' ,'20170808220200' ,'20170808220300'
- ,'20170808220400' ,'20170808220500' ,'20170808220600' ,'20170808220700'
- ,'20170808220800' ,'20170808220900' ,'20170808221000' ,'20170808221100'
- ,'20170808221200' ,'20170808221300' ,'20170808221400' ,'20170808221500'
- ,'20170808221600' ,'20170808221700' ,'20170808221800' ,'20170808221900'
- ,'20170808222000' ,'20170808222100' ,'20170808222200' ,'20170808222300'
- ,'20170808222400' ,'20170808222500' ,'20170808222600' ,'20170808222700'
- ,'20170808222800' ,'20170808222900' ,'20170808223000' ,'20170808223100'
- ,'20170808223200' ,'20170808223300' ,'20170808223400' ,'20170808223500'
- ,'20170808223600' ,'20170808223700' ,'20170808223800' ,'20170808223900'
- ,'20170808224000' ,'20170808224100' ,'20170808224200' ,'20170808224300'
- ,'20170808224400' ,'20170808224500' ,'20170808224600' ,'20170808224700'
- ,'20170808224800' ,'20170808224900' ,'20170808225000' ,'20170808225100'
- ,'20170808225200' ,'20170808225300' ,'20170808225400' ,'20170808225500'
- ,'20170808225600' ,'20170808225700' ,'20170808225800' ,'20170808225900'
- ,'20170808230000' ,'20170809090000' ,'20170809090100' ,'20170809090200'
- ,'20170809090300' ,'20170809090400' ,'20170809090500' ,'20170809090600'
- ,'20170809090700' ,'20170809090800' ,'20170809090900' ,'20170809091000'
- ,'20170809091100' ,'20170809091200' ,'20170809091300' ,'20170809091400'
- ,'20170809091500' ,'20170809091600' ,'20170809091700' ,'20170809091800'
- ,'20170809091900' ,'20170809092000' ,'20170809092100' ,'20170809092200'
- ,'20170809092300' ,'20170809092400' ,'20170809092500' ,'20170809092600'
- ,'20170809092700' ,'20170809092800' ,'20170809092900' ,'20170809093000'
- ,'20170809093100' ,'20170809093200' ,'20170809093300' ,'20170809093400'
- ,'20170809093500' ,'20170809093600' ,'20170809093700' ,'20170809093800'
- ,'20170809093900' ,'20170809094000' ,'20170809094100' ,'20170809094200'
- ,'20170809094300' ,'20170809094400' ,'20170809094500' ,'20170809094600'
- ,'20170809094700' ,'20170809094800' ,'20170809094900' ,'20170809095000'
- ,'20170809095100' ,'20170809095200' ,'20170809095300' ,'20170809095400'
- ,'20170809095500' ,'20170809095600' ,'20170809095700' ,'20170809095800'
- ,'20170809095900' ,'20170809100000' ,'20170809100100' ,'20170809100200'
- ,'20170809100300' ,'20170809100400' ,'20170809100500' ,'20170809100600'
- ,'20170809100700' ,'20170809100800' ,'20170809100900' ,'20170809101000'
- ,'20170809101100' ,'20170809101200' ,'20170809101300' ,'20170809101400'
- ,'20170809103000' ,'20170809103100' ,'20170809103200' ,'20170809103300'
- ,'20170809103400' ,'20170809103500' ,'20170809103600' ,'20170809103700'
- ,'20170809103800' ,'20170809103900' ,'20170809104000' ,'20170809104100'
- ,'20170809104200' ,'20170809104300' ,'20170809104400' ,'20170809104500'
- ,'20170809104600' ,'20170809104700' ,'20170809104800' ,'20170809104900'
- ,'20170809105000' ,'20170809105100' ,'20170809105200' ,'20170809105300'
- ,'20170809105400' ,'20170809105500' ,'20170809105600' ,'20170809105700'
- ,'20170809105800' ,'20170809105900' ,'20170809110000' ,'20170809110100'
- ,'20170809110200' ,'20170809110300' ,'20170809110400' ,'20170809110500'
- ,'20170809110600' ,'20170809110700' ,'20170809110800' ,'20170809110900'
- ,'20170809111000' ,'20170809111100' ,'20170809111200' ,'20170809111300'
- ,'20170809111400' ,'20170809111500' ,'20170809111600' ,'20170809111700'
- ,'20170809111800' ,'20170809111900' ,'20170809112000' ,'20170809112100'
- ,'20170809112200' ,'20170809112300' ,'20170809112400' ,'20170809112500'
- ,'20170809112600' ,'20170809112700' ,'20170809112800' ,'20170809112900'
- ,'20170809133000' ,'20170809133100' ,'20170809133200' ,'20170809133300'
- ,'20170809133400' ,'20170809133500' ,'20170809133600' ,'20170809133700'
- ,'20170809133800' ,'20170809133900' ,'20170809134000' ,'20170809134100'
- ,'20170809134200' ,'20170809134300' ,'20170809134400' ,'20170809134500'
- ,'20170809134600' ,'20170809134700' ,'20170809134800' ,'20170809134900'
- ,'20170809135000' ,'20170809135100' ,'20170809135200' ,'20170809135300'
- ,'20170809135400' ,'20170809135500' ,'20170809135600' ,'20170809135700'
- ,'20170809135800' ,'20170809135900' ,'20170809140000' ,'20170809140100'
- ,'20170809140200' ,'20170809140300' ,'20170809140400' ,'20170809140500'
- ,'20170809140600' ,'20170809140700' ,'20170809140800' ,'20170809140900'
- ,'20170809141000' ,'20170809141100' ,'20170809141200' ,'20170809141300'
- ,'20170809141400' ,'20170809141500' ,'20170809141600' ,'20170809141700'
- ,'20170809141800' ,'20170809141900' ,'20170809142000' ,'20170809142100'
- ,'20170809142200' ,'20170809142300' ,'20170809142400' ,'20170809142500'
- ,'20170809142600' ,'20170809142700' ,'20170809142800' ,'20170809142900'
- ,'20170809143000' ,'20170809143100' ,'20170809143200' ,'20170809143300'
- ,'20170809143400' ,'20170809143500' ,'20170809143600' ,'20170809143700'
- ,'20170809143800' ,'20170809143900' ,'20170809144000' ,'20170809144100'
- ,'20170809144200' ,'20170809144300' ,'20170809144400' ,'20170809144500'
- ,'20170809144600' ,'20170809144700' ,'20170809144800' ,'20170809144900'
- ,'20170809145000' ,'20170809145100' ,'20170809145200' ,'20170809145300'
- ,'20170809145400' ,'20170809145500' ,'20170809145600' ,'20170809145700'
- ,'20170809145800' ,'20170809145900']
-
- nump_array_date= pd.to_datetime(nump_array_date) # convert str to date
- nump_array_price = [3900.0, 3903.0, 3891.0, 3888.0, 3893.0, 3895.0, 3899.0, 3906.0, 3914.0, 3911.0,
- 3912.0, 3910.0, 3914.0, 3920.0, 3920.0, 3915.0, 3915.0, 3915.0, 3911.0, 3903.0,
- 3901.0, 3899.0, 3894.0, 3901.0, 3901.0, 3897.0, 3891.0, 3882.0, 3878.0, 3881.0,
- 3878.0, 3885.0, 3886.0, 3889.0, 3887.0, 3887.0, 3886.0, 3885.0, 3886.0, 3887.0,
- 3894.0, 3888.0, 3890.0, 3887.0, 3888.0, 3883.0, 3880.0, 3885.0, 3887.0, 3882.0,
- 3882.0, 3887.0, 3886.0, 3885.0, 3890.0, 3891.0, 3887.0, 3890.0, 3886.0, 3891.0,
- 3888.0, 3891.0, 3881.0, 3878.0, 3877.0, 3875.0, 3871.0, 3872.0, 3879.0, 3876.0,
- 3879.0, 3885.0, 3884.0, 3883.0, 3879.0, 3877.0, 3880.0, 3878.0, 3882.0, 3885.0,
- 3883.0, 3884.0, 3883.0, 3881.0, 3882.0, 3889.0, 3896.0, 3891.0, 3897.0, 3905.0,
- 3901.0, 3902.0, 3899.0, 3897.0, 3896.0, 3899.0, 3902.0, 3902.0, 3905.0, 3913.0,
- 3910.0, 3909.0, 3902.0, 3901.0, 3902.0, 3897.0, 3903.0, 3902.0, 3901.0, 3900.0,
- 3903.0, 3906.0, 3906.0, 3909.0, 3904.0, 3902.0, 3902.0, 3902.0, 3904.0, 3909.0,
- 3909.0, 3941.0, 3934.0, 3947.0, 3938.0, 3939.0, 3938.0, 3932.0, 3930.0, 3929.0,
- 3924.0, 3930.0, 3930.0, 3926.0, 3929.0, 3918.0, 3914.0, 3912.0, 3908.0, 3912.0,
- 3913.0, 3910.0, 3915.0, 3916.0, 3913.0, 3915.0, 3918.0, 3913.0, 3908.0, 3912.0,
- 3911.0, 3916.0, 3913.0, 3915.0, 3918.0, 3917.0, 3916.0, 3920.0, 3920.0, 3917.0,
- 3916.0, 3912.0, 3913.0, 3909.0, 3911.0, 3910.0, 3907.0, 3908.0, 3901.0, 3907.0,
- 3908.0, 3909.0, 3910.0, 3909.0, 3911.0, 3912.0, 3914.0, 3915.0, 3913.0, 3919.0,
- 3917.0, 3915.0, 3918.0, 3919.0, 3918.0, 3926.0, 3925.0, 3925.0, 3927.0, 3923.0,
- 3926.0, 3926.0, 3920.0, 3921.0, 3919.0, 3919.0, 3917.0, 3921.0, 3924.0, 3922.0,
- 3921.0, 3923.0, 3922.0, 3922.0, 3927.0, 3928.0, 3928.0, 3929.0, 3926.0, 3927.0,
- 3928.0, 3926.0, 3922.0, 3912.0, 3911.0, 3908.0, 3912.0, 3910.0, 3913.0, 3905.0,
- 3910.0, 3904.0, 3893.0, 3896.0, 3898.0, 3896.0, 3903.0, 3905.0, 3905.0, 3907.0,
- 3906.0, 3909.0, 3910.0, 3910.0, 3913.0, 3911.0, 3911.0, 3914.0, 3913.0, 3908.0,
- 3913.0, 3910.0, 3910.0, 3911.0, 3914.0, 3918.0, 3917.0, 3917.0, 3919.0, 3919.0,
- 3917.0, 3922.0, 3926.0, 3924.0, 3927.0, 3925.0, 3940.0, 3940.0, 3943.0, 3949.0,
- 3953.0, 3951.0, 3951.0, 3953.0, 3950.0, 3957.0, 3964.0, 3964.0, 3960.0, 3958.0,
- 3963.0, 3963.0, 3956.0, 3959.0, 3959.0, 3957.0, 3961.0, 3960.0, 3960.0, 3963.0,
- 3972.0, 3971.0, 3974.0, 3982.0, 3980.0, 3981.0, 3969.0, 3970.0, 3970.0, 3972.0,
- 3968.0, 3968.0, 3970.0, 3974.0, 3973.0, 3974.0, 3971.0, 3972.0, 3978.0, 3982.0,
- 3975.0, 3971.0, 3970.0, 3972.0, 3971.0, 3970.0, 3973.0, 3973.0, 3976.0, 3976.0,
- 3975.0, 3981.0, 3980.0, 3979.0, 3979.0, 3984.0, 3980.0, 3977.0, 3983.0, 3984.0,
- 3984.0, 3980.0, 3979.0, 3979.0, 3978.0, 3978.0, 3978.0, 3979.0, 3983.0, 3978.0,
- 3978.0, 3981.0, 3984.0, 3990.0, 3993.0, 3997.0, 4001.0, 4000.0, 3997.0, 4003.0,
- 4003.0, 4000.0, 4002.0, 3991.0, 3994.0, 4006.0,]
-
-
-
- fig, ax = plt.subplots()
-
- # plt.plot(nump_array_date,nump_array_price)
- plt.plot(nump_array_price)
- plt.show()
-
-
复制代码
这应该是楼主想要的效果吧?细节可以调整。 - df=pd.DataFrame({'date':nump_array_date,'price':nump_array_price})
- df_night=df[df.date<'2017-08-09 09:00:00']
- df_0900=df[(df.date>='2017-08-09 09:00:00')&(df.date<='2017-08-09 10:15:00')]
- df_1030=df[(df.date>='2017-08-09 10:30:00')&(df.date<='2017-08-09 11:30:00')]
- df_1330=df[(df.date>='2017-08-09 13:30:00')&(df.date<='2017-08-09 15:00:00')]
- df_all=df_0900.append(df_1030)
- df_final=df_all.append(df_1330)
-
- x_tk=[]
- x_lb=[]
-
- for i in range(0,len(df_final.date.tolist())):
- x_tk.append(i)
- if i % 7==0:
- x_lb.append(df_final.date.tolist()[i])
- else:
- x_lb.append("")
-
- plt.plot(x_tk,df_final.price)
- plt.xticks(x_tk,(x_lb),rotation=80)
- plt.show()
复制代码
|