y軸の数値ラベルとtick、そしてy軸のラベルを打つ。そのために、drawVolumeLabels()関数を作成し、その関数内で、
1 2 3 4 5 6 7 8 9 10 11 12 13 |
for v in range(dataMin, int(dataMax)+1, volumeIntervalMinor): if (v % volumeIntervalMinor == 0): y = map(v, dataMin, dataMax, plotY2, plotY1) if (v % volumeInterval == 0): textOffset = textAscent()/2 if (v == dataMin): textOffset = 0 elif (v == dataMax): textOffset = textAscent() text(floor(v), plotX1 - 10, y + textOffset) line(plotX1 - 4, y, plotX1, y) else: line(plotX1 - 2, y, plotX1, y) |
として、グローバル変数volumeInterval = 10と、volumeIntervalMinor = 5で割り切れる値にtickラインを追加する。
figure_05_ylabels_and_ticks_py.pyde
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 |
import FloatTable width = 720 height = 405 #Corners of the plotted time series plotX1 = 50 plotX2 = width - plotX1 plotY1 = 60 plotY2 = height - plotY1 dataMin = 0 dataMax = 0 years =[] value =[] currentColumn = 0 yearInterval = 10 volumeInterval = 10 volumeIntervalMinor = 5 def setup(): global data global dataMin global dataMax global value global years global years_str global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 global currentColumn global columnCount global rowCount global yearInterval global volumeIntervalMinor global volumeInterval size(width, height) data = FloatTable.FloatTable("milk-tea-coffee.tsv") columnCount = data.getColumnCount() years_str = data.getRowNames() for i in range(len(years_str)): years.append(int(years_str[i])) print "years=:" print years yearMin = years[0] yearMax = years[len(years) - 1] #dataMax = data.getTableMax() dataMax = ceil(data.getTableMax() / volumeInterval) * volumeInterval smooth() def draw(): global plotX1 global plotX2 global plotY1 global plotY2 background(224); #Show the plot area as a white box fill(255) rectMode(CORNERS) noStroke() rect(plotX1, plotY1, plotX2, plotY2) drawTitle() drawYearLabels() strokeWeight(5) stroke('#5679C1') drawDataPoints(currentColumn) drawVolumeLabels() def drawDataPoints(col): global rowCount global data global dataMin global dataMax global value global years global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 #dataMin = 0 #dataMax = data.getTableMax() #dataMax = 100 rowCount = data.getRowCount() for row in range(rowCount): #if (data.isValid(row, col)): value = data.getFloat(row, col) x = map(years[row], yearMin, yearMax, plotX1, plotX2) y = map(value, dataMin, dataMax, plotY2, plotY1) point(x, y) def drawTitle(): fill(0) textSize(20) textAlign(LEFT) title = data.getColumnName(currentColumn) text(title, plotX1, plotY1 - 10) def drawYearLabels(): global rowCount global data global yearInterval global volumeInterval global volumeIntervalMinor global years global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 fill(0) textSize(10) textAlign(CENTER, TOP) stroke(224) strokeWeight(1) rowCount = data.getRowCount() for row in range(rowCount): if (years[row] % yearInterval == 0): x = map(years[row], yearMin, yearMax, plotX1, plotX2) text(years[row], x, plotY2 + textAscent()+ 10) line(x, plotY1, x, plotY2) def drawVolumeLabels(): global dataMin global dataMax global plotX1 global plotX2 global plotY1 global plotY2 global volumeInterval global volumeIntervalMinor fill(0) textSize(10) textAlign(RIGHT) stroke(128) strokeWeight(1) for v in range(dataMin, int(dataMax)+1, volumeIntervalMinor): if (v % volumeIntervalMinor == 0): y = map(v, dataMin, dataMax, plotY2, plotY1) if (v % volumeInterval == 0): textOffset = textAscent()/2 if (v == dataMin): textOffset = 0 elif (v == dataMax): textOffset = textAscent() text(floor(v), plotX1 - 10, y + textOffset) line(plotX1 - 4, y, plotX1, y) else: line(plotX1 - 2, y, plotX1, y) def keyPressed(): global currentColumn global columnCount if (key == '['): currentColumn = currentColumn - 1 if (currentColumn < 0): currentColumn = columnCount - 1 elif (key == ']'): currentColumn = currentColumn + 1 if (currentColumn == columnCount): currentColumn = 0 |
最後にdrawAxisLabels()関数を追加し、X軸、Y軸のタイトルを、
1 2 |
text("Gallons\nconsumed\nper capita", labelX, (plotY1+plotY2)/2) text("Year", (plotX1+plotX2)/2, labelY) |
と記載して、一旦完成。
figure_06_finalish_py.pyde
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 |
import FloatTable width = 720 height = 405 dataMin = 0 dataMax = 0 #Corners of the plotted time series plotX1 = 120 plotX2 = width - 80 labelX = 50 plotY1 = 60 plotY2 = height - 70 labelY = height - 25 currentColumn = 0 yearInterval = 10 volumeInterval = 10 volumeIntervalMinor = 5 years =[] value =[] def setup(): global data global dataMin global dataMax global value global years global years_str global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 global labelX global labelY global currentColumn global columnCount global rowCount global yearInterval global volumeIntervalMinor global volumeInterval size(width, height) data = FloatTable.FloatTable("milk-tea-coffee.tsv") columnCount = data.getColumnCount() years_str = data.getRowNames() for i in range(len(years_str)): years.append(int(years_str[i])) print "years=:" print years yearMin = years[0] yearMax = years[len(years) - 1] #dataMax = data.getTableMax() dataMax = ceil(data.getTableMax() / volumeInterval) * volumeInterval smooth() def draw(): global plotX1 global plotX2 global plotY1 global plotY2 background(224); #Show the plot area as a white box fill(255) rectMode(CORNERS) noStroke() rect(plotX1, plotY1, plotX2, plotY2) drawTitle() drawYearLabels() drawAxisLabels() strokeWeight(5) stroke('#5679C1') drawDataPoints(currentColumn) drawVolumeLabels() def drawDataPoints(col): global rowCount global data global dataMin global dataMax global value global years global yearMin global yearMax global plotX1 global plotX2 global plotY1 global plotY2 #dataMin = 0 #dataMax = data.getTableMax() #dataMax = 100 rowCount = data.getRowCount() for row in range(rowCount): if (data.isValid(row, col)): value = data.getFloat(row, col) x = map(years[row], yearMin, yearMax, plotX1, plotX2) y = map(value, dataMin, dataMax, plotY2, plotY1) point(x, y) def drawTitle(): global data global plotX1 global plotX2 fill(0) textSize(20) textAlign(LEFT) title = data.getColumnName(currentColumn) text(title, plotX1, plotY1 - 10) def drawYearLabels(): global rowCount global data global yearInterval global plotX1 global plotX2 global plotY1 global plotY2 fill(0) textSize(10) textAlign(CENTER, TOP) stroke(224) strokeWeight(1) rowCount = data.getRowCount() for row in range(rowCount): if (years[row] % yearInterval == 0): x = map(years[row], yearMin, yearMax, plotX1, plotX2) text(years[row], x, plotY2 + textAscent()+ 10) line(x, plotY1, x, plotY2) def drawVolumeLabels(): global dataMin global dataMax global plotX1 global plotX2 global plotY1 global plotY2 global volumeInterval global volumeIntervalMinor fill(0) textSize(10) textAlign(RIGHT) stroke(128) strokeWeight(1) for v in range(dataMin, dataMax+1, volumeIntervalMinor): if (v % volumeIntervalMinor == 0): y = map(v, dataMin, dataMax, plotY2, plotY1) if (v % volumeInterval == 0): if (v == dataMin): textAlign(RIGHT) elif (v == dataMax): textAlign(RIGHT, TOP) else: textAlign(RIGHT, CENTER) text(floor(v), plotX1 - 10, y) line(plotX1 - 4, y, plotX1, y) else: line(plotX1 - 2, y, plotX1, y) def drawAxisLabels(): global plotX1 global plotX2 global plotY1 global plotY2 global labelX global labelY fill(0) textSize(13) textLeading(15) textAlign(CENTER, CENTER) text("Gallons\nconsumed\nper capita", labelX, (plotY1+plotY2)/2) textAlign(CENTER) text("Year", (plotX1+plotX2)/2, labelY) def keyPressed(): global currentColumn global columnCount if (key == '['): currentColumn = currentColumn - 1 if (currentColumn < 0): currentColumn = columnCount - 1 elif (key == ']'): currentColumn = currentColumn + 1 if (currentColumn == columnCount): currentColumn = 0 |