欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?如何將?matplotlib?圖表集成進到PDF?中

 更新時間:2022年03月28日 17:22:47   作者:Python中文社區(qū)  
這篇文章主要介紹了Python?如何將?matplotlib?圖表集成進到PDF?中,文章介紹內容詳細,具有一定的參考價值,需要的小伙伴可以參考一下,希望對你的學習有所幫助

1.介紹

PDF 格式是與平臺無關,它獨立于底層操作系統(tǒng)和渲染引擎。事實上,PDF 是基于一種腳本語言——PostScript,它是第一個獨立于設備的頁面描述語言。

在本指南中,我們將使用 borb —— 一個專門用于閱讀、操作和生成 PDF 文檔的 Python 庫。它提供了一個低級模型(允許您訪問精確的坐標和布局)和一個高級模型(您可以將邊距、位置等精確計算委托給布局管理器) .

matplotlib 是一個數(shù)據(jù)可視化庫,也是許多其他流行庫(如 Seaborn)背后的引擎。

基于用于創(chuàng)建報告(通常包括圖形)的常見 PDF 文檔,我們將看看如何使用 borbMatplotlib 圖表集成到 PDF 文檔中。

2.安裝 borb和 matplotlib

borb 可以從 GitHub 上的源代碼下載,或通過 pip 安裝:

$ pip install borb

matplotlib 也可以通過 pip 安裝:

$ pip install matplotlib

用 Borb 在 PDF 文檔中集成 Matplotlib 圖表

在創(chuàng)建餅圖等圖表之前,我們將編寫一個小的效用函數(shù),該函數(shù)生成 N 種顏色,均勻分布在顏色光譜中。

每當我們需要創(chuàng)建繪圖并為每個部分著色時,這將對我們有所幫助:

from borb.pdf.canvas.color.color import HSVColor, HexColor
from decimal import Decimal
import typing
?
def create_n_colors(n: int) -> typing.List[str]:
? # The base color is borb-blue
? base_hsv_color: HSVColor = HSVColor.from_rgb(HexColor("56cbf9"))
? # This array comprehension creates n HSVColor objects, transforms then to RGB, and then returns their hex string
? return [HSVColor(base_hsv_color.hue + Decimal(x / 360), Decimal(1), Decimal(1)).to_rgb().to_hex_string() for x in range(0, 360, int(360/n))]

HSL 和 HSV/HSB 是由計算機圖形學研究人員在 1970 年代設計的,目的是更接近人類視覺感知色彩屬性的方式。在這些模型中,每種色調的顏色都排列在一個徑向切片中,圍繞中性色的中心軸,范圍從底部的黑色到頂部的白色:

用它表示顏色的優(yōu)點是我們可以輕松地將顏色光譜分成相等的部分。

現(xiàn)在我們可以定義一個 create_pie_chart() 函數(shù)(或其他類型圖的函數(shù)):

# New import(s)
import matplotlib.pyplot as plt
from borb.pdf.canvas.layout.image.chart import Chart
from borb.pdf.canvas.layout.layout_element import Alignment
?
def create_piechart(labels: typing.List[str], data: typing.List[float]):
?
? # Symetric figure to ensure equal aspect ratio
? fig1, ax1 = plt.subplots(figsize=(4, 4))
? ax1.pie(
? ? data,
? ? explode=[0 for _ in range(0, len(labels))],
? ? labels=labels,
? ? autopct="%1.1f%%",
? ? shadow=True,
? ? startangle=90,
? ? colors=create_n_colors(len(labels)),
? )
?
? ax1.axis("equal") ?# Equal aspect ratio ensures that pie is drawn as a circle.
?
? return Chart(
? ? plt.gcf(),
? ? width=Decimal(200),
? ? height=Decimal(200),
? ? horizontal_alignment=Alignment.CENTERED,
? )

在這里,我們使用 Matplotlib 通過 pie() 函數(shù)創(chuàng)建餅圖。

PyPlot 實例的 gcf() 函數(shù)返回當前圖形。該圖可以嵌入到 PDF 文檔中,方法是將其注入到 Chart 構造函數(shù)中,并與您的自定義參數(shù)(例如width, height 和 horizontal_alignment)一起插入。

我們只需向Chart構造函數(shù)提供一個 Matplotlib 圖。

3.將 Matplotlib 圖表添加到 PDF 文檔

現(xiàn)在是時候創(chuàng)建我們的 PDF 文檔并向其中添加內容了。

# New import(s)
from borb.pdf.document import Document
from borb.pdf.page.page import Page
from borb.pdf.pdf import PDF
from borb.pdf.canvas.layout.page_layout.multi_column_layout import MultiColumnLayout
from borb.pdf.canvas.layout.page_layout.page_layout import PageLayout
from borb.pdf.canvas.layout.text.paragraph import Paragraph
?
# Create empty Document
pdf = Document()
?
# Create empty Page
page = Page()
?
# Add Page to Document
pdf.append_page(page)
?
# Create PageLayout
layout: PageLayout = MultiColumnLayout(page)
?
# Write title
layout.add(Paragraph("About Lorem Ipsum",?
? ? ? ? ? ? ? ? ? ? ?font_size=Decimal(20),?
? ? ? ? ? ? ? ? ? ? ?font="Helvetica-Bold"))

我們將在此 PDF 中使用連字符,以確保文本的布局更加流暢。borb 中的連字符非常簡單:

# New import(s)
from borb.pdf.canvas.layout.hyphenation.hyphenation import Hyphenation
?
# Create hyphenation algorithm
hyphenation_algorithm: Hyphenation = Hyphenation("en-gb")
?
# Write paragraph
layout.add(Paragraph(
? ? """
? ? Lorem Ipsum is simply dummy text of the printing and typesetting industry.?
? ? Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,?
? ? when an unknown printer took a galley of type and scrambled it to make a type specimen book.?
? ? It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.?
? ? It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,?
? ? and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
? ? """, text_alignment=Alignment.JUSTIFIED, hyphenation=hyphenation_algorithm))

現(xiàn)在我們可以使用我們之前聲明的函數(shù)添加餅圖;

# Write graph
layout.add(create_piechart(["Loren", "Ipsum", "Dolor"],?
? ? ? ? ? ? ? ? ? ? ? ? ? ?[0.6, 0.3, 0.1]))

接下來我們將編寫另外三個 Paragraph對象。其中一個將不僅僅表示引用(側面邊框,不同字體等)。

# Write paragraph
layout.add(Paragraph(
? ? """
? ? Contrary to popular belief, Lorem Ipsum is not simply random text.?
? ? It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.?
? ? Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words,?
? ? consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature,?
? ? discovered the undoubtable source.
? ? """, text_alignment=Alignment.JUSTIFIED, hyphenation=hyphenation_algorithm))
?
# Write paragraph
layout.add(Paragraph(
? ? """
? ? Lorem Ipsum is simply dummy text of the printing and typesetting industry.?
? ? """,?
? ? font="Courier-Bold",
? ? text_alignment=Alignment.JUSTIFIED,?
? ? hyphenation=hyphenation_algorithm,
? ? border_color=HexColor("56cbf9"),
? ? border_width=Decimal(3),
? ? border_left=True,
? ? padding_left=Decimal(5),
? ? padding_bottom=Decimal(5),
))
?
# Write paragraph
layout.add(Paragraph(
? ? """
? ? Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum"?
? ? (The Extremes of Good and Evil) by Cicero, written in 45 BC.?
? ? This book is a treatise on the theory of ethics, very popular during the Renaissance.
? ? """, text_alignment=Alignment.JUSTIFIED, hyphenation=hyphenation_algorithm))

讓我們添加另一個繪圖。

# Write graph
layout.add(create_piechart(["Loren", "Ipsum", "Dolor", "Sit", "Amet"],?
? ? ? ? ? ? ? ? ? ? ? ? ? ?[600, 30, 89, 100, 203]))

還有一段內容(Paragraph):

# Write paragraph
layout.add(Paragraph(
? ? """
? ? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.?
? ? The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here',?
? ? making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text,?
? ? and a search for 'lorem ipsum' will uncover many web sites still in their infancy.?
? ? Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
? ? """, text_alignment=Alignment.JUSTIFIED, hyphenation=hyphenation_algorithm))

最后,我們可以存儲文檔(Document):

# Write to disk
with open("output.pdf", "wb") as pdf_file_handle:
? PDF.dumps(pdf_file_handle, pdf)

運行此代碼會生成如下所示的 PDF 文檔:

到此這篇關于Python 如何將 matplotlib 圖表集成進到PDF 中的文章就介紹到這了,更多相關 matplotlib 圖表集成到 PDF 中內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • python下讀取公私鑰做加解密實例詳解

    python下讀取公私鑰做加解密實例詳解

    這篇文章主要介紹了python下讀取公私鑰做加解密實例詳解的相關資料,需要的朋友可以參考下
    2017-03-03
  • python機器學習實戰(zhàn)之最近鄰kNN分類器

    python機器學習實戰(zhàn)之最近鄰kNN分類器

    這篇文章主要介紹了python機器學習實戰(zhàn)之最近鄰kNN分類器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Tensorflow中的圖(tf.Graph)和會話(tf.Session)的實現(xiàn)

    Tensorflow中的圖(tf.Graph)和會話(tf.Session)的實現(xiàn)

    這篇文章主要介紹了Tensorflow中的圖(tf.Graph)和會話(tf.Session)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • python 含子圖的gif生成時內存溢出的方法

    python 含子圖的gif生成時內存溢出的方法

    今天小編就為大家分享一篇python 含子圖的gif生成時內存溢出的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python實現(xiàn)回旋矩陣方式(旋轉矩陣)

    python實現(xiàn)回旋矩陣方式(旋轉矩陣)

    今天小編就為大家分享一篇python實現(xiàn)回旋矩陣方式(旋轉矩陣),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python使用selenium實現(xiàn)網(wǎng)頁用戶名 密碼 驗證碼自動登錄功能

    Python使用selenium實現(xiàn)網(wǎng)頁用戶名 密碼 驗證碼自動登錄功能

    這篇文章主要介紹了Python使用selenium實現(xiàn)網(wǎng)頁用戶名 密碼 驗證碼自動登錄功能,實現(xiàn)思路很簡單,感興趣的朋友跟隨腳本之家小編一起學習吧
    2018-05-05
  • 妙用itchat! python實現(xiàn)久坐提醒功能

    妙用itchat! python實現(xiàn)久坐提醒功能

    python編寫的久坐提醒,給最愛的那個她,這篇文章主要為大家分享了python久坐提醒功能的實現(xiàn)代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Python 切分數(shù)組實例解析

    Python 切分數(shù)組實例解析

    這篇文章主要介紹了Python 切分數(shù)組實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • python_tkinter彈出對話框創(chuàng)建2

    python_tkinter彈出對話框創(chuàng)建2

    這篇文章主要介紹了python_tkinter彈出對話框創(chuàng)建,上以篇文章我們簡單的對對話框創(chuàng)建做了簡單介紹,本文將繼續(xù)更多相關內容,需要的小伙伴可以參考一下
    2022-03-03
  • Pygame實戰(zhàn)之檢測按鍵正確的小游戲

    Pygame實戰(zhàn)之檢測按鍵正確的小游戲

    這篇文章主要為大家介紹了利用Pygame模塊實現(xiàn)的檢測按鍵正確的小游戲:每個字母有10秒的按鍵時間,如果按對,則隨機產(chǎn)生新的字符,一共60s,如果時間到了,則游戲結束。快來跟隨小編一起學習一下吧
    2021-12-12

最新評論