57
57
-- in "ax". Plotting commands should use the current axis, never the plot
58
58
-- itself; the two APIs are almost identical. When creating low-level bindings
59
59
-- one must remember to call "plot.sci" to set the current image when plotting a
60
- -- graph. The current spine of the axes that's being manipulated is in "spine".
60
+ -- graph. The current spine of the axes that's being manipulated is in
61
+ -- "spine". The current quiver is in "q"
61
62
-----------------------------------------------------------------------------
62
63
63
64
module Graphics.Matplotlib
64
65
( module Graphics.Matplotlib
65
66
-- * Creating custom plots and applying options
66
- , Matplotlib (), Option (),(@@) , (%) , o1 , o2 , (##) , (#) , mp , def , readData
67
+ , Matplotlib (), Option (),(@@) , (%) , o1 , o2 , (##) , (#) , mp , def , readData , readImage
67
68
, str , raw , lit , updateAxes , updateFigure , mapLinear )
68
69
where
69
70
import Data.List
@@ -89,6 +90,7 @@ file filename m = withMplot m (\s -> python $ pyIncludes (pyBackend "agg") ++ s
89
90
-- | Plot the cross-correlation and autocorrelation of several variables. TODO Due to
90
91
-- a limitation in the options mechanism this takes explicit options.
91
92
xacorr xs ys opts = readData (xs, ys)
93
+ % figure
92
94
% addSubplot 2 1 1
93
95
% xcorr xs ys @@ opts
94
96
% grid True
@@ -111,6 +113,11 @@ scatter :: (ToJSON t1, ToJSON t) => t1 -> t -> Matplotlib
111
113
scatter x y = readData (x, y)
112
114
% mp # " plot.sci(ax.scatter(data[0], data[1]" ## " ))"
113
115
116
+ -- | Create a bar at a position with a height
117
+ bar :: (ToJSON t1 , ToJSON t ) => t1 -> t -> Matplotlib
118
+ bar left height = readData (left, height)
119
+ % mp # " ax.bar(data[0], data[1]" ## " )"
120
+
114
121
-- | Plot a line
115
122
line :: (ToJSON t1 , ToJSON t ) => t1 -> t -> Matplotlib
116
123
line x y = plot x y `def` [o1 " -" ]
@@ -171,11 +178,29 @@ matShow :: ToJSON a => a -> Matplotlib
171
178
matShow d = readData d
172
179
% (mp # " plot.sci(ax.matshow(data" ## " ))" )
173
180
181
+ -- | Plot an image
182
+ imshow :: MplotImage a => a -> Matplotlib
183
+ imshow i = readImage i
184
+ % (mp # " plot.sci(ax.imshow(img" ## " ))" )
185
+
174
186
-- | Plot a matrix
175
187
pcolor :: ToJSON a => a -> Matplotlib
176
188
pcolor d = readData d
177
189
% (mp # " plot.sci(ax.pcolor(np.array(data)" ## " ))" )
178
190
191
+ -- | Plot a matrix
192
+ pcolor3 x y z = readData (x,y,z)
193
+ % (mp # " plot.sci(ax.pcolor(np.array(data[0]),np.array(data[1]),np.array(data[2])" ## " ))" )
194
+
195
+ -- | Create a non-uniform image from samples
196
+ nonUniformImage x y z = readData (x,y,z)
197
+ % mp # " im = mpimg.NonUniformImage(ax" ## " )"
198
+ % mp # " im.set_data(data[0], data[1], data[2])"
199
+
200
+ -- | Create a pie chart
201
+ pie l = readData l
202
+ % mp # " plot.pie(" # l ## " )"
203
+
179
204
-- | Plot a KDE of the given functions; a good bandwith will be chosen automatically
180
205
density :: [Double ] -> Maybe (Double , Double ) -> Matplotlib
181
206
density l maybeStartEnd =
@@ -186,6 +211,9 @@ density l maybeStartEnd =
186
211
187
212
-- * Matplotlib configuration
188
213
214
+ -- | Set an rc parameter
215
+ rc s = mp # " plot.rc(" # str s ## " )"
216
+
189
217
-- | Set an rcParams key-value
190
218
setParameter k v = mp # " matplotlib.rcParams[" # str k # " ] = " # v
191
219
@@ -207,6 +235,9 @@ dataPlot a b = mp # "p = ax.plot(data[" # a # "], data[" # b # "]" ## ")"
207
235
plot :: (ToJSON t , ToJSON t1 ) => t1 -> t -> Matplotlib
208
236
plot x y = readData (x, y) % dataPlot 0 1
209
237
238
+ streamplot x y u v = readData (x, y, u, v)
239
+ % mp # " ax.streamplot(np.asarray(data[0]), np.asarray(data[1]), np.asarray(data[2]), np.asarray(data[3])" ## " )"
240
+
210
241
-- | Plot x against y where x is a date.
211
242
-- xunit is something like 'weeks', yearStart, monthStart, dayStart are an offset to x.
212
243
-- TODO This isn't general enough; it's missing some settings about the format. The call is also a mess.
@@ -310,11 +341,24 @@ xcorr x y = readData (x, y) % mp # "ax.xcorr(data[0], data[1]" ## ")"
310
341
-- | Plot auto-correlation
311
342
acorr x = readData x % mp # " ax.acorr(data" ## " )"
312
343
344
+ -- | A quiver plot; color is optional and can be nothing
345
+ quiver x y u v Nothing = readData(x,y,u,v)
346
+ % mp # " q = ax.quiver(data[0], data[1], data[2], data[3]" ## " )"
347
+ quiver x y u v (Just c) = readData(x,y,u,v,c)
348
+ % mp # " q = ax.quiver(data[0], data[1], data[2], data[3], data[4]" ## " )"
349
+
350
+ -- | A key of a given size with a label for a quiver plot
351
+ quiverKey x y u label = mp # " ax.quiverkey(q, " # x# " , " # y# " , " # u# " , " # label## " )"
352
+
313
353
-- | Plot text at a specified location
314
354
text x y s = mp # " ax.text(" # x # " ," # y # " ," # raw s ## " )"
315
355
356
+ -- | Add a text to a figure instead of a particular plot
316
357
figText x y s = mp # " plot.figtext(" # x # " ," # y # " ," # raw s ## " )"
317
358
359
+ -- | Add an annotation
360
+ annotate s = mp # " ax.annotate(" # str s ## " )"
361
+
318
362
-- * Layout, axes, and legends
319
363
320
364
-- | Square up the aspect ratio of a plot.
@@ -386,16 +430,16 @@ axis3DLabels xs ys zs =
386
430
% mp # " ax.set_zlim3d(" # minimum2 zs # " , " # maximum2 zs # " )"
387
431
388
432
-- | Add a label to the x axis
389
- xLabel :: String -> Matplotlib
390
- xLabel label = mp # " ax.set_xlabel(" # raw label ## " )"
433
+ xlabel :: String -> Matplotlib
434
+ xlabel label = mp # " ax.set_xlabel(" # raw label ## " )"
391
435
392
436
-- | Add a label to the y axis
393
- yLabel :: String -> Matplotlib
394
- yLabel label = mp # " ax.set_ylabel(" # raw label ## " )"
437
+ ylabel :: String -> Matplotlib
438
+ ylabel label = mp # " ax.set_ylabel(" # raw label ## " )"
395
439
396
440
-- | Add a label to the z axis
397
- zLabel :: String -> Matplotlib
398
- zLabel label = mp # " ax.set_zlabel(" # raw label ## " )"
441
+ zlabel :: String -> Matplotlib
442
+ zlabel label = mp # " ax.set_zlabel(" # raw label ## " )"
399
443
400
444
setSizeInches w h = mp # " fig.set_size_inches(" # w # " ," # h # " , forward=True)"
401
445
@@ -461,9 +505,12 @@ subplots = mp # "fig, axes = plot.subplots(" ## ")"
461
505
-- | Access a subplot
462
506
setSubplot s = mp # " ax = axes[" # s # " ]" % setAx
463
507
464
- -- | Add axes to a figure
508
+ -- | Add axes to a plot
465
509
axes = mp # " ax = plot.axes(" ## " )" % updateAxes % setAx
466
510
511
+ -- | Add axes to a figure
512
+ addAxes = mp # " ax = fig.add_axes(" ## " )" % updateAxes % setAx
513
+
467
514
-- | Creates a new figure with the given id. If the Id is already in use it
468
515
-- switches to that figure.
469
- figure id = mp # " plot.figure(" # id ## " )" % updateFigure
516
+ figure = mp # " plot.figure(" ## " )" % updateFigure
0 commit comments