小白教程

 找回密码
 立即注册
查看: 10042|回复: 1

[已解决]各位大神求助python matplotlib 时间序列绘图问题

[复制链接]

2

主题

3

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2021-5-31 20:29:03 | 显示全部楼层 |阅读模式
现在我想通过Python绘制一个期货交易分时图,因为分时图的特点是昨天的夜间交易数据从晚上21:00到23:00开始,全天的交易时间是不连续的。上午的前半部分是9:00-10:15,上午的后半部分是10:30-11:30,下午的时段是1:30-3:00,所以整个时间线是一个不连续的时间数据,Y轴数据是市场的价格。
当简单地绘制价格图时,它是一条连续曲线,如下所示。

当时间作为X轴数据加入时,它是一条不连续的价格曲线,如下图所示。

我想要的是价格曲线是连续的,但是它对应于价格的时间,所以让我们看看是否有一个好的方法来做到这一点。
源代码太大,包含数据,方便您帮忙调试,谢谢!
  1. #coding=utf-8
  2. from __future__ import division
  3. import matplotlib.pyplot as plt
  4. import pandas as pd

  5. duan ="--------------------------"  #在控制台断行区别的     

  6. if __name__ == "__main__":  
  7.         
  8.    
  9.      
  10.     nump_array_date  = ['20170808210000' ,'20170808210100' ,'20170808210200' ,'20170808210300'
  11. ,'20170808210400' ,'20170808210500' ,'20170808210600' ,'20170808210700'
  12. ,'20170808210800' ,'20170808210900' ,'20170808211000' ,'20170808211100'
  13. ,'20170808211200' ,'20170808211300' ,'20170808211400' ,'20170808211500'
  14. ,'20170808211600' ,'20170808211700' ,'20170808211800' ,'20170808211900'
  15. ,'20170808212000' ,'20170808212100' ,'20170808212200' ,'20170808212300'
  16. ,'20170808212400' ,'20170808212500' ,'20170808212600' ,'20170808212700'
  17. ,'20170808212800' ,'20170808212900' ,'20170808213000' ,'20170808213100'
  18. ,'20170808213200' ,'20170808213300' ,'20170808213400' ,'20170808213500'
  19. ,'20170808213600' ,'20170808213700' ,'20170808213800' ,'20170808213900'
  20. ,'20170808214000' ,'20170808214100' ,'20170808214200' ,'20170808214300'
  21. ,'20170808214400' ,'20170808214500' ,'20170808214600' ,'20170808214700'
  22. ,'20170808214800' ,'20170808214900' ,'20170808215000' ,'20170808215100'
  23. ,'20170808215200' ,'20170808215300' ,'20170808215400' ,'20170808215500'
  24. ,'20170808215600' ,'20170808215700' ,'20170808215800' ,'20170808215900'
  25. ,'20170808220000' ,'20170808220100' ,'20170808220200' ,'20170808220300'
  26. ,'20170808220400' ,'20170808220500' ,'20170808220600' ,'20170808220700'
  27. ,'20170808220800' ,'20170808220900' ,'20170808221000' ,'20170808221100'
  28. ,'20170808221200' ,'20170808221300' ,'20170808221400' ,'20170808221500'
  29. ,'20170808221600' ,'20170808221700' ,'20170808221800' ,'20170808221900'
  30. ,'20170808222000' ,'20170808222100' ,'20170808222200' ,'20170808222300'
  31. ,'20170808222400' ,'20170808222500' ,'20170808222600' ,'20170808222700'
  32. ,'20170808222800' ,'20170808222900' ,'20170808223000' ,'20170808223100'
  33. ,'20170808223200' ,'20170808223300' ,'20170808223400' ,'20170808223500'
  34. ,'20170808223600' ,'20170808223700' ,'20170808223800' ,'20170808223900'
  35. ,'20170808224000' ,'20170808224100' ,'20170808224200' ,'20170808224300'
  36. ,'20170808224400' ,'20170808224500' ,'20170808224600' ,'20170808224700'
  37. ,'20170808224800' ,'20170808224900' ,'20170808225000' ,'20170808225100'
  38. ,'20170808225200' ,'20170808225300' ,'20170808225400' ,'20170808225500'
  39. ,'20170808225600' ,'20170808225700' ,'20170808225800' ,'20170808225900'
  40. ,'20170808230000' ,'20170809090000' ,'20170809090100' ,'20170809090200'
  41. ,'20170809090300' ,'20170809090400' ,'20170809090500' ,'20170809090600'
  42. ,'20170809090700' ,'20170809090800' ,'20170809090900' ,'20170809091000'
  43. ,'20170809091100' ,'20170809091200' ,'20170809091300' ,'20170809091400'
  44. ,'20170809091500' ,'20170809091600' ,'20170809091700' ,'20170809091800'
  45. ,'20170809091900' ,'20170809092000' ,'20170809092100' ,'20170809092200'
  46. ,'20170809092300' ,'20170809092400' ,'20170809092500' ,'20170809092600'
  47. ,'20170809092700' ,'20170809092800' ,'20170809092900' ,'20170809093000'
  48. ,'20170809093100' ,'20170809093200' ,'20170809093300' ,'20170809093400'
  49. ,'20170809093500' ,'20170809093600' ,'20170809093700' ,'20170809093800'
  50. ,'20170809093900' ,'20170809094000' ,'20170809094100' ,'20170809094200'
  51. ,'20170809094300' ,'20170809094400' ,'20170809094500' ,'20170809094600'
  52. ,'20170809094700' ,'20170809094800' ,'20170809094900' ,'20170809095000'
  53. ,'20170809095100' ,'20170809095200' ,'20170809095300' ,'20170809095400'
  54. ,'20170809095500' ,'20170809095600' ,'20170809095700' ,'20170809095800'
  55. ,'20170809095900' ,'20170809100000' ,'20170809100100' ,'20170809100200'
  56. ,'20170809100300' ,'20170809100400' ,'20170809100500' ,'20170809100600'
  57. ,'20170809100700' ,'20170809100800' ,'20170809100900' ,'20170809101000'
  58. ,'20170809101100' ,'20170809101200' ,'20170809101300' ,'20170809101400'
  59. ,'20170809103000' ,'20170809103100' ,'20170809103200' ,'20170809103300'
  60. ,'20170809103400' ,'20170809103500' ,'20170809103600' ,'20170809103700'
  61. ,'20170809103800' ,'20170809103900' ,'20170809104000' ,'20170809104100'
  62. ,'20170809104200' ,'20170809104300' ,'20170809104400' ,'20170809104500'
  63. ,'20170809104600' ,'20170809104700' ,'20170809104800' ,'20170809104900'
  64. ,'20170809105000' ,'20170809105100' ,'20170809105200' ,'20170809105300'
  65. ,'20170809105400' ,'20170809105500' ,'20170809105600' ,'20170809105700'
  66. ,'20170809105800' ,'20170809105900' ,'20170809110000' ,'20170809110100'
  67. ,'20170809110200' ,'20170809110300' ,'20170809110400' ,'20170809110500'
  68. ,'20170809110600' ,'20170809110700' ,'20170809110800' ,'20170809110900'
  69. ,'20170809111000' ,'20170809111100' ,'20170809111200' ,'20170809111300'
  70. ,'20170809111400' ,'20170809111500' ,'20170809111600' ,'20170809111700'
  71. ,'20170809111800' ,'20170809111900' ,'20170809112000' ,'20170809112100'
  72. ,'20170809112200' ,'20170809112300' ,'20170809112400' ,'20170809112500'
  73. ,'20170809112600' ,'20170809112700' ,'20170809112800' ,'20170809112900'
  74. ,'20170809133000' ,'20170809133100' ,'20170809133200' ,'20170809133300'
  75. ,'20170809133400' ,'20170809133500' ,'20170809133600' ,'20170809133700'
  76. ,'20170809133800' ,'20170809133900' ,'20170809134000' ,'20170809134100'
  77. ,'20170809134200' ,'20170809134300' ,'20170809134400' ,'20170809134500'
  78. ,'20170809134600' ,'20170809134700' ,'20170809134800' ,'20170809134900'
  79. ,'20170809135000' ,'20170809135100' ,'20170809135200' ,'20170809135300'
  80. ,'20170809135400' ,'20170809135500' ,'20170809135600' ,'20170809135700'
  81. ,'20170809135800' ,'20170809135900' ,'20170809140000' ,'20170809140100'
  82. ,'20170809140200' ,'20170809140300' ,'20170809140400' ,'20170809140500'
  83. ,'20170809140600' ,'20170809140700' ,'20170809140800' ,'20170809140900'
  84. ,'20170809141000' ,'20170809141100' ,'20170809141200' ,'20170809141300'
  85. ,'20170809141400' ,'20170809141500' ,'20170809141600' ,'20170809141700'
  86. ,'20170809141800' ,'20170809141900' ,'20170809142000' ,'20170809142100'
  87. ,'20170809142200' ,'20170809142300' ,'20170809142400' ,'20170809142500'
  88. ,'20170809142600' ,'20170809142700' ,'20170809142800' ,'20170809142900'
  89. ,'20170809143000' ,'20170809143100' ,'20170809143200' ,'20170809143300'
  90. ,'20170809143400' ,'20170809143500' ,'20170809143600' ,'20170809143700'
  91. ,'20170809143800' ,'20170809143900' ,'20170809144000' ,'20170809144100'
  92. ,'20170809144200' ,'20170809144300' ,'20170809144400' ,'20170809144500'
  93. ,'20170809144600' ,'20170809144700' ,'20170809144800' ,'20170809144900'
  94. ,'20170809145000' ,'20170809145100' ,'20170809145200' ,'20170809145300'
  95. ,'20170809145400' ,'20170809145500' ,'20170809145600' ,'20170809145700'
  96. ,'20170809145800' ,'20170809145900']
  97.      
  98.     nump_array_date= pd.to_datetime(nump_array_date) # convert str to date
  99.     nump_array_price  = [3900.0,  3903.0,  3891.0,  3888.0,  3893.0,  3895.0,  3899.0,  3906.0,  3914.0,  3911.0,
  100.   3912.0,  3910.0,  3914.0,  3920.0,  3920.0,  3915.0,  3915.0,  3915.0,  3911.0,  3903.0,
  101.   3901.0,  3899.0,  3894.0,  3901.0,  3901.0,  3897.0,  3891.0,  3882.0,  3878.0,  3881.0,
  102.   3878.0,  3885.0,  3886.0,  3889.0,  3887.0,  3887.0,  3886.0,  3885.0,  3886.0,  3887.0,
  103.   3894.0,  3888.0,  3890.0,  3887.0,  3888.0,  3883.0,  3880.0,  3885.0,  3887.0,  3882.0,
  104.   3882.0,  3887.0,  3886.0,  3885.0,  3890.0,  3891.0,  3887.0,  3890.0,  3886.0,  3891.0,
  105.   3888.0,  3891.0,  3881.0,  3878.0,  3877.0,  3875.0,  3871.0,  3872.0,  3879.0,  3876.0,
  106.   3879.0,  3885.0,  3884.0,  3883.0,  3879.0,  3877.0,  3880.0,  3878.0,  3882.0,  3885.0,
  107.   3883.0,  3884.0,  3883.0,  3881.0,  3882.0,  3889.0,  3896.0,  3891.0,  3897.0,  3905.0,
  108.   3901.0,  3902.0,  3899.0,  3897.0,  3896.0,  3899.0,  3902.0,  3902.0,  3905.0,  3913.0,
  109.   3910.0,  3909.0,  3902.0,  3901.0,  3902.0,  3897.0,  3903.0,  3902.0,  3901.0,  3900.0,
  110.   3903.0,  3906.0,  3906.0,  3909.0,  3904.0,  3902.0,  3902.0,  3902.0,  3904.0,  3909.0,
  111.   3909.0,  3941.0,  3934.0,  3947.0,  3938.0,  3939.0,  3938.0,  3932.0,  3930.0,  3929.0,
  112.   3924.0,  3930.0,  3930.0,  3926.0,  3929.0,  3918.0,  3914.0,  3912.0,  3908.0,  3912.0,
  113.   3913.0,  3910.0,  3915.0,  3916.0,  3913.0,  3915.0,  3918.0,  3913.0,  3908.0,  3912.0,
  114.   3911.0,  3916.0,  3913.0,  3915.0,  3918.0,  3917.0,  3916.0,  3920.0,  3920.0,  3917.0,
  115.   3916.0,  3912.0,  3913.0,  3909.0,  3911.0,  3910.0,  3907.0,  3908.0,  3901.0,  3907.0,
  116.   3908.0,  3909.0,  3910.0,  3909.0,  3911.0,  3912.0,  3914.0,  3915.0,  3913.0,  3919.0,
  117.   3917.0,  3915.0,  3918.0,  3919.0,  3918.0,  3926.0,  3925.0,  3925.0,  3927.0,  3923.0,
  118.   3926.0,  3926.0,  3920.0,  3921.0,  3919.0,  3919.0,  3917.0,  3921.0,  3924.0,  3922.0,
  119.   3921.0,  3923.0,  3922.0,  3922.0,  3927.0,  3928.0,  3928.0,  3929.0,  3926.0,  3927.0,
  120.   3928.0,  3926.0,  3922.0,  3912.0,  3911.0,  3908.0,  3912.0,  3910.0,  3913.0,  3905.0,
  121.   3910.0,  3904.0,  3893.0,  3896.0,  3898.0,  3896.0,  3903.0,  3905.0,  3905.0,  3907.0,
  122.   3906.0,  3909.0,  3910.0,  3910.0,  3913.0,  3911.0,  3911.0,  3914.0,  3913.0,  3908.0,
  123.   3913.0,  3910.0,  3910.0,  3911.0,  3914.0,  3918.0,  3917.0,  3917.0,  3919.0,  3919.0,
  124.   3917.0,  3922.0,  3926.0,  3924.0,  3927.0,  3925.0,  3940.0,  3940.0,  3943.0,  3949.0,
  125.   3953.0,  3951.0,  3951.0,  3953.0,  3950.0,  3957.0,  3964.0,  3964.0,  3960.0,  3958.0,
  126.   3963.0,  3963.0,  3956.0,  3959.0,  3959.0,  3957.0,  3961.0,  3960.0,  3960.0,  3963.0,
  127.   3972.0,  3971.0,  3974.0,  3982.0,  3980.0,  3981.0,  3969.0,  3970.0,  3970.0,  3972.0,
  128.   3968.0,  3968.0,  3970.0,  3974.0,  3973.0,  3974.0,  3971.0,  3972.0,  3978.0,  3982.0,
  129.   3975.0,  3971.0,  3970.0,  3972.0,  3971.0,  3970.0,  3973.0,  3973.0,  3976.0,  3976.0,
  130.   3975.0,  3981.0,  3980.0,  3979.0,  3979.0,  3984.0,  3980.0,  3977.0,  3983.0,  3984.0,
  131.   3984.0,  3980.0,  3979.0,  3979.0,  3978.0,  3978.0,  3978.0,  3979.0,  3983.0,  3978.0,
  132.   3978.0,  3981.0,  3984.0,  3990.0,  3993.0,  3997.0,  4001.0,  4000.0,  3997.0,  4003.0,
  133.   4003.0,  4000.0,  4002.0,  3991.0,  3994.0,  4006.0,]

  134.      

  135.     fig, ax = plt.subplots()
  136.    
  137. #     plt.plot(nump_array_date,nump_array_price)
  138.     plt.plot(nump_array_price)
  139.     plt.show()

复制代码



最佳答案
2021-6-1 03:06:32
这应该是楼主想要的效果吧?细节可以调整。
  1.   df=pd.DataFrame({'date':nump_array_date,'price':nump_array_price})
  2.     df_night=df[df.date<'2017-08-09 09:00:00']
  3.     df_0900=df[(df.date>='2017-08-09 09:00:00')&(df.date<='2017-08-09 10:15:00')]
  4.     df_1030=df[(df.date>='2017-08-09 10:30:00')&(df.date<='2017-08-09 11:30:00')]
  5.     df_1330=df[(df.date>='2017-08-09 13:30:00')&(df.date<='2017-08-09 15:00:00')]
  6.     df_all=df_0900.append(df_1030)
  7.     df_final=df_all.append(df_1330)
  8.      
  9.     x_tk=[]
  10.     x_lb=[]

  11.     for i in range(0,len(df_final.date.tolist())):
  12.         x_tk.append(i)
  13.         if i % 7==0:
  14.             x_lb.append(df_final.date.tolist()[i])
  15.         else:
  16.             x_lb.append("")
  17.      
  18.     plt.plot(x_tk,df_final.price)
  19.     plt.xticks(x_tk,(x_lb),rotation=80)
  20.     plt.show()
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0

主题

3

帖子

5

积分

新手上路

Rank: 1

积分
5
发表于 2021-6-1 03:06:32 | 显示全部楼层 &
这应该是楼主想要的效果吧?细节可以调整。
  1.   df=pd.DataFrame({'date':nump_array_date,'price':nump_array_price})
  2.     df_night=df[df.date<'2017-08-09 09:00:00']
  3.     df_0900=df[(df.date>='2017-08-09 09:00:00')&(df.date<='2017-08-09 10:15:00')]
  4.     df_1030=df[(df.date>='2017-08-09 10:30:00')&(df.date<='2017-08-09 11:30:00')]
  5.     df_1330=df[(df.date>='2017-08-09 13:30:00')&(df.date<='2017-08-09 15:00:00')]
  6.     df_all=df_0900.append(df_1030)
  7.     df_final=df_all.append(df_1330)
  8.      
  9.     x_tk=[]
  10.     x_lb=[]

  11.     for i in range(0,len(df_final.date.tolist())):
  12.         x_tk.append(i)
  13.         if i % 7==0:
  14.             x_lb.append(df_final.date.tolist()[i])
  15.         else:
  16.             x_lb.append("")
  17.      
  18.     plt.plot(x_tk,df_final.price)
  19.     plt.xticks(x_tk,(x_lb),rotation=80)
  20.     plt.show()
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-11-28 07:00 , Processed in 0.071028 second(s), 34 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

快速回复 返回顶部 返回列表