Skip to content

Commit a9c867e

Browse files
committed
Add tip about original resolution output
1 parent 8f4ae09 commit a9c867e

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ https://kolibril13.github.io/plywood-gallery-matplotlib-scalebar/
8787

8888
![citurs](https://user-images.githubusercontent.com/44469195/202899151-483bad4b-bacf-4845-a7cd-ace7bb6417b1.png)
8989

90+
## Tips
91+
92+
### Save image in original resolution
93+
94+
The code snippet below shows how to save an image (stored in variable `arr`) in the same resolution as the original image without any horizontal and vertical axes.
95+
96+
```python
97+
dpi = 200
98+
fig = plt.figure(figsize=(arr.shape[1] / dpi, arr.shape[0] / dpi), frameon=False, dpi=dpi)
99+
100+
ax = fig.add_axes([0.0, 0.0, 1.0, 1.0])
101+
ax.set_axis_off()
102+
103+
ax.imshow(arr)
104+
105+
scalebar = ScaleBar(100, "nm", length_fraction=0.25, location="lower right")
106+
ax.add_artist(scalebar)
107+
108+
fig.savefig("original_resolution.png", dpi=dpi)
109+
```
90110

91111
## ScaleBar arguments
92112

doc/original_resolution.png

58.7 KB
Loading

doc/original_resolution.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import matplotlib.cbook as cbook
4+
from matplotlib_scalebar.scalebar import ScaleBar
5+
6+
# Load image
7+
with cbook.get_sample_data("s1045.ima.gz") as dfile:
8+
arr = np.frombuffer(dfile.read(), np.uint16).reshape((256, 256))
9+
10+
# Create figure
11+
dpi = 200
12+
fig = plt.figure(
13+
figsize=(arr.shape[1] / dpi, arr.shape[0] / dpi), frameon=False, dpi=dpi
14+
)
15+
16+
ax = fig.add_axes([0.0, 0.0, 1.0, 1.0])
17+
ax.set_axis_off()
18+
19+
ax.imshow(arr)
20+
21+
scalebar = ScaleBar(100, "nm", length_fraction=0.25, location="lower right")
22+
ax.add_artist(scalebar)
23+
24+
fig.savefig("original_resolution.png", dpi=dpi)

0 commit comments

Comments
 (0)