GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pythonlib/cardinal_pythonlib/plot.py at master · RudolfCardinal/pythonlib

Expand file tree

/

plot.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
123 lines (100 loc) · 4.11 KB
/

plot.py

Copy path

File metadata and controls

123 lines (100 loc) · 4.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python
# cardinal_pythonlib/plot.py
"""
===============================================================================
Original code copyright (C) 2009-2022 Rudolf Cardinal ([email protected]).
This file is part of cardinal_pythonlib.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
===============================================================================
**Support for plotting via matplotlib.pyplot.**
"""
import io
# noinspection PyUnresolvedReferences
from types import ModuleType
from typing import TYPE_CHECKING, Union
from cardinal_pythonlib import rnc_web
if TYPE_CHECKING:
try:
# noinspection PyPackageRequirements
from matplotlib.figure import Figure
except ImportError:
Figure = None
# =============================================================================
# Image embedding in PDFs
# =============================================================================
# xhtml2pdf (2013-04-11) supports PNG, but not SVG.
# You can convert SVG to PNG for embedding:
# https://stackoverflow.com/questions/787287
# You could make a PDF and append it, though that would (without further
# effort) lack the patient headers.
def png_img_html_from_pyplot_figure(
fig: "Figure", dpi: int = 100, extra_html_class: str = None
) -> str:
"""
Converts a ``pyplot`` figure to an HTML IMG tag with encapsulated PNG.
"""
if fig is None:
return ""
# Make a file-like object
memfile = io.BytesIO()
# In general, can do
# fig.savefig(filename/file-like-object/backend, format=...)
# or
# backend.savefig(fig):
# see e.g. http://matplotlib.org/api/backend_pdf_api.html
# noinspection PyUnresolvedReferences
fig.savefig(memfile, format="png", dpi=dpi)
memfile.seek(0)
pngblob = memoryview(memfile.read())
return rnc_web.get_png_img_html(pngblob, extra_html_class)
def svg_html_from_pyplot_figure(fig: "Figure") -> str:
"""
Converts a ``pyplot`` figure to an SVG tag.
"""
if fig is None:
return ""
memfile = io.BytesIO() # StringIO doesn't like mixing str/unicode
# noinspection PyUnresolvedReferences
fig.savefig(memfile, format="svg")
return memfile.getvalue().decode("utf-8") # returns a text/Unicode type
# SVG works directly in HTML; it returns <svg ...></svg>
# =============================================================================
# Plotting
# =============================================================================
def set_matplotlib_fontsize(
matplotlib: ModuleType, fontsize: Union[int, float] = 12
) -> None:
"""
Sets the current font size within the ``matplotlib`` library.
**WARNING:** not an appropriate method for multithreaded environments, as
it writes (indirectly) to ``matplotlib`` global objects. See CamCOPS for
alternative methods.
"""
font = {
# https://stackoverflow.com/questions/3899980
# http://matplotlib.org/users/customizing.html
"family": "sans-serif",
# ... serif, sans-serif, cursive, fantasy, monospace
"style": "normal", # normal (roman), italic, oblique
"variant": "normal", # normal, small-caps
"weight": "normal",
# ... normal [=400], bold [=700], bolder [relative to current],
# lighter [relative], 100, 200, 300, ..., 900
"size": fontsize, # in pt (default 12)
}
# noinspection PyUnresolvedReferences
matplotlib.rc("font", **font)
legend = {
# https://stackoverflow.com/questions/7125009
"fontsize": fontsize
}
# noinspection PyUnresolvedReferences
matplotlib.rc("legend", **legend)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.