siat 3.10.25__py3-none-any.whl → 3.10.75__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
siat/grafix.py CHANGED
@@ -425,6 +425,13 @@ def plot_line(df0,colname,collabel,ylabeltxt,titletxt,footnote,datatag=False, \
425
425
 
426
426
  #==============================================================================
427
427
  if __name__ =="__main__":
428
+ df1=df2=option_comm_china(symbol='黄金期权',contract='au2508',printout=False,graph=False)
429
+
430
+ ticker1='看涨期权'; ticker2='看跌期权'
431
+ colname1='最新价C'; colname2='最新价P'
432
+ label1=label2=ylabeltxt='价格'
433
+ twinx=True
434
+
428
435
  power=0
429
436
  datatag1=False
430
437
  datatag2=False
@@ -434,6 +441,10 @@ if __name__ =="__main__":
434
441
  yline=999
435
442
  xline=999
436
443
  resample_freq='D'
444
+
445
+ attention_value_area=''; attention_point_area=''
446
+ loc1='best'; loc2='best'
447
+ color1='red'; color2='blue'; facecolor='whitesmoke'; maxticks=20
437
448
 
438
449
  def plot_line2(df1,ticker1,colname1,label1, \
439
450
  df2,ticker2,colname2,label2, \
@@ -465,6 +476,7 @@ def plot_line2(df1,ticker1,colname1,label1, \
465
476
  if len(df2) ==0:
466
477
  print (" #Warning(plot_line2): no data to plot df2.")
467
478
  if (len(df1) ==0) and (len(df2) ==0):
479
+ pass
468
480
  return
469
481
 
470
482
  if DEBUG:
@@ -618,17 +630,20 @@ def plot_line2_coaxial(df01,ticker1,colname1,label1, \
618
630
  """
619
631
  DEBUG=False
620
632
 
621
- #插值平滑
622
- try:
623
- df01x=df01[[colname1]].astype('float')
624
- df1=df_smooth_manual(df01x,resample_freq=resample_freq)
625
- except:
626
- df1=df01
627
- try:
628
- df02x=df02[[colname2]].astype('float')
629
- df2=df_smooth_manual(df02x,resample_freq=resample_freq)
630
- except:
631
- df2=df02
633
+ #插值平滑:如果横轴不为日期型时不可平滑,否则数据会丢失!
634
+ if not isinstance(maxticks,bool):
635
+ try:
636
+ df01x=df01[[colname1]].astype('float')
637
+ df1=df_smooth_manual(df01x,resample_freq=resample_freq)
638
+ except:
639
+ df1=df01
640
+ try:
641
+ df02x=df02[[colname2]].astype('float')
642
+ df2=df_smooth_manual(df02x,resample_freq=resample_freq)
643
+ except:
644
+ df2=df02
645
+ else:
646
+ df1=df01; df2=df02
632
647
 
633
648
  #预处理ticker_type
634
649
  ticker_type_list=ticker_type_preprocess_mticker_mixed([ticker1,ticker2],ticker_type)
@@ -813,17 +828,20 @@ def plot_line2_coaxial(df01,ticker1,colname1,label1, \
813
828
  plt.plot(df2.index, f(df2.id),"r--", label='',linewidth=1)
814
829
  except: pass
815
830
 
816
- # 同轴绘图时,loc1/loc2未用上!
831
+ # 同轴绘图时,loc2未用上!
817
832
  plt.legend(loc=loc1,fontsize=legend_txt_size)
818
833
 
819
834
  # 使用 AutoDateLocator 自动选择最佳间隔,目的是显示最后一个日期,亲测有效!!!
820
- import matplotlib.dates as mdates
821
- try:
822
- ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=maxticks))
823
- except:
824
- pass
825
-
826
- plt.gcf().autofmt_xdate(ha="center") # 优化标注(自动倾斜)
835
+ if not isinstance(maxticks,bool):
836
+ import matplotlib.dates as mdates
837
+ try:
838
+ ax=plt.gca()
839
+ ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=maxticks))
840
+ except:
841
+ pass
842
+
843
+ plt.gcf().autofmt_xdate(ha="center") # 优化标注(自动倾斜)
844
+
827
845
  try:
828
846
  plt.gca().set_facecolor(facecolor)
829
847
  except:
@@ -883,17 +901,20 @@ def plot_line2_coaxial2(df01,ticker1,colname1,label1, \
883
901
  import pandas as pd
884
902
  DEBUG=False
885
903
 
886
- #插值平滑
887
- try:
888
- df01x=df01[[colname1]].astype('float')
889
- df1=df_smooth_manual(df01x,resample_freq=resample_freq)
890
- except:
891
- df1=df01
892
- try:
893
- df02x=df02[[colname2]].astype('float')
894
- df2=df_smooth_manual(df02x,resample_freq=resample_freq)
895
- except:
896
- df2=df02
904
+ #插值平滑:对于df索引为非日期型的不能进行插值平滑,否则会丢失数据!!!
905
+ if not isinstance(maxticks,bool):
906
+ try:
907
+ df01x=df01[[colname1]].astype('float')
908
+ df1=df_smooth_manual(df01x,resample_freq=resample_freq)
909
+ except:
910
+ df1=df01
911
+ try:
912
+ df02x=df02[[colname2]].astype('float')
913
+ df2=df_smooth_manual(df02x,resample_freq=resample_freq)
914
+ except:
915
+ df2=df02
916
+ else:
917
+ df1=df01; df2=df02
897
918
 
898
919
  #预处理ticker_type
899
920
  ticker_type_list=ticker_type_preprocess_mticker_mixed([ticker1,ticker2],ticker_type)
@@ -1104,10 +1125,11 @@ def plot_line2_coaxial2(df01,ticker1,colname1,label1, \
1104
1125
  plt.legend(loc=loc1,fontsize=legend_txt_size)
1105
1126
 
1106
1127
  # 使用 AutoDateLocator 自动选择最佳间隔,目的是显示最后一个日期,亲测有效!!!
1107
- import matplotlib.dates as mdates
1108
- ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=maxticks))
1109
-
1110
- plt.gcf().autofmt_xdate(ha="center") # 优化标注(自动倾斜)
1128
+ if not isinstance(maxticks,bool):
1129
+ import matplotlib.dates as mdates
1130
+ ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=maxticks))
1131
+
1132
+ plt.gcf().autofmt_xdate(ha="center") # 优化标注(自动倾斜)
1111
1133
  try:
1112
1134
  plt.gca().set_facecolor(facecolor)
1113
1135
  except:
@@ -1123,6 +1145,10 @@ def plot_line2_coaxial2(df01,ticker1,colname1,label1, \
1123
1145
  return
1124
1146
 
1125
1147
  #==============================================================================
1148
+ if __name__ =="__main__":
1149
+ df01=df1; df02=df2
1150
+ maxticks=False
1151
+
1126
1152
  def plot_line2_twinx(df01,ticker1,colname1,label1, \
1127
1153
  df02,ticker2,colname2,label2, \
1128
1154
  titletxt,footnote,power=0,datatag1=False,datatag2=False, \
@@ -1148,16 +1174,19 @@ def plot_line2_twinx(df01,ticker1,colname1,label1, \
1148
1174
  #plt.rcParams['axes.grid']=False
1149
1175
 
1150
1176
  #插值平滑
1151
- try:
1152
- df01x=df01[[colname1]].astype('float')
1153
- df1=df_smooth_manual(df01x,resample_freq=resample_freq)
1154
- except:
1155
- df1=df01
1156
- try:
1157
- df02x=df02[[colname2]].astype('float')
1158
- df2=df_smooth_manual(df02x,resample_freq=resample_freq)
1159
- except:
1160
- df2=df02
1177
+ if not isinstance(maxticks,bool):
1178
+ try:
1179
+ df01x=df01[[colname1]].astype('float')
1180
+ df1=df_smooth_manual(df01x,resample_freq=resample_freq)
1181
+ except:
1182
+ df1=df01
1183
+ try:
1184
+ df02x=df02[[colname2]].astype('float')
1185
+ df2=df_smooth_manual(df02x,resample_freq=resample_freq)
1186
+ except:
1187
+ df2=df02
1188
+ else:
1189
+ df1=df01; df2=df02
1161
1190
 
1162
1191
  #预处理ticker_type
1163
1192
  ticker_type_list=ticker_type_preprocess_mticker_mixed([ticker1,ticker2],ticker_type)
@@ -1327,12 +1356,13 @@ def plot_line2_twinx(df01,ticker1,colname1,label1, \
1327
1356
  ax2.legend(loc=loc2,fontsize=legend_txt_size)
1328
1357
 
1329
1358
  # 使用 AutoDateLocator 自动选择最佳间隔,目的是显示最后一个日期,亲测有效!!!
1330
- import matplotlib.dates as mdates
1331
- ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=maxticks))
1332
- ax2.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=maxticks))
1333
-
1334
- #自动优化x轴标签
1335
- plt.gcf().autofmt_xdate(ha="center") # 优化标注(自动倾斜)
1359
+ if not isinstance(maxticks,bool):
1360
+ import matplotlib.dates as mdates
1361
+ ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=maxticks))
1362
+ ax2.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=maxticks))
1363
+
1364
+ #自动优化x轴标签
1365
+ plt.gcf().autofmt_xdate(ha="center") # 优化标注(自动倾斜)
1336
1366
 
1337
1367
  plt.title(titletxt,fontweight='bold',fontsize=title_txt_size)
1338
1368
  plt.show()
@@ -1381,17 +1411,20 @@ def plot_line2_LR(df01,ticker1,colname1,label1, \
1381
1411
 
1382
1412
  #plt.rcParams['axes.grid']=False
1383
1413
 
1384
- #插值平滑
1385
- try:
1386
- df01x=df01[[colname1]].astype('float')
1387
- df1=df_smooth_manual(df01x,resample_freq=resample_freq)
1388
- except:
1389
- df1=df01
1390
- try:
1391
- df02x=df02[[colname2]].astype('float')
1392
- df2=df_smooth_manual(df02x,resample_freq=resample_freq)
1393
- except:
1394
- df2=df02
1414
+ #插值平滑:若df索引为非日期型,不能进行插值平滑
1415
+ if not isinstance(maxticks,bool):
1416
+ try:
1417
+ df01x=df01[[colname1]].astype('float')
1418
+ df1=df_smooth_manual(df01x,resample_freq=resample_freq)
1419
+ except:
1420
+ df1=df01
1421
+ try:
1422
+ df02x=df02[[colname2]].astype('float')
1423
+ df2=df_smooth_manual(df02x,resample_freq=resample_freq)
1424
+ except:
1425
+ df2=df02
1426
+ else:
1427
+ df1=df01; df2=df02
1395
1428
 
1396
1429
  #预处理ticker_type
1397
1430
  ticker_type_list=ticker_type_preprocess_mticker_mixed([ticker1,ticker2],ticker_type)
@@ -1594,7 +1627,8 @@ def plot_line2_LR(df01,ticker1,colname1,label1, \
1594
1627
  #plt.xlabel(footnote,ha='center',fontsize=xlabel_txt_size,color='gray')
1595
1628
 
1596
1629
  # 自动倾斜横轴日期
1597
- fig.autofmt_xdate(ha="center")
1630
+ if not isinstance(maxticks,bool):
1631
+ fig.autofmt_xdate(ha="center")
1598
1632
 
1599
1633
  # 调整布局防止重叠
1600
1634
  plt.tight_layout()
@@ -1630,16 +1664,19 @@ def plot_line2_UD(df01,ticker1,colname1,label1, \
1630
1664
  #plt.rcParams['axes.grid']=False
1631
1665
 
1632
1666
  #插值平滑
1633
- try:
1634
- df01x=df01[[colname1]].astype('float')
1635
- df1=df_smooth_manual(df01x,resample_freq=resample_freq)
1636
- except:
1637
- df1=df01
1638
- try:
1639
- df02x=df02[[colname2]].astype('float')
1640
- df2=df_smooth_manual(df02x,resample_freq=resample_freq)
1641
- except:
1642
- df2=df02
1667
+ if not isinstance(maxticks,bool):
1668
+ try:
1669
+ df01x=df01[[colname1]].astype('float')
1670
+ df1=df_smooth_manual(df01x,resample_freq=resample_freq)
1671
+ except:
1672
+ df1=df01
1673
+ try:
1674
+ df02x=df02[[colname2]].astype('float')
1675
+ df2=df_smooth_manual(df02x,resample_freq=resample_freq)
1676
+ except:
1677
+ df2=df02
1678
+ else:
1679
+ df1=df01; df2=df02
1643
1680
 
1644
1681
  #预处理ticker_type
1645
1682
  ticker_type_list=ticker_type_preprocess_mticker_mixed([ticker1,ticker2],ticker_type)
@@ -1850,7 +1887,8 @@ def plot_line2_UD(df01,ticker1,colname1,label1, \
1850
1887
  #plt.xlabel(footnote,ha='center',fontsize=xlabel_txt_size,color='gray')
1851
1888
 
1852
1889
  # 自动倾斜横轴日期
1853
- fig.autofmt_xdate(ha="center")
1890
+ if not isinstance(maxticks,bool):
1891
+ fig.autofmt_xdate(ha="center")
1854
1892
 
1855
1893
  # 调整布局防止重叠
1856
1894
  plt.tight_layout()
@@ -1883,16 +1921,19 @@ def plot_line2_twinx2(df01,ticker1,colname1,label1, \
1883
1921
  #plt.rcParams['axes.grid']=False
1884
1922
 
1885
1923
  #插值平滑
1886
- try:
1887
- df01x=df01[[colname1]].astype('float')
1888
- df1=df_smooth_manual(df01x,resample_freq=resample_freq)
1889
- except:
1890
- df1=df01
1891
- try:
1892
- df02x=df02[[colname2]].astype('float')
1893
- df2=df_smooth_manual(df02x,resample_freq=resample_freq)
1894
- except:
1895
- df2=df02
1924
+ if not isinstance(maxticks,bool):
1925
+ try:
1926
+ df01x=df01[[colname1]].astype('float')
1927
+ df1=df_smooth_manual(df01x,resample_freq=resample_freq)
1928
+ except:
1929
+ df1=df01
1930
+ try:
1931
+ df02x=df02[[colname2]].astype('float')
1932
+ df2=df_smooth_manual(df02x,resample_freq=resample_freq)
1933
+ except:
1934
+ df2=df02
1935
+ else:
1936
+ df1=df01; df2=df02
1896
1937
 
1897
1938
  #预处理ticker_type
1898
1939
  ticker_type_list=ticker_type_preprocess_mticker_mixed([ticker1,ticker2],ticker_type)
@@ -2097,7 +2138,8 @@ def plot_line2_twinx2(df01,ticker1,colname1,label1, \
2097
2138
  #自动优化x轴标签
2098
2139
  #格式化时间轴标注
2099
2140
  #plt.gca().xaxis.set_major_formatter(mdate.DateFormatter('%y-%m-%d'))
2100
- plt.gcf().autofmt_xdate(ha="center") # 优化标注(自动倾斜)
2141
+ if not isinstance(maxticks,bool):
2142
+ plt.gcf().autofmt_xdate(ha="center") # 优化标注(自动倾斜)
2101
2143
 
2102
2144
  plt.title(titletxt,fontweight='bold',fontsize=title_txt_size)
2103
2145
  plt.show()
@@ -2133,7 +2175,8 @@ def draw_lines(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
2133
2175
  mark_start=False,mark_top=False,mark_bottom=False,mark_end=False, \
2134
2176
  ticker_type='auto',facecolor='whitesmoke', \
2135
2177
  maxticks_enable=True,maxticks=15, \
2136
- translate=False):
2178
+ translate=False, \
2179
+ precision=2):
2137
2180
  """
2138
2181
  函数功能:根据df的内容绘制折线图
2139
2182
  输入参数:
@@ -2167,7 +2210,7 @@ def draw_lines(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
2167
2210
  return
2168
2211
 
2169
2212
  #插值平滑
2170
- if smooth:
2213
+ if smooth and not isinstance(maxticks,bool):
2171
2214
  #print(" Rendering graphics ...")
2172
2215
  try:
2173
2216
  df=df_smooth_manual(df0,resample_freq=resample_freq)
@@ -2270,7 +2313,7 @@ def draw_lines(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
2270
2313
 
2271
2314
  if annotate_value: #在标记曲线名称的同时标记其末端数值
2272
2315
  #y1=str(int(y_end)) if y_end >= 100 else str(round(y_end,2))
2273
- y1=str(round(y_end,2)) if abs(y_end) >= 100 else str(round(y_end,2)) if abs(y_end) >= 1 else str(round(y_end,3))
2316
+ y1=str(round(y_end,2)) if abs(y_end) >= 100 else str(round(y_end,precision)) if abs(y_end) >= 1 else str(round(y_end,precision))
2274
2317
  plt.annotate(text=' '+auto_translate2(c,translate)+': '+y1,
2275
2318
  xy=(x_end, y_end),
2276
2319
  xytext=(x_end, y_end),fontsize=annotate_size,
@@ -2325,7 +2368,7 @@ def draw_lines(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
2325
2368
  x_end = df_end[c].idxmin() # 开始值的x坐标
2326
2369
 
2327
2370
  #y1=str(int(y_end)) if y_end >= 100 else str(round(y_end,2))
2328
- y1=str(round(y_end,1)) if abs(y_end) >= 100 else str(round(y_end,2)) if abs(y_end) >= 1 else str(round(y_end,3))
2371
+ y1=str(round(y_end,1)) if abs(y_end) >= 100 else str(round(y_end,precision)) if abs(y_end) >= 1 else str(round(y_end,precision))
2329
2372
  plt.annotate(text=' '+y1,
2330
2373
  xy=(x_end, y_end),
2331
2374
  xytext=(x_end, y_end*0.998),fontsize=annotate_size,
@@ -2338,7 +2381,7 @@ def draw_lines(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
2338
2381
  x_end = df_end[c].idxmin() # 末端值的x坐标
2339
2382
 
2340
2383
  #y1=str(int(y_end)) if y_end >= 100 else str(round(y_end,2))
2341
- y1=str(round(y_end,1)) if abs(y_end) >= 100 else str(round(y_end,2)) if abs(y_end) >= 1 else str(round(y_end,3))
2384
+ y1=str(round(y_end,1)) if abs(y_end) >= 100 else str(round(y_end,precision)) if abs(y_end) >= 1 else str(round(y_end,precision))
2342
2385
  plt.annotate(text=' '+y1,
2343
2386
  xy=(x_end, y_end),
2344
2387
  xytext=(x_end, y_end*0.998),fontsize=annotate_size,
@@ -2487,12 +2530,13 @@ def draw_lines(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
2487
2530
  #图示标题
2488
2531
  plt.title(auto_translate2(title_txt,translate),fontweight='bold',fontsize=title_txt_size)
2489
2532
 
2490
- if maxticks_enable:
2491
- # 使用 AutoDateLocator 自动选择最佳间隔,目的是显示最后一个日期,亲测有效!!!
2492
- import matplotlib.dates as mdates
2493
- ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=maxticks))
2494
-
2495
- plt.gcf().autofmt_xdate(ha="center") # 优化标注(自动倾斜)
2533
+ if not isinstance(maxticks,bool):
2534
+ if maxticks_enable:
2535
+ # 使用 AutoDateLocator 自动选择最佳间隔,目的是显示最后一个日期,亲测有效!!!
2536
+ import matplotlib.dates as mdates
2537
+ ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=maxticks))
2538
+
2539
+ plt.gcf().autofmt_xdate(ha="center") # 优化标注(自动倾斜)
2496
2540
  try:
2497
2541
  plt.gca().set_facecolor(facecolor)
2498
2542
  except:
@@ -2584,7 +2628,7 @@ def draw_lines2(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
2584
2628
  return
2585
2629
 
2586
2630
  #插值平滑
2587
- if smooth:
2631
+ if smooth and not isinstance(maxticks,bool):
2588
2632
  print(" Smoothening curves ...")
2589
2633
  try:
2590
2634
  df=df_smooth_manual(df0,resample_freq=resample_freq)
@@ -2605,18 +2649,19 @@ def draw_lines2(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
2605
2649
  high_low=dfmax - dfmin
2606
2650
 
2607
2651
  #定义横轴标签:显示完整开始、结束日期
2608
- ax=plt.gca()
2609
- date_start=df.index[0]
2610
- date_end=df.index[-1]
2611
- if date_range and not date_freq:
2612
- ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
2613
- plt.xticks(pd.date_range(date_start,date_end))
2614
- if not date_range and date_freq:
2615
- ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
2616
- plt.xticks(pd.date_range(freq=date_freq))
2617
- if date_range and date_freq:
2618
- ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
2619
- plt.xticks(pd.date_range(date_start,date_end,freq=date_freq))
2652
+ if not isinstance(maxticks,bool):
2653
+ ax=plt.gca()
2654
+ date_start=df.index[0]
2655
+ date_end=df.index[-1]
2656
+ if date_range and not date_freq:
2657
+ ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
2658
+ plt.xticks(pd.date_range(date_start,date_end))
2659
+ if not date_range and date_freq:
2660
+ ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
2661
+ plt.xticks(pd.date_range(freq=date_freq))
2662
+ if date_range and date_freq:
2663
+ ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
2664
+ plt.xticks(pd.date_range(date_start,date_end,freq=date_freq))
2620
2665
 
2621
2666
  #取得df字段名列表
2622
2667
  collist=df.columns.values.tolist()
@@ -2857,10 +2902,11 @@ def draw_lines2(df0,y_label,x_label,axhline_value,axhline_label,title_txt, \
2857
2902
  plt.title(auto_translate2(title_txt,translate),fontweight='bold',fontsize=title_txt_size)
2858
2903
 
2859
2904
  # 使用 AutoDateLocator 自动选择最佳间隔,目的是显示最后一个日期,亲测有效!!!
2860
- import matplotlib.dates as mdates
2861
- ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=maxticks))
2862
-
2863
- plt.gcf().autofmt_xdate(ha="center") # 优化标注(自动倾斜)
2905
+ if not isinstance(maxticks,bool):
2906
+ import matplotlib.dates as mdates
2907
+ ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=maxticks))
2908
+
2909
+ plt.gcf().autofmt_xdate(ha="center") # 优化标注(自动倾斜)
2864
2910
  try:
2865
2911
  plt.gca().set_facecolor(facecolor)
2866
2912
  except:
@@ -3045,54 +3091,59 @@ def plot_2lines(df01,colname1,label1, \
3045
3091
  return
3046
3092
 
3047
3093
  #插值平滑
3048
- try:
3049
- df01x=df01[[colname1]].astype('float')
3050
- df1=df_smooth_manual(df01x,resample_freq=resample_freq)
3051
- except:
3052
- df1=df01
3053
-
3054
- try:
3055
- df02x=df02[[colname2]].astype('float')
3056
- df2=df_smooth_manual(df02x,resample_freq=resample_freq)
3057
- except:
3058
- df2=df02
3094
+ if not isinstance(maxticks,bool):
3095
+ try:
3096
+ df01x=df01[[colname1]].astype('float')
3097
+ df1=df_smooth_manual(df01x,resample_freq=resample_freq)
3098
+ except:
3099
+ df1=df01
3100
+
3101
+ try:
3102
+ df02x=df02[[colname2]].astype('float')
3103
+ df2=df_smooth_manual(df02x,resample_freq=resample_freq)
3104
+ except:
3105
+ df2=df02
3106
+ else:
3107
+ df1=df01; df2=df02
3059
3108
 
3060
3109
  plt.title(titletxt,fontweight='bold',fontsize=title_txt_size)
3061
3110
 
3062
3111
  #证券1:先绘制折线图
3063
- date_start=df1.index[0]
3064
- date_end=df1.index[-1]
3065
- if date_range and not date_freq:
3066
- ax=plt.gca()
3067
- ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
3068
- plt.xticks(pd.date_range(date_start,date_end))
3069
- if not date_range and date_freq:
3070
- ax=plt.gca()
3071
- ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
3072
- plt.xticks(pd.date_range(freq=date_freq))
3073
- if date_range and date_freq:
3074
- ax=plt.gca()
3075
- ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
3076
- plt.xticks(pd.date_range(date_start,date_end,freq=date_freq))
3112
+ if not isinstance(maxticks,bool):
3113
+ date_start=df1.index[0]
3114
+ date_end=df1.index[-1]
3115
+ if date_range and not date_freq:
3116
+ ax=plt.gca()
3117
+ ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
3118
+ plt.xticks(pd.date_range(date_start,date_end))
3119
+ if not date_range and date_freq:
3120
+ ax=plt.gca()
3121
+ ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
3122
+ plt.xticks(pd.date_range(freq=date_freq))
3123
+ if date_range and date_freq:
3124
+ ax=plt.gca()
3125
+ ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
3126
+ plt.xticks(pd.date_range(date_start,date_end,freq=date_freq))
3077
3127
 
3078
3128
  lwadjust=linewidth_adjust(df1)
3079
3129
  plt.plot(df1.index,df1[colname1],label=label1,linestyle='-',linewidth=lwadjust)
3080
3130
 
3081
3131
  #证券2:先绘制折线图
3082
- date_start=df2.index[0]
3083
- date_end=df2.index[-1]
3084
- if date_range and not date_freq:
3085
- ax=plt.gca()
3086
- ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
3087
- plt.xticks(pd.date_range(date_start,date_end))
3088
- if not date_range and date_freq:
3089
- ax=plt.gca()
3090
- ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
3091
- plt.xticks(pd.date_range(freq=date_freq))
3092
- if date_range and date_freq:
3093
- ax=plt.gca()
3094
- ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
3095
- plt.xticks(pd.date_range(date_start,date_end,freq=date_freq))
3132
+ if not isinstance(maxticks,bool):
3133
+ date_start=df2.index[0]
3134
+ date_end=df2.index[-1]
3135
+ if date_range and not date_freq:
3136
+ ax=plt.gca()
3137
+ ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
3138
+ plt.xticks(pd.date_range(date_start,date_end))
3139
+ if not date_range and date_freq:
3140
+ ax=plt.gca()
3141
+ ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
3142
+ plt.xticks(pd.date_range(freq=date_freq))
3143
+ if date_range and date_freq:
3144
+ ax=plt.gca()
3145
+ ax.xaxis.set_major_formatter(mdate.DateFormatter(date_fmt))
3146
+ plt.xticks(pd.date_range(date_start,date_end,freq=date_freq))
3096
3147
 
3097
3148
  lwadjust=linewidth_adjust(df2)
3098
3149
  plt.plot(df2.index,df2[colname2],label=label2,linestyle='-.',linewidth=lwadjust)
@@ -3109,10 +3160,12 @@ def plot_2lines(df01,colname1,label1, \
3109
3160
  plt.legend(loc='best',fontsize=legend_txt_size)
3110
3161
 
3111
3162
  # 使用 AutoDateLocator 自动选择最佳间隔,目的是显示最后一个日期,亲测有效!!!
3112
- import matplotlib.dates as mdates
3113
- ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=maxticks))
3114
-
3115
- plt.gcf().autofmt_xdate(ha="center") # 优化标注(自动倾斜)
3163
+ if not isinstance(maxticks,bool):
3164
+ import matplotlib.dates as mdates
3165
+ ax=plt.gca()
3166
+ ax.xaxis.set_major_locator(mdates.AutoDateLocator(maxticks=maxticks))
3167
+
3168
+ plt.gcf().autofmt_xdate(ha="center") # 优化标注(自动倾斜)
3116
3169
  try:
3117
3170
  plt.gca().set_facecolor(facecolor)
3118
3171
  except:
@@ -3140,6 +3193,11 @@ def df_smooth(df):
3140
3193
  注意2:order阶数仅对'spline'和'polynomial'方法有效,其中'polynomial'方法的阶数只能为奇数。
3141
3194
  """
3142
3195
 
3196
+ # df索引项若非日期型,不适合采用本函数进行插值
3197
+ import pandas as pd
3198
+ if not pd.api.types.is_datetime64_any_dtype(df.index):
3199
+ return df
3200
+
3143
3201
  #如果样本个数多于100个,其实没必要进行平滑,因为看不出效果
3144
3202
  if len(df) >= 100: return df
3145
3203
 
@@ -3213,6 +3271,11 @@ def df_smooth_manual(df,method='linear',resample_freq='D',order=3):
3213
3271
  注意3:pchip方法经常失败,可改为cubic
3214
3272
  """
3215
3273
 
3274
+ # df索引项若非日期型,不适合采用本函数进行插值
3275
+ import pandas as pd
3276
+ if not pd.api.types.is_datetime64_any_dtype(df.index):
3277
+ return df
3278
+
3216
3279
  #如果样本个数多于100个,没必要进行平滑,完全看不出效果
3217
3280
  if len(df) >= 100: return df
3218
3281
 
siat/market_china.py CHANGED
@@ -176,7 +176,8 @@ def market_profile_china(market='SSE'):
176
176
 
177
177
  info1t=info.T
178
178
  info1t['全貌']=info1t['股票']
179
- info1t['主板']=info1t['主板A股']+info1t['主板B股']+info1t['中小板']
179
+ #info1t['主板']=info1t['主板A股']+info1t['主板B股']+info1t['中小板']
180
+ info1t['主板']=info1t['主板A股']+info1t['主板B股']
180
181
  info1t['创业板']=info1t['创业板A股']
181
182
 
182
183
  infot2=info1t.T