teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

d

2019/06/06 08:59

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -58,4 +58,74 @@
58
58
 
59
59
  plt.show()
60
60
  ```
61
- ![イメージ説明](15fcb1aca7fd0512833a1fcb7d06bfa1.png)
61
+ ![イメージ説明](15fcb1aca7fd0512833a1fcb7d06bfa1.png)
62
+
63
+ ## 追記
64
+
65
+ 次の手順でできます。
66
+
67
+ 1. Axes.get_yticks() で y 軸の目盛りの位置一覧を取り出す。
68
+ 2. Axes.get_yticklabels() で y 軸の目盛りのラベル一覧を取り出す。
69
+ 3. Axes.set_yticks() で y 軸の目盛りの位置一覧を10個置きにスライスして設定する。
70
+ 4. Axes.set_yticklabels() で y 軸の目盛りのラベル一覧を10個置きにスライスして設定する。
71
+
72
+ ```python
73
+ from io import StringIO
74
+
75
+ import pandas as pd
76
+ import matplotlib.pyplot as plt
77
+ import seaborn as sns
78
+
79
+ sns.set(style="whitegrid")
80
+
81
+ # データフレーム作成
82
+ num_samples = 100
83
+ data = pd.DataFrame(
84
+ {
85
+ "price": np.arange(855000, 855000 + num_samples),
86
+ "sell": np.random.uniform(0, 10, num_samples),
87
+ "buy": np.random.uniform(0, 10, num_samples),
88
+ }
89
+ )
90
+
91
+ # 描画する。
92
+ fig, ax = plt.subplots(figsize=(7, 5))
93
+
94
+ # 棒グラフのデフォルトは右方向→
95
+ # sell の値は符号を反転させることで左方向←に棒グラフが作成されるようにする。
96
+ copied = data.copy() # 元のデータを変更しないようにコピーしておく。
97
+ copied["sell"] *= -1
98
+
99
+ colors = ["#60D394", "#FFD97D"] # 色
100
+ names = ["sell", "buy"] # 列名
101
+
102
+ for name, color in zip(names, colors):
103
+ sns.barplot(
104
+ x=name,
105
+ y="price",
106
+ data=copied,
107
+ color=color,
108
+ label=name,
109
+ orient="h",
110
+ order=copied["price"].iloc[::-1],
111
+ ax=ax,
112
+ )
113
+
114
+ ax.set_xlabel("") # x 軸のラベル
115
+ ax.set_ylabel("Price", fontsize=12) # y 軸のラベル
116
+ # x 軸の範囲を左右対称になるように調整する。
117
+ max_val = data[["sell", "buy"]].values.max() * 1.1
118
+ ax.set_xlim(-max_val, max_val)
119
+
120
+ # y 軸の目盛りの間隔を10個おきに調整する。
121
+ yticks = ax.get_yticks()
122
+ yticklabels = ax.get_yticklabels()
123
+ ax.set_yticks(yticks[::10])
124
+ ax.set_yticklabels(yticklabels[::10])
125
+
126
+ ax.legend() # 凡例追加
127
+
128
+ plt.show()
129
+ ```
130
+
131
+ ![イメージ説明](2e9f4dc611bfc62007037c9f5641b680.png)

1

d

2019/06/06 08:59

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -24,7 +24,7 @@
24
24
  8 855800.0 19.0589 3.3583
25
25
  9 855900.0 5.8494 6.9031"""
26
26
  )
27
- data = pd.read_csv(src, delim_whitespace=True, index_col=[0])
27
+ data = pd.read_csv(src, delim_whitespace=True)
28
28
 
29
29
  # 描画する。
30
30
  fig, ax = plt.subplots(figsize=(7, 5))
@@ -36,9 +36,17 @@
36
36
 
37
37
  colors = ["#60D394", "#FFD97D"] # 色
38
38
  names = ["sell", "buy"] # 列名
39
+
39
40
  for name, color in zip(names, colors):
40
41
  sns.barplot(
42
+ x=name,
43
+ y="price",
44
+ data=copied,
45
+ color=color,
46
+ label=name,
47
+ orient="h",
41
- x=name, y="price", data=copied, color=color, label=name, orient="h", ax=ax
48
+ order=copied["price"].iloc[::-1],
49
+ ax=ax,
42
50
  )
43
51
 
44
52
  ax.set_xlabel("") # x 軸のラベル
@@ -50,4 +58,4 @@
50
58
 
51
59
  plt.show()
52
60
  ```
53
- ![イメージ説明](51178b88492a77c263aa67fc35196704.png)
61
+ ![イメージ説明](15fcb1aca7fd0512833a1fcb7d06bfa1.png)