Skip to content

Commit 2ce3471

Browse files
committed
Init
0 parents  commit 2ce3471

File tree

81 files changed

+2767
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+2767
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.pyc
2+
.DS_Store
3+
*.gif
4+
*.pdf
5+
*.png
6+
__pycache__

README.md

+377
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,377 @@
1+
# Python Plotting Tools
2+
3+
This repository provides a python code to generate figures (e.g., curves and barcharts) that can be used in the paper to show the results.
4+
5+
Dependencies: Python 3.+, numpy, and matplotlib.
6+
7+
## Table of Contents
8+
* [Preliminary](#preliminary)
9+
+ [Layout of the diagram](#layout-of-the-diagram)
10+
+ [Sample configuration file](#sample-configuration-file)
11+
* [Examples for Plotting Curves](#examples-for-plotting-curves)
12+
+ [Plot simple curves](#plot-simple-curves)
13+
+ [Plot dots](#plot-dots)
14+
+ [Plot figure with customized xticklabel](#plot-figure-with-customized-xticklabel)
15+
+ [Plot figure with two different Y-axes](#plot-figure-with-two-different-y-axes)
16+
+ [Plot figure with customized legends](#plot-figure-with-customized-legends)
17+
* [Examples for Plot Functions](#examples-for-plot-functions)
18+
* [Examples for Plotting Barchart](#examples-for-plotting-barchart)
19+
+ [Layout of the barchart](#layout-of-the-barchart)
20+
+ [Plot barchart with customized yticklabel](#plot-barchart-with-customized-yticklabel)
21+
+ [Plot barchart with four bars in each group](#plot-barchart-with-four-bars-in-each-group)
22+
* [Create Colorbar](#create-colorbar)
23+
* [Crop Patches for Zoom-in Comparison](#crop-patches-for-zoom-in-comparison)
24+
25+
## Preliminary
26+
### Layout of the diagram
27+
The following shows a simple but complete diagram.
28+
<p align="center">
29+
<img src='examples/demo/demo_simple_plot.jpg' width="600" >
30+
</p>
31+
32+
It contains the following common components. When creating a new diagram, we will modify these components to present our data:
33+
- Title
34+
- X-Label, xtick, and, xticklabel
35+
- Y-Label, ytick, and, yticklabel
36+
- Line, Marker, Legend
37+
- Grid
38+
39+
<p align="center">
40+
<img src='examples/demo/demo_layout.jpg' width="700" >
41+
</p>
42+
43+
### Sample configuration file
44+
In this code, we define the appearance of the diagram with a configuration file.
45+
Then, we can plot the diagram by simply running:
46+
```shell
47+
python plot_diagram.py examples/demo/simple_plot.conf
48+
```
49+
The configuration file for the above simple plot is shown below with comments.
50+
51+
```shell
52+
# CONFIGURATION FILE
53+
54+
# Comments start with '#';
55+
# Parameters start with '!';
56+
# If a parameter contains space, please replace the space with '&' for correct parsing
57+
# For bool type, 1 is True else False
58+
59+
# Plot type: ploty|plotxy|plottwins
60+
# ploty: The input data only contains Y values, the X values are generated as [0, ..., len(Y)]
61+
# plotxy: The input data contains both X and Y values
62+
# plottwins: The input data only contains Y values. Plot figure with two different Y-axis
63+
! plot_type plotxy
64+
65+
# Figure format: pdf|jpg|png
66+
! format pdf
67+
68+
# Canvas setting, fig size in inches
69+
# https://matplotlib.org/devdocs/gallery/subplots_axes_and_figures/figure_size_units.html
70+
! width 7
71+
! height 3
72+
! dpi 220
73+
74+
# Line and marker setting, different lines have different colors and marker shapes
75+
# https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
76+
# Example colors: 'r', 'k', 'b', 'g', 'y', 'm', 'c', 'tab:blue', 'tab:orange'
77+
# Example markers: 'd', 'v', '1', '8', 'o', '^', '<', '>', 's', '*', 'p'
78+
! linewidth 1.5
79+
! line_style -
80+
! color tab:blue tab:orange tab:green
81+
! markersize 4
82+
! marker d v *
83+
84+
# Title and label setting
85+
# None indicates ignore; '&' is a placeholder for space;
86+
# Eample font sizes: 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'larger', 'smaller'
87+
! title Simple&Plot
88+
! title_font x-large
89+
! xlabel x-Label
90+
! xlabel_font x-large
91+
! ylabel y-Label
92+
! ylabel_font x-large
93+
94+
# Legend setting
95+
# https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html
96+
# Example legend loc: 'best', 'upper left', 'upper right', 'lower left', 'lower right'
97+
! legend Linear Quadratic Cubic
98+
! legend_loc upper&left
99+
! legend_font x-large
100+
! legend_ncol 1
101+
102+
# Set grid on or off, 1 for on, 0 for off
103+
! grid_on 1
104+
105+
# Data configuration
106+
# Store the data values of a curve in a file, e.g., data.txt
107+
# If have multiple curves, just list the file names one by one
108+
! datafile data/linear.txt data/quadratic.txt data/cubic.txt
109+
110+
# Specify the maximum number of points,
111+
! max_point_num 1000
112+
113+
# set whether sort the data (None|ascend|descend), all x values should be the same for different curves
114+
! sort_data None
115+
```
116+
117+
## Examples for Plotting Curves
118+
119+
### Plot simple curves
120+
The main difference between the following three configuration files is the number of curves.
121+
```shell
122+
# Figure at the below left
123+
python plot_diagram.py examples/curve_simple_example/ploty_single_curve.conf
124+
125+
# Figure at the below middle
126+
python plot_diagram.py examples/curve_simple_example/ploty_two_curves.conf
127+
128+
# Figure at the below right
129+
python plot_diagram.py examples/curve_simple_example/ploty_multi_curves.conf
130+
```
131+
132+
<p align="center">
133+
<img src='examples/curve_simple_example/curve_simple_example_ploty_single_curve.jpg' width="250" >
134+
<img src='examples/curve_simple_example/curve_simple_example_ploty_two_curves.jpg' width="250" >
135+
<img src='examples/curve_simple_example/curve_simple_example_ploty_multi_curves.jpg' width="250">
136+
</p>
137+
138+
### Plot dots
139+
By adding "`! draw_dot 1`" in the `.conf`, we can plot dots instead of lines.
140+
```shell
141+
python plot_diagram.py examples/curve_simple_example/ploty_multi_dots.conf
142+
```
143+
<p align="center">
144+
<img src='examples/curve_simple_example/curve_simple_example_ploty_multi_dots.jpg' width="400">
145+
</p>
146+
147+
### Plot figure with customized xticklabel
148+
We can manually set the xticklabel in the configuration file. e.g., adding "`! xticklabel 2 4 9 18 30 36 45 60 90 180 $\infty$`".
149+
```shell
150+
python plot_diagram.py examples/curve_custom_xtick/ploty_set_xtick.conf
151+
```
152+
<p align="center">
153+
<img src='examples/curve_custom_xtick/curve_custom_xtick_ploty_set_xtick.jpg' width="400">
154+
</p>
155+
156+
We can also load the xticklabel from a file by setting the path, e.g., adding "`! xtick_path data/merl_name.txt`". We can rotate the xticklabel if they are too long by adding "`! xtick_rot 90`".
157+
158+
```shell
159+
python plot_diagram.py examples/curve_custom_xtick/ploty_set_rotate_xtick.conf
160+
# Remember that we can plot dots by setting draw_dot to 1 in the configuration file
161+
```
162+
163+
<p align="center">
164+
<img src='examples/curve_custom_xtick/curve_custom_xtick_ploty_set_rotate_xtick.jpg' width="1000">
165+
<img src='examples/curve_custom_xtick/curve_custom_xtick_ploty_set_rotate_xtick_dots.jpg' width="1000">
166+
</p>
167+
168+
### Plot figure with two different Y-axes
169+
By setting the `plot_type` to `plottwins`, we can draw the figure with two different Y-axes. But remember that this current implementation only supports two curves, one for each Y-axis.
170+
```shell
171+
python plot_diagram.py examples/curve_twin_y_axis/plottwins_yaxis.conf
172+
```
173+
<p align="center">
174+
<img src='examples/curve_twin_y_axis/curve_twin_y_axis_plottwins_yaxis.jpg' width="400">
175+
</p>
176+
177+
### Plot figure with customized legends
178+
Note that this example is a hardcode for this specific legend pattern (i.e., two curves share the same legend).
179+
```shell
180+
python plot_diagram.py examples/curve_custom_legend/ploty_custom_legend.conf
181+
```
182+
<p align="center">
183+
<img src='examples/curve_custom_legend/curve_custom_legend_ploty_custom_legend.jpg' width="400">
184+
</p>
185+
186+
## Examples for Plot Functions
187+
TODO.
188+
189+
## Examples for Plotting Barchart
190+
### Layout of the barchart
191+
The following shows a simple barchart.
192+
<p align="center">
193+
<img src='examples/barchart_example1/barchart_example1_simple_barchart.jpg' width="400"> <br>
194+
</p>
195+
196+
It contains the following common components. When creating a new barchart, we will modify these components to present our data:
197+
- Title
198+
- X-Label, xtick, and, xticklabel
199+
- Y-Label, ytick, and, yticklabel
200+
- Bar, Text, Legend
201+
- Grid
202+
<p align="center">
203+
<img src='examples/barchart_example1/barchart_layout.jpg' width="600">
204+
</p>
205+
206+
The above barchart can be generated by running:
207+
```shell
208+
python plot_diagram.py examples/barchart_example1/simple_barchart.conf
209+
```
210+
211+
<details>
212+
<summary>Configuration file for the above barchart</summary>
213+
214+
```shell
215+
# CONFIGURATION FILE
216+
217+
# Comments start with '#';
218+
# Parameters start with '!';
219+
# If a parameter contains space, please replace the space with '&' for correct parsing
220+
# For bool type, 1 is True else False
221+
222+
# Plot type: ploty|plotxy|plottwins
223+
# ploty: The input data only contains Y values, the X values are generated as [0, ..., len(Y)]
224+
# plotxy: The input data contains both X and Y values
225+
# plottwins: The input data only contains Y values. Plot figure with two different Y-axis
226+
! plot_type plotbar
227+
228+
# Figure format: pdf|jpg|png
229+
! format pdf
230+
231+
# Canvas setting, fig size in inches
232+
# https://matplotlib.org/devdocs/gallery/subplots_axes_and_figures/figure_size_units.html
233+
! width 5.5
234+
! height 3
235+
! dpi 220
236+
237+
# Data configuration
238+
# Store the data values of the barchart in a single file, e.g., data.txt
239+
# Each column corresponds to a group
240+
# The number of row equals to the number of bars in a group
241+
! datafile data/bar_data_3group.txt
242+
243+
# IMPORTANT: Please remember to update the color, legend, xticklabel to match the input
244+
245+
# Bar setting
246+
# Opacity sets the transparency of the bar, 0 indicates solid color
247+
# Number of color and Opacity should equal to the bar numbers
248+
! bar_width 0.3
249+
! color tab:blue tab:red
250+
! opacity 0.4 0.4
251+
! y_min 0
252+
! y_max 1
253+
254+
# xtick and ytick setting
255+
! xticklabel vs.&Method1 vs.&Method2 vs.&Method3
256+
# ! ytick 0 0.2 0.4 0.6 0.8 1.0
257+
# ! yticklabel 0 20% 40% 60% 80% 100%
258+
259+
# Text setting
260+
! put_text 1
261+
! text_font 18
262+
! percentage 0
263+
264+
# Title and label setting
265+
# None indicates ignore; '&' is a placeholder for space;
266+
# Eample font sizes: 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'larger', 'smaller'
267+
! title Title
268+
! title_font x-large
269+
! xlabel x-Label
270+
! xlabel_font x-large
271+
! ylabel y-Label
272+
! ylabel_font x-large
273+
274+
# Legend setting
275+
# https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html
276+
# Example legend loc: 'best', 'upper left', 'upper right', 'lower left', 'lower right'
277+
! legend Vote&Ours Vote&Others
278+
! legend_loc upper&left
279+
! legend_font xx-large
280+
! legend_ncol 1
281+
# You might need to tune the following bbox_to_anchor parameters to manually place the legends
282+
! bbox_to_anchor -0.015 1.40
283+
284+
# Set grid on or off, 1 for on, 0 for off
285+
! grid_on 1
286+
```
287+
</details>
288+
289+
### Plot barchart with customized yticklabel
290+
```shell
291+
python plot_diagram.py examples/barchart_example1/simple_barchart_custom_ytick.conf
292+
```
293+
We set yticklabel in percentage, legend column number to 2, and show text in percentage, by adding the following to the config file.
294+
```
295+
! ytick 0 0.2 0.4 0.6 0.8 1.0
296+
! yticklabel 0 20% 40% 60% 80% 100%
297+
! legend_ncol 2
298+
! percentage 1
299+
```
300+
<p align="center">
301+
<img src='examples/barchart_example1/barchart_example1_simple_barchart_custom_ytick.jpg' width="400">
302+
</p>
303+
304+
### Plot barchart with four bars in each group
305+
```shell
306+
python plot_diagram.py examples/barchart_example2/barchart_color.conf
307+
```
308+
<p align="center">
309+
<img src='examples/barchart_example2/barchart_example2_barchart_color.jpg' width="800">
310+
</p>
311+
312+
## Create Colorbar
313+
We also provide a simple script to generate colorbar.
314+
```shell
315+
python img_tools/color_bar.py --colormap jet
316+
python img_tools/color_bar.py --colormap jet --horizontal
317+
python img_tools/color_bar.py --colormap viridis
318+
python img_tools/color_bar.py --colormap viridis --horizontal
319+
```
320+
<p align="center">
321+
<img src='results/color_bar_jet.png' width="15">
322+
<img src='results/color_bar_jet_horz.png' width="200">
323+
<img src='results/color_bar_viridis.png' width="15">
324+
<img src='results/color_bar_viridis_horz.png' width="200">
325+
</p>
326+
327+
## Crop Patches for Zoom-in Comparison
328+
As it is very common to show zoom-in comparison between different methods in the paper, we provide a small image cropping scripts for this task.
329+
<p align="center">
330+
<img src='examples/image_cropper_example/input.jpg' width="250"> &emsp;
331+
<img src='examples/image_cropper_example/method1.jpg' width="250"> &emsp;
332+
<img src='examples/image_cropper_example/method2.jpg' width="250">
333+
</p>
334+
335+
By specifying the directory storing images, the desired box locations, and the colors, the following command can crop and highlight the boxes in the original images. However, you have to determine the locations of the boxes [left top bottom right] using other softwares.
336+
```shell
337+
python img_tools/image_cropper.py --in_dir examples/image_cropper_example/ -k '*.jpg' \
338+
--save_dir ROI --save_ext .jpg \
339+
--boxes 118 60 193 150 --boxes 371 452 431 521 --colors r g
340+
# bash scripts/image_cropping.sh
341+
```
342+
<p align="center">
343+
<img src='examples/image_cropper_example/ROI/input_draw.jpg' width="250"> &emsp;
344+
<img src='examples/image_cropper_example/ROI/method1_draw.jpg' width="250"> &emsp;
345+
<img src='examples/image_cropper_example/ROI/method2_draw.jpg' width="250">
346+
<br>
347+
<img src='examples/image_cropper_example/ROI/input_00_ROI.jpg' width="125" height='100'>
348+
<img src='examples/image_cropper_example/ROI/input_01_ROI.jpg' width="125" height='100'> &emsp;
349+
<img src='examples/image_cropper_example/ROI/method1_00_ROI.jpg' width="125" height='100'>
350+
<img src='examples/image_cropper_example/ROI/method1_01_ROI.jpg' width="125" height='100'> &emsp;
351+
<img src='examples/image_cropper_example/ROI/method2_00_ROI.jpg' width="125" height='100'>
352+
<img src='examples/image_cropper_example/ROI/method2_01_ROI.jpg' width="125" height='100'>
353+
</p>
354+
355+
We can also add arrows onto the images to further highlight the differences.
356+
```shell
357+
python img_tools/image_cropper.py --in_dir examples/image_cropper_example/ --key '*.jpg' \
358+
--save_dir ROI_arrow --save_ext .jpg \
359+
--boxes 118 60 193 150 --boxes 371 452 431 521 --colors r g \
360+
--arrows 86 138 99 154 --arrows 502 412 488 393 --arrow_color r g
361+
```
362+
<p align="center">
363+
<img src='examples/image_cropper_example/ROI_arrow/input_draw.jpg' width="250"> &emsp;
364+
<img src='examples/image_cropper_example/ROI_arrow/method1_draw.jpg' width="250"> &emsp;
365+
<img src='examples/image_cropper_example/ROI_arrow/method2_draw.jpg' width="250">
366+
<br>
367+
<img src='examples/image_cropper_example/ROI_arrow/input_00_ROI_arrow.jpg' width="125" height='100'>
368+
<img src='examples/image_cropper_example/ROI_arrow/input_01_ROI_arrow.jpg' width="125" height='100'> &emsp;
369+
<img src='examples/image_cropper_example/ROI_arrow/method1_00_ROI_arrow.jpg' width="125" height='100'>
370+
<img src='examples/image_cropper_example/ROI_arrow/method1_01_ROI_arrow.jpg' width="125" height='100'> &emsp;
371+
<img src='examples/image_cropper_example/ROI_arrow/method2_00_ROI_arrow.jpg' width="125" height='100'>
372+
<img src='examples/image_cropper_example/ROI_arrow/method2_01_ROI_arrow.jpg' width="125" height='100'>
373+
</p>
374+
375+
TODO: support selecting boxes in an interactive manner.
376+
377+
Loading
Loading
609 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
0.76 0.78
2+
0.24 0.22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
0.76 0.78 0.60
2+
0.24 0.22 0.40

0 commit comments

Comments
 (0)