-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2graph.py
More file actions
1233 lines (1051 loc) · 45 KB
/
csv2graph.py
File metadata and controls
1233 lines (1051 loc) · 45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
csvからグラフを作る
fgosccnt.py から作成した csv ファイルを元に、グラフを作成するツール
fgosccnt.py : https://github.com/fgosc/fgosccnt
機能:
・箱ひげ図
・ヴァイオリンプロット
・周回数毎のアイテムのドロップ数
・周回数毎のアイテムのドロップ率
・平行座標
・複数のファイルに対応
・ヒストグラム (予定)
・イベントアイテムのイベントボーナス+毎の表を作成 (予定)
・データテーブルの表示
・統計データの出力 (予定)
・画像に出力
・HTMLに出力
"""
import argparse
from asyncio.log import logger
import warnings
import re
from pathlib import Path
import unicodedata
from typing import NoReturn
from typing import Union
import numpy as np
import pandas as pd
import plotly
import plotly.offline as offline
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import matplotlib.pyplot as plt
import japanize_matplotlib
import seaborn as sns
from kaleido.scopes.plotly import PlotlyScope
import cufflinks as cf
from .dataframe import Data
progname = "csv2graph"
version = "0.0.1.20220606.2"
warnings.simplefilter('ignore', FutureWarning)
pd.options.plotting.backend = "plotly"
sns.set()
scope = PlotlyScope()
cf.go_offline()
# 'japanize_matplotlib' imported but unused flake8(F401) 対策
# import japanize_matplotlib の後に # noqa でもいいけれど、__all__にリストアップしておく
__all__ = ['japanize_matplotlib']
quest_name = ''
report_data = None
def make_df(csv_path: str, total_row=False, qp_sum=False) -> pd.core.frame.DataFrame:
"""
fgosccntで作成したcsvからDataFrameを作成する
Args:
csv_path (str): fgosccntで作成したcsvファイルのパス
total_row (bool): 合計の行を残すか否か
グラフの処理では残さない
Returns:
DataFrame
"""
global report_data
global quest_name
report_data = Data(csv_path, total_row=total_row, qp_sum=qp_sum)
quest_name = report_data.quest_name
return report_data.df
def output_graphs(
fig: plotly.graph_objs._figure.Figure,
graph_type: str
) -> NoReturn:
def plot_on_web_browser(fig):
offline.iplot(
fig,
config={
"displaylogo": False,
"modeBarButtonsToRemove": ["sendDataToCloud"]}
)
def export_img_file(fig, title, img_format='png'):
output_path = _create_output_path(title, args.imgdir, img_format)
with open(output_path, "wb") as f:
f.write(scope.transform(fig, format=img_format))
def exporpt_html(fig, graph_type):
output_path = _create_output_path(graph_type, args.htmldir, '.html')
fig.write_html(fig, output_path)
def _create_output_path(graph_type, args_dir, file_suffix):
dir = Path(args_dir)
if not dir.parent.is_dir():
dir.parent.mkdir(parents=True)
return dir / Path(quest_name + '-' + graph_type + '.' + file_suffix)
if args.web:
plot_on_web_browser(fig)
if args.imgdir is not None:
export_img_file(fig, graph_type)
if args.htmldir is not None:
exporpt_html(fig, graph_type)
def get_pixel_of_width_of_string(text: str, half: int = 8, full: int = 14) -> int:
"""
指定された半角と全角の文字幅から、与えられた文字列の幅 (pixel) を計算する
前提は以下の通り
font size : ?
font: meiryo ?
全角: full pixel
半角: half pixel
`unicodedata.east_asian_width` については以下を参照
Unicode® Standard Annex #11 EAST ASIAN WIDTH:
http://www.unicode.org/reports/tr11/
全角: F, W, A
半角: H, Na, N
Wikipedia:
https://ja.wikipedia.org/wiki/%E6%9D%B1%E3%82%A2%E3%82%B8%E3%82%A2%E3%81%AE%E6%96%87%E5%AD%97%E5%B9%85
"""
width = 0
for c in text:
if unicodedata.east_asian_width(c) in 'FWA':
width += full
else:
width += half
return width
def plt_ridgeline(df: pd.core.frame.DataFrame) -> NoReturn:
df: pd.core.frame.DataFrame = drop_filename(df)
fig: plotly.graph_objs._figure.Figure = go.Figure()
data = [go.Violin(x=df[col], name=col, showlegend=False, box_visible=True, meanline_visible=True)
for col in df.columns]
layout = {'title': ''} # dict(title='')
fig = go.Figure(data=data, layout=layout)
fig.update_traces(orientation='h', side='positive', width=2, points=False)
fig.update_xaxes(title_text="ドロップ数", dtick=1)
# fig.update_layout(xaxis_showgrid=False, xaxis_zeroline=False)
output_graphs(fig, '稜線図')
# Use in box or violin
def plt_not_ordered_graphs(df: pd.core.frame.DataFrame) -> NoReturn:
"""plot iolin plot or box plot"""
layout = _get_not_ordered_graphs_layout(df)
if args.violin:
plot_data = _get_violine_data(df)
fig = go.Figure(data=plot_data, layout=layout)
output_graphs(fig, 'violin_plot')
if args.box:
plot_data = _get_box_data(df)
fig = go.Figure(data=plot_data, layout=layout)
output_graphs(fig, 'box_plot')
# Use in box or violin
def _get_not_ordered_graphs_layout(df: pd.core.frame.DataFrame) -> dict:
FONT_SIZE = 15
AXIS_FONT_SIZE = 13
TEXT_Y_OFFSET = 1
TITLE_X = 0.5
MARGIN_TOP = 50
MARGIN_BOTTOM = 64
MARGIN_RIGHT = 40
MARGIN_LEFT = 76
MARGIN_PAD = 0
AXES_HEIGHT = 600
fig_height = MARGIN_TOP + AXES_HEIGHT + MARGIN_BOTTOM
fig_width = 100 * len(df.columns) # 70 * len(df.columns)
Text_size_2_text_heiht = {15: 13, 16: 15, 17: 15, 18: 15}
if FONT_SIZE < 15:
text_height = 13 # 未検証
elif 15 <= FONT_SIZE <= 18:
text_height = Text_size_2_text_heiht[FONT_SIZE]
elif 18 < FONT_SIZE:
text_height = 15 # 未検証
title_y = (MARGIN_BOTTOM + AXES_HEIGHT + (MARGIN_TOP + text_height) / 2 - TEXT_Y_OFFSET) / fig_height
df = drop_filename(df)
ymax = df.max().values.max()
dtick = round(ymax / 11 / 10) * 10 if 200 < ymax else 10 if 100 < ymax else 5 if 30 < ymax else 1
layout = dict(
title={
'text': quest_name,
'x': TITLE_X,
'y': title_y,
'xanchor': 'center',
'font': dict(size=FONT_SIZE)},
height=fig_height,
width=fig_width,
margin=dict(l=MARGIN_LEFT, t=MARGIN_TOP, b=MARGIN_BOTTOM, r=MARGIN_RIGHT, pad=MARGIN_PAD, autoexpand=False),
template="seaborn",
paper_bgcolor='#FFFFFF', # "#aaf",EAEAF2,DBE3E6,#FFFFFF (白)
xaxis=dict(
title_text='obtained items',
title_font=dict(size=AXIS_FONT_SIZE)
),
yaxis=dict(
title_text='number of items obtained',
title_font=dict(size=AXIS_FONT_SIZE),
# range=[-0.5, df.max().max()]
# range=[-1.5, 30],
# dtick=1,
dtick=dtick
)
)
return layout
def _get_violine_data(df: pd.core.frame.DataFrame) -> list[plotly.graph_objs._violin.Violin]:
df = drop_filename(df)
data = [
go.Violin(
y=df[col],
name=col,
showlegend=False,
box_visible=True,
meanline_visible=True
) for col in df.columns]
return data
def _get_box_data(df: pd.core.frame.DataFrame) -> list[plotly.graph_objs._box.Box]:
df = drop_filename(df)
data = [
go.Box(
y=df[col],
name=col,
showlegend=False
) for col in df.columns]
return data
def plot_line(
df: pd.core.frame.DataFrame,
title: str = '各周回数における素材ドロップ数',
rate: bool = False,
range_expans: bool = False
) -> NoReturn:
MARGIN_TOP = 65
MARGIN_BOTTOM = 55
MARGIN_LEFT = 70
MARGIN_RIGHT = 38
height_of_sub_figure = 100
vertical_spacing = 72 # サブプロット間の間隔 [pixel]
template = "seaborn"
if rate:
fill = 'tozerox' # ['none', 'tozeroy', 'tozerox', 'tonexty', 'tonextx','toself', 'tonext']
ticksuffix = '%'
mode = 'lines+markers'
else:
fill = 'none'
ticksuffix = ''
mode = "lines+markers"
ytext = 'ドロ数'
df = drop_filename(df)
total_runs = df.index.max() + 1
number_of_cols = 2
r = int(len(df.columns) / number_of_cols)
number_of_rows = r if len(df.columns) % 2 == 0 else r + 1
fig_height = height_of_sub_figure * number_of_rows +\
MARGIN_TOP + vertical_spacing * (number_of_rows - 1) + MARGIN_BOTTOM
# In plotly, vertical spacing can only be specified as a percentage,
# so calculate the percentage.
vertical_spacing = vertical_spacing / (fig_height - MARGIN_TOP - MARGIN_BOTTOM)
fig = make_subplots(
rows=number_of_rows,
cols=number_of_cols,
vertical_spacing=vertical_spacing,
subplot_titles=df.columns
)
for i, col in enumerate(df.columns):
y = df[col]
ymean = y[len(y) - 1]
is_exp = re.search('種火|灯火|猛火|業火', col) is not None
# %表記にしないアイテム
# Over 300%
if 300 <= ymean:
df[col] /= 100
y = df[col]
ymean = y[len(y) - 1]
if rate:
ytext = '平均ドロ数'
tix = ''
# 100% over Ember, Light, Fire, Blaze
elif (100 <= ymean) & is_exp:
df[col] /= 100
y = df[col]
ymean = y[len(y) - 1]
if rate:
ytext = '平均ドロ数'
tix = ''
else:
if rate:
ytext = 'ドロ率'
tix = ticksuffix
_ymin, _ymax = y.min(), y.max()
yrange = _ymax - _ymin
# 末尾付近をy軸方向に拡大
if range_expans:
# ymin = ymean - 1 * y[5:].std()
ymin = ymean - yrange * 0.1
if ymin <= 0:
ymin = 0 - yrange * 0.04
# ymax = ymean + 1 * y[5:].std()
ymax = ymean + yrange * 0.1
# 線が見えなくならないようにy軸の範囲を微調整する
else:
ymin = _ymin - yrange * 0.05 # 0%の時線が見えなくなるので 範囲 * 5% 下げる
ymax = _ymax + yrange * 0.05 # minだけ調整すると上にずれるので 範囲 * +5% 上げる
marker_size, line_width = _get_line_width_and_marker_size(total_runs, rate)
fig.add_trace(
go.Scatter(
x=df.index + 1,
y=y,
mode=mode,
name=col,
fill=fill,
# 凡例は、作成時現在のplotlyの仕様だと一カ所にまとめて表示しかできない
# 離れると分かりにくいため、図の上にそれぞれ素材名を表示して代用する
# 手動で線や文字を描画することで、凡例を自作することも可能と思われる
showlegend=False,
opacity=0.9,
marker_size=marker_size,
line_width=line_width
),
# グラフの位置
row=int(i / 2) + 1,
col=i % 2 + 1
)
fig.update_yaxes(
title_text=ytext,
title_standoff=5,
title_font={"size": 11},
range=[ymin, ymax],
ticksuffix=tix,
type="linear", # log
# rangemode="tozero",
row=int(i / 2) + 1,
col=i % 2 + 1
)
fig.update_xaxes(
# range=[0, df.index.max()+1],
# fixedrange=True, # 固定範囲 trueの場合、ズームは無効
# tickmode='array', # Type: enumerated , one of ( 'auto' | 'linear' | 'array' )
# tickmode='auto' の場合は、nticks でメモリ数を指定する
# 楽だがどの刻み幅が使われるかわからない
# nticks=20,
# tickmode = 'linear' の場合は、tick0 と dick によってメモリを設定する
# 0開始の0,5,10,...か、1開始の1,6,11,...の選択を迫られる
tick0=0,
dtick=_get_dtick(total_runs),
# tickmode='array' の場合は、ticktext でメモリテキストを tickvals でメモリの配置を設定する
# 周回数が少ない場合は、1,5,10,... でいいが、多い場合が課題になる
# ticktext=[1 if i ==0 else 5 * i for i in range(int((df.index.max()+1)/5)+1)],
# tickvals=[1 if i == 0 else 5 * i for i in range(int((total_runs)/5)+1)],
title_text='周回数',
title_standoff=0,
title_font={"size": 11},
# x軸のラベルの位置の調整は、ドキュメントを探した限りだとやり方がなかった
# 表示をOFFにして、位置を指定してテキストを直打することで代用はおそらく可能
# title_xanchor='right',
row=int(i / 2) + 1,
col=i % 2 + 1
)
fig.update_layout(
height=fig_height, width=1000,
# 背景色を変えてfigの範囲を確認する場合や、単に背景色を変えたい時に変更
paper_bgcolor='#FFFFFF', # "#aaf",EAEAF2,DBE3E6
title={'text': title, 'x': 0.5, 'y': 0.985, 'xanchor': 'center', 'font': dict(size=15)},
font=dict(size=12), template=template, legend=dict(x=1.005, y=1),
margin=dict(l=MARGIN_LEFT, t=MARGIN_TOP, b=MARGIN_BOTTOM, r=MARGIN_RIGHT, pad=0, autoexpand=False)
)
output_graphs(fig, title)
def _get_line_width_and_marker_size(total_runs, rate):
# 周回数が多い場合、サイズが大きいとそれぞれの点や線が重なり合って潰れてしまうため、
# 線と点の大きさを、周回数によって変化させる
if total_runs < 70:
marker_size = 6
line_width = 2
elif total_runs <= 140:
marker_size = 2
line_width = 1
# 200周以上はマーカーが完全に潰れるので、線を無しにする (100~200は未確認)
else:
# ドロ率はfillと点にする
if rate:
marker_size = 1
line_width = 0
# ドロ数は線がないと意味不明瞭になるので、線を残す
else:
marker_size = 2
line_width = 1
return marker_size, line_width
def _get_dtick(total_runs):
"""
Formatting Ticks in Python
https://plotly.com/python/tick-formatting/
https://plotly.com/python/axes/
tickmode
Parent: layout.coloraxis.colorbar
Type: enumerated , one of ( 'auto' | 'linear' | 'array' )
Sets the tick mode for this axis. If 'auto', the number of
ticks is set via `nticks`. If 'linear', the placement of the
ticks is determined by a starting position `tick0` and a tick
step `dtick` ('linear' is the default value if `tick0` and `dtick`
are provided). If 'array', the placement of the ticks is set via
`tickvals` and the tick text is `ticktext`. ('array' is the default
value if `tickvals` is provided).
https://plotly.com/matlab/reference/#layout-margin
"""
if total_runs < 30:
dtick = 5
elif total_runs < 101:
dtick = 10
elif total_runs < 300:
dtick = 25
elif total_runs < 1000:
dtick = 100
elif total_runs < 5000:
dtick = 500
elif total_runs < 10000:
dtick = 1000
else:
dtick = 5000
return dtick
def plt_rate(df: pd.core.frame.DataFrame):
"""
ドロップ率の収束過程を表示する
"""
df = drop_filename(df)
# 一周ずつ素材数を加算
tmp = df.values
n = len(df)
for i in range(n):
# 最初は既に値が入っているので、スキップ
if i == 0:
continue
# 現在の値と前の値を加算
tmp[i] = tmp[i] + tmp[i - 1]
# それぞれの周回数で割り、%表記に合わせるために *100
droprate_df = pd.DataFrame(columns=df.columns, data=[tmp[i] / (i + 1) * 100 for i in range(n)])
# ドロ数は%だと見にくいのでそのままで
# droprate_df['ドロ数'] /= 100
# %表記を指定してプロット
plot_line(droprate_df.copy(), title='各周回数における素材ドロップ率', rate=True)
plot_line(droprate_df.copy(), title='各周回数における素材ドロップ率 (平均値近傍の拡大)', rate=True, range_expans=True)
def drop_filename(df: pd.core.frame.DataFrame):
"""DataFrameからファイル名の列を削除する"""
try:
df = df.drop('filename', axis=1)
except KeyError as e:
logger.info(f'filenameのカラムが存在しません。drop_filename(df), {e}')
return df
def plt_table(df: pd.core.frame.DataFrame) -> NoReturn:
"""
ドロップ数とドロップ率のテーブルを表示する
ブラウザ上で枠線が表示されない場合は、ブラウザの拡大率を100%に戻すことで表示される
クエストの名前は、以下から取得する
- csvのファイル名
- file nameの2行目 (fgoscdataに対応)
- TODO 指定できるようにする
表の幅は自動で調整する
既知の問題
- プロポーショナルフォントには対応していない
- Mなどの横幅の広いアルファベットが多いと改行が発生する
-> 短くして対処
`HIMEJIサバイバルカジノ ビギナー級` → `HIMEJI ビギナー級`
表示上のアイテム名列の幅 (実測値): 左右8pxずつ + 文字列の幅
実際にぴったりの幅を指定すると、改行されレイアウトが崩れるため、更に7px余裕を持たせる
デフォルトの列幅の比率は、15:6:9
"""
def is_integer(num: Union[int, float, str]) -> bool:
"""
Receives a number and determines if it is an integer
Returns True if it is an integer, False if it is not an integer
"""
try:
float(num)
except ValueError:
return False
else:
return float(num).is_integer()
MARGIN_TOP = 30
MARGIN_BOTTOM = 30
MARGIN_LEFT = 40
MARGIN_RIGHT = 40
MARGIN_PAD = 0
CELL_HEIGHT = 26
LINE_WIDTH = 1
FONT_SIZE = 16
HIGHT_OFFSET = 1 # 上下の枠線が消える問題のため調整を行う
WIDTH_OFFSET = 8 * 2 + 14
# quest_name_width = get_pixel_of_width_of_string(quest_name)
# if 150 < quest_name_width + 8 * 2 + 14:
# place_width = quest_name_width + 8 * 2 + 7
# else:
# place_width = 150
# DROPS_WIDTH, RATES_WIDTH = 6, 9
# items_width = np.ceil(place_width / 150 * (DROPS_WIDTH + RATES_WIDTH))
# width = place_width + 150 + MARGIN_LEFT + MARGIN_RIGHT
df = drop_filename(df)
runs = report_data.run
# アイテムカラムは、報酬QP(+xxxx) 次のカラム以降と仮定
# ドロップしたアイテム名を取得
QpColIndex = df.columns.get_loc(report_data.reward_QP_name)
items = df.columns[QpColIndex + 1:]
# ドロップしたアイテム数を取得
drops = df.sum().values[QpColIndex + 1:]
# ドロップ率
# 小数点1位で統一する場合
# rates = [f'{i/runs:>.2%}' for i in drops]
# 有効桁数を3桁以上にする
# 4桁の時は有効桁数 5 1234.5678... -> 1234.5%
# n桁の時は有効桁数 n + 1
# 2桁以下の場合は有効桁数 3 1.2345... -> 1.23%
rates = []
max_significant_gigures = 0
for drop in drops:
# drop rate
drop_rate = drop / runs * 100
# 整数部の桁数
n = len(str(int(drop_rate // 1)))
# 3桁以上の場合
# 有効数字 n+1 桁
# %を付けない
if 3 <= n:
significant_figures = n - 1
drop_rate /= 100
drop_rate_str = f'{drop_rate:>.{significant_figures}g} '
rates.append(drop_rate_str)
else:
significant_figures = 3
drop_rate_str = f'{drop_rate:>.{significant_figures}g}'
# % を付与
# 小数点第1位 (the tenths place) が0の場合、.0が省略されるため、.0を加える
if is_integer(drop_rate_str):
rates.append(f'{drop_rate:>.1f} %')
else:
# 小数点第2位が0の場合、0を加える ex. 6.9 -> 6.90
if len(drop_rate_str.replace('.', '')) == 2:
drop_rate_str += '0'
rates.append(drop_rate_str + ' %')
if max_significant_gigures < significant_figures:
max_significant_gigures = significant_figures
# カラムの幅を計算
items_max_width = 0
drops_max_width = 0
rates_max_width = 0
for item_name, drop_num, drop_rate in zip(items, drops, rates):
item_name_pixel = get_pixel_of_width_of_string(item_name, half=np.ceil(FONT_SIZE / 2), full=FONT_SIZE)
drops_max_pixel = get_pixel_of_width_of_string(str(drop_num.item()), half=np.ceil(FONT_SIZE),
full=FONT_SIZE) # np.int64 -> int -> str
rates_max_pixel = get_pixel_of_width_of_string(drop_rate, half=np.ceil(FONT_SIZE / 2), full=FONT_SIZE)
if items_max_width < item_name_pixel:
items_max_width = item_name_pixel
if drops_max_width < drops_max_pixel:
drops_max_width = drops_max_pixel
if rates_max_width < rates_max_pixel:
rates_max_width = rates_max_pixel
items_max_width = max(items_max_width, get_pixel_of_width_of_string('アイテム名')) + WIDTH_OFFSET + 10
drops_max_width = max(drops_max_width, get_pixel_of_width_of_string('ドロ数')) + WIDTH_OFFSET - 5
rates_max_width += WIDTH_OFFSET + 5
fig: plotly.graph_objs._figure.Figure = go.Figure(
data=[
go.Table(
columnorder=[0, 1, 2],
columnwidth=[items_max_width, drops_max_width, rates_max_width], # [items_width, DROPS_WIDTH, RATES_WIDTH],
header=dict(
values=['アイテム名', 'ドロ数', 'ドロ率'], # [quest_name, runs, ''],
line_color='black',
line_width=LINE_WIDTH,
fill_color='white',
align=['center', 'center', 'center'],
font_color='black',
font_size=15,
# font=dict(
# color='black',
# size=14
# ),
height=CELL_HEIGHT
),
cells=dict(
values=[items, drops, rates],
# suffix=['', '', '%'],
line_color='black',
line_width=LINE_WIDTH,
fill_color='white',
align=['center', 'right'],
font=dict(
color='black',
size=15
),
height=CELL_HEIGHT
)
)
]
)
fig.update_layout(
title={
'text': quest_name + f' {runs} 周',
'x': 0.5,
'xanchor': 'center',
'font': dict(size=15)
},
# LINE_WIDTH は現時点で1or2以外の場合を考慮していない
# 線幅を考える時に考える
# HIGHT_OFFSETを設定しないと下の枠線が消える
height=CELL_HEIGHT * len(df.columns[1:]) + MARGIN_TOP + MARGIN_BOTTOM + LINE_WIDTH + HIGHT_OFFSET,
width=items_max_width + drops_max_width + rates_max_width + MARGIN_LEFT + MARGIN_RIGHT,
font=dict(size=14),
# 背景色を変えてfigの範囲を確認する場合や、単に背景色を変えたい時に変更
paper_bgcolor='white', # white', '#FFFFFF', "#aaf", '#EAEAF2', '#DBE3E6'
margin=dict(
l=MARGIN_LEFT,
r=MARGIN_RIGHT,
b=MARGIN_BOTTOM,
t=MARGIN_TOP,
pad=MARGIN_PAD,
autoexpand=False
)
)
output_graphs(fig, 'table')
def plt_event_line(df: pd.core.frame.DataFrame):
"""
ボーナス毎のイベントアイテムのドロップ数を線形グラフを表示する
"""
# イベントアイテムに使用するDF1
# TODO 変数名を考える
#
# データ
# - (x3)などを取り除いたアイテム名
# - (x3)がついているアイテム名
# - (x3)がついているものの数
# - (x3)などを取り除いた場合の数
# | | アイテム名 | 枠名 | ドロップ枠数 | 枠数 | アイテム数 |
# |----|--------------|------------------|----------------|--------|--------------|
# | 0 | チェーンソー | チェーンソー(x3) | 1002 | 3 | 3006 |
# | 1 | 薪 | 薪(x3) | 153 | 3 | 459 |
E_df = pd.DataFrame({
'アイテム名': [re.search('.+(?=\(x\d)', i).group(0) for i in df.columns[df.columns.str.contains('\(x')]],
'枠名': df.columns[df.columns.str.contains('\(x')],
'ドロップ枠数': [df[i].sum() for i in df.columns[df.columns.str.contains('\(x')]],
'枠数': [np.uint8(re.search('(?<=\(x)\d+', i).group(0)) for i in df.columns[df.columns.str.contains('\(x')]],
'アイテム数': [df[i].sum() * np.uint8(re.search('(?<=\(x)\d+', i).group(0)) for i in df.columns[df.columns.str.contains('\(x')]]
})
# イベントアイテムに使用するDF2
# TODO 変数名を考える
# データ
# - アイテム毎、礼装ボーナス毎のドロップ数
# | | チェーンソー(x3) | 薪(x3) |
# |-----|--------------------|----------|
# | +0 | 45.5455 | 6.95455 |
# | +1 | 60.7273 | 9.27273 |
# | +2 | 75.9091 | 11.5909 |
# | +3 | 91.0909 | 13.9091 |
# | +4 | 106.273 | 16.2273 |
# | +5 | 121.455 | 18.5455 |
# | +6 | 136.636 | 20.8636 |
# | +7 | 151.818 | 23.1818 |
# | +8 | 167 | 25.5 |
# | +9 | 182.182 | 27.8182 |
# | +10 | 197.364 | 30.1364 |
# | +11 | 212.545 | 32.4545 |
# | +12 | 227.727 | 34.7727 |
E_df2 = pd.DataFrame(
np.array(
[
# ボーナス礼装装備時の1周あたり平均アイテムドロップ数 [アイテムドロップ数/周]
# (基本束数 + ボーナス増加数) [アイテムドロップ数/枠] * 平均枠数 [枠/周]
# ex. (3 + 0~12) * 1002 / 66
# ex. (3 + 0~12) * 153 / 66
(E_df['枠数'].values[j] + i) * E_df['ドロップ枠数'].values[j] / len(df)
for i in range(13) for j in range(len(E_df))
]
).reshape(13, len(E_df)),
columns=[i for i in E_df['枠名']],
index=['+' + str(i) for i in range(13)]
)
# イベントアイテムに使用するDF3
# TODO 変数名を考える
# データ
# - 束数毎に分かれていたアイテム毎の合計
# | | チェーンソー | 薪 |
# |-----|----------------|----------|
# | +0 | 45.5455 | 6.95455 |
# | +1 | 60.7273 | 9.27273 |
# | +2 | 75.9091 | 11.5909 |
# | +3 | 91.0909 | 13.9091 |
# | +4 | 106.273 | 16.2273 |
# | +5 | 121.455 | 18.5455 |
# | +6 | 136.636 | 20.8636 |
# | +7 | 151.818 | 23.1818 |
# | +8 | 167 | 25.5 |
# | +9 | 182.182 | 27.8182 |
# | +10 | 197.364 | 30.1364 |
# | +11 | 212.545 | 32.4545 |
# | +12 | 227.727 | 34.7727 |
E_df3 = pd.DataFrame()
for i in E_df2.columns:
if not re.search('.+(?=\(x\d)', i).group(0) in E_df3.columns: # アイテムの列がまだなければ作成
E_df3[re.search('.+(?=\(x\d)', i).group(0)] = E_df2[i]
else:
E_df3[re.search('.+(?=\(x\d)', i).group(0)] += E_df2[i] # 既にあれば加算
# 確認用コード
# from tabulate import tabulate # コード確認用
# print()
# print(tabulate(E_df3, E_df3.columns, tablefmt='github', showindex=True))
max2 = E_df2.max().max()
max3 = E_df3.max().max()
dtick2 = round(max2 / 11 / 10) * 10 if 130 < max2 else 10 if 70 < max2 else 5 if 13 < max2 else 1
dtick3 = round(max3 / 11 / 10) * 10 if 130 < max3 else 10 if 70 < max3 else 5 if 13 < max3 else 1
# イベントアイテム毎にドロ枠が何種類あるか
keys = E_df3.columns
values = np.zeros(len(E_df3.columns), dtype=np.uint8)
d = dict(zip(keys, values))
for j in E_df3.columns:
for i in range(len(E_df2.columns)):
m = re.search(j, E_df2.columns[i])
if m is not None:
d[j] += 1
# 2種類以上ある場合は、2つグラフを表示
if 1 < max(d.values()):
# イベントアイテムの平均ドロップ数
from plotly.subplots import make_subplots
template = "seaborn"
fig: plotly.graph_objs._figure.Figure = make_subplots(
rows=1, cols=2, subplot_titles=('枠毎の平均ドロップ数', 'アイテム毎の平均ドロップ数'))
# left plot
for i in range(len(E_df2.columns)):
fig.add_trace(
go.Scatter(
x=E_df2.index, # 礼装ボーナス増加数
y=E_df2[E_df2.columns[i]], # 平均アイテムドロップ数
name=E_df2.columns[i]
),
row=1, col=1
)
fig.update_xaxes(
title_text="礼装ボーナス",
dtick=1,
range=[0, 12],
domain=[0, 0.45],
row=1, col=1
)
fig.update_yaxes(
title_text="ドロップ数",
dtick=dtick2,
row=1, col=1
)
# right plot
for i in range(len(E_df3.columns)):
fig.add_trace(
go.Scatter(
x=E_df3.index,
y=E_df3[E_df3.columns[i]],
name=E_df3.columns[i]
),
row=1, col=2
)
fig.update_xaxes(
title_text="礼装ボーナス",
dtick=1,
range=[0, 12],
domain=[0.55, 1],
row=1, col=2
)
fig.update_yaxes(
title_text="",
dtick=dtick3,
row=1, col=2
)
fig.update_layout(
height=570,
width=1000,
title={
'text': "イベントアイテムの平均ドロップ数",
'x': 0.45,
'y': 0.98,
'xanchor': 'center',
'font': dict(size=15)
},
font=dict(size=12),
annotations=[dict(font=dict(size=14))],
template=template,
legend=dict(x=1.005, y=1),
margin=dict(l=70, t=65, b=55, r=120, pad=0, autoexpand=False),
paper_bgcolor='white' # 'white' "LightSteelBlue"
)
output_graphs(fig, 'ボーナス毎のイベントアイテムのドロップ数')
# 1種類の場合は、1つグラフを表示
else:
template = "seaborn"
# fig.titles('アイテム毎の平均ドロップ数')
# fig = px.scatter(E_df3, x=E_df3.index, y=E_df3.columns,
# #color=E_df2.columns,
# labels=dict(index="ボーナス+", value="ドロップ数", variable=""),
fig: plotly.graph_objs._figure.Figure = go.Figure()
for i in range(len(E_df3.columns)):
fig.add_trace(
go.Scatter(
x=E_df3.index, y=E_df3[E_df3.columns[i]],
name=E_df3.columns[i],
mode='lines+markers'
)
)
fig.update_xaxes(
title_text="概念礼装ボーナス",
dtick=1,
range=[0, 12],
domain=[0, 1]
)
fig.update_yaxes(
title_text="ドロップ数",
title_standoff=5,
dtick=dtick3
)
fig.update_layout(
height=550, width=480,
title={
'text': "イベントアイテムの平均ドロップ数",
# 'x': 0.45, 'y':0.98,
'x': 0.5, 'y': 0.96,
'xanchor': 'center',
'font': dict(size=14)
},
font=dict(size=12),
template=template,
legend=dict(x=0.03, y=.97), # 左上
# legend=dict(x=1.05, y=1), # 右上外
margin=dict(l=70, t=50, b=55, r=38, pad=0, autoexpand=False),
paper_bgcolor='white'
)
output_graphs(fig, 'ボーナス毎のイベントアイテムのドロップ数')
def plt_line_matplot(df: pd.core.frame.DataFrame) -> NoReturn:
"""
概念礼装ボーナス毎のイベントアイテム獲得量をラインプロットで描く
"""
E_df = pd.DataFrame({
'アイテム名': [re.search('.+(?=\(x\d)', i).group(0) for i in df.columns[df.columns.str.contains('\(x')]],
'枠名': df.columns[df.columns.str.contains('\(x')],
'ドロップ枠数': [df[i].sum() for i in df.columns[df.columns.str.contains('\(x')]],
'枠数': [np.uint8(re.search('(?<=\(x)\d+', i).group(0)) for i in df.columns[df.columns.str.contains('\(x')]],
'アイテム数': [df[i].sum() * np.uint8(re.search('(?<=\(x)\d+', i).group(0)) for i in df.columns[df.columns.str.contains('\(x')]]
})
E_df2 = pd.DataFrame(
np.array([(E_df['枠数'].values[j] + i) * E_df['ドロップ枠数'].values[j] / len(df.index.values)
for i in range(13) for j in range(len(E_df))]).reshape(13, len(E_df)),
columns=[i for i in E_df['枠名']],
index=['+' + str(i) for i in range(13)]
)
E_df3 = pd.DataFrame()
for i in E_df2.columns:
if not re.search('.+(?=\(x\d)', i).group(0) in E_df3.columns: # アイテムの列がまだなければ作成
E_df3[re.search('.+(?=\(x\d)', i).group(0)] = E_df2[i]
else:
E_df3[re.search('.+(?=\(x\d)', i).group(0)] += E_df2[i] # 既にあれば加算
# prepare data
x = range(10)
y = [i * 0.5 for i in range(10)]
# 2行1列のグラフの描画
# subplot で 2*1 の領域を確保し、それぞれにグラフ・表を描画
nrow = 2
ncol = 2
# plt.figure(figsize=(6*ncol,6*nrow))
plt.figure(figsize=(12, 7))
# 1つ目のsubplot領域にグラフ
plt.subplot(nrow, ncol, 1)
for i in range(len(E_df2.columns)):
plt.plot(
E_df2.index,
E_df2[E_df2.columns[i]],
label=E_df2.columns[i],
marker="o",
markersize=5
)