pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      <tfoot id='yjd5a'></tfoot>

      <legend id='yjd5a'><style id='yjd5a'><dir id='yjd5a'><q id='yjd5a'></q></dir></style></legend>

      <small id='yjd5a'></small><noframes id='yjd5a'>

        <bdo id='yjd5a'></bdo><ul id='yjd5a'></ul>
      <i id='yjd5a'><tr id='yjd5a'><dt id='yjd5a'><q id='yjd5a'><span id='yjd5a'><b id='yjd5a'><form id='yjd5a'><ins id='yjd5a'></ins><ul id='yjd5a'></ul><sub id='yjd5a'></sub></form><legend id='yjd5a'></legend><bdo id='yjd5a'><pre id='yjd5a'><center id='yjd5a'></center></pre></bdo></b><th id='yjd5a'></th></span></q></dt></tr></i><div class="3nmdf3j" id='yjd5a'><tfoot id='yjd5a'></tfoot><dl id='yjd5a'><fieldset id='yjd5a'></fieldset></dl></div>

        matplotlib中換行的文本框?

        Text box with line wrapping in matplotlib?(matplotlib中換行的文本框?)
        • <bdo id='gJVFf'></bdo><ul id='gJVFf'></ul>

                <tbody id='gJVFf'></tbody>

              <small id='gJVFf'></small><noframes id='gJVFf'>

              <i id='gJVFf'><tr id='gJVFf'><dt id='gJVFf'><q id='gJVFf'><span id='gJVFf'><b id='gJVFf'><form id='gJVFf'><ins id='gJVFf'></ins><ul id='gJVFf'></ul><sub id='gJVFf'></sub></form><legend id='gJVFf'></legend><bdo id='gJVFf'><pre id='gJVFf'><center id='gJVFf'></center></pre></bdo></b><th id='gJVFf'></th></span></q></dt></tr></i><div class="lv8izde" id='gJVFf'><tfoot id='gJVFf'></tfoot><dl id='gJVFf'><fieldset id='gJVFf'></fieldset></dl></div>
                <legend id='gJVFf'><style id='gJVFf'><dir id='gJVFf'><q id='gJVFf'></q></dir></style></legend>

                1. <tfoot id='gJVFf'></tfoot>
                  本文介紹了matplotlib中換行的文本框?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  是否可以通過 Matplotlib 在框中顯示文本,自動換行?通過使用 pyplot.text(),我只能打印超出窗口邊界的多行文本,這很煩人.事先不知道線條的大小......任何想法將不勝感激!

                  Is it possible to display text in a box through Matplotlib, with automatic line breaks? By using pyplot.text(), I was only able to print multi-line text that flows beyond the boundaries of the window, which is annoying. The size of the lines is not known in advance… Any idea would be much appreciated!

                  推薦答案

                  這個答案的內容被合并到了https://github.com/matplotlib/matplotlib/pull/4342 并將在下一個功能版本中.

                  The contents of this answer were merged into mpl master in https://github.com/matplotlib/matplotlib/pull/4342 and will be in the next feature release.

                  哇...這是一個棘手的問題...(并且它暴露了matplotlib的文本渲染中的很多限制...)

                  Wow... This is a thorny problem... (And it exposes a lot of limitations in matplotlib's text rendering...)

                  這應該(i.m.o.)是 matplotlib 內置的東西,但事實并非如此.有一些 關于它的線程郵件列表,但我找不到自動換行的解決方案.

                  This should (i.m.o.) be something that matplotlib has built-in, but it doesn't. There have been a few threads about it on the mailing list, but no solution that I could find to automatic text wrapping.

                  因此,首先,在 matplotlib 中繪制之前,無法確定渲染文本字符串的大小(以像素為單位).這不是太大的問題,因為我們可以繪制它,獲取大小,然后重新繪制包裝的文本.(雖然很貴,但也不算太差)

                  So, first off, there's no way to determine the size (in pixels) of the rendered text string before it's drawn in matplotlib. This isn't too large of a problem, as we can just draw it, get the size, and then redraw the wrapped text. (It's expensive, but not too excessively bad)

                  下一個問題是字符沒有以像素為單位的固定寬度,因此將文本字符串包裝到給定數量的字符不一定會在渲染時反映給定的寬度.不過,這不是一個大問題.

                  The next problem is that characters don't have a fixed width in pixels, so wrapping a text string to a given number of characters won't necessarily reflect a given width when rendered. This isn't a huge problem, though.

                  除此之外,我們不能只做一次......否則,它會在第一次繪制時正確包裹(例如在屏幕上),但如果再次繪制(當圖形調整大小或保存為與屏幕具有不同 DPI 的圖像).這不是一個大問題,因為我們可以將回調函數連接到 matplotlib 繪制事件.

                  Beyond that, we can't just do this once... Otherwise, it will be wrapped correctly when drawn the first time (on the screen, for example), but not if drawn again (when the figure is resized or saved as an image with a different DPI than the screen). This isn't a huge problem, as we can just connect a callback function to the matplotlib draw event.

                  無論如何,這個解決方案是不完美的,但它應該適用于大多數情況.我不會嘗試考慮 tex 渲染的字符串、任何拉伸字體或具有不尋常縱橫比的字體.但是,它現在應該可以正確處理旋轉的文本.

                  At any rate this solution is imperfect, but it should work in most situations. I don't try to account for tex-rendered strings, any stretched fonts, or fonts with an unusual aspect ratio. However, it should now properly handle rotated text.

                  但是,它應該嘗試自動將任何文本對象包裝在多個子圖中,無論您將 on_draw 回調連接到哪個圖形...它在許多情況下都是不完美的,但它做得不錯.

                  However, It should attempt automatically wrap any text objects in multiple subplots in whichever figures you connect the on_draw callback to... It will be imperfect in many cases, but it does a decent job.

                  import matplotlib.pyplot as plt
                  
                  def main():
                      fig = plt.figure()
                      plt.axis([0, 10, 0, 10])
                  
                      t = "This is a really long string that I'd rather have wrapped so that it"
                      " doesn't go outside of the figure, but if it's long enough it will go"
                      " off the top or bottom!"
                      plt.text(4, 1, t, ha='left', rotation=15)
                      plt.text(5, 3.5, t, ha='right', rotation=-15)
                      plt.text(5, 10, t, fontsize=18, ha='center', va='top')
                      plt.text(3, 0, t, family='serif', style='italic', ha='right')
                      plt.title("This is a really long title that I want to have wrapped so it"
                               " does not go outside the figure boundaries", ha='center')
                  
                      # Now make the text auto-wrap...
                      fig.canvas.mpl_connect('draw_event', on_draw)
                      plt.show()
                  
                  def on_draw(event):
                      """Auto-wraps all text objects in a figure at draw-time"""
                      import matplotlib as mpl
                      fig = event.canvas.figure
                  
                      # Cycle through all artists in all the axes in the figure
                      for ax in fig.axes:
                          for artist in ax.get_children():
                              # If it's a text artist, wrap it...
                              if isinstance(artist, mpl.text.Text):
                                  autowrap_text(artist, event.renderer)
                  
                      # Temporarily disconnect any callbacks to the draw event...
                      # (To avoid recursion)
                      func_handles = fig.canvas.callbacks.callbacks[event.name]
                      fig.canvas.callbacks.callbacks[event.name] = {}
                      # Re-draw the figure..
                      fig.canvas.draw()
                      # Reset the draw event callbacks
                      fig.canvas.callbacks.callbacks[event.name] = func_handles
                  
                  def autowrap_text(textobj, renderer):
                      """Wraps the given matplotlib text object so that it exceed the boundaries
                      of the axis it is plotted in."""
                      import textwrap
                      # Get the starting position of the text in pixels...
                      x0, y0 = textobj.get_transform().transform(textobj.get_position())
                      # Get the extents of the current axis in pixels...
                      clip = textobj.get_axes().get_window_extent()
                      # Set the text to rotate about the left edge (doesn't make sense otherwise)
                      textobj.set_rotation_mode('anchor')
                  
                      # Get the amount of space in the direction of rotation to the left and 
                      # right of x0, y0 (left and right are relative to the rotation, as well)
                      rotation = textobj.get_rotation()
                      right_space = min_dist_inside((x0, y0), rotation, clip)
                      left_space = min_dist_inside((x0, y0), rotation - 180, clip)
                  
                      # Use either the left or right distance depending on the horiz alignment.
                      alignment = textobj.get_horizontalalignment()
                      if alignment is 'left':
                          new_width = right_space 
                      elif alignment is 'right':
                          new_width = left_space
                      else:
                          new_width = 2 * min(left_space, right_space)
                  
                      # Estimate the width of the new size in characters...
                      aspect_ratio = 0.5 # This varies with the font!! 
                      fontsize = textobj.get_size()
                      pixels_per_char = aspect_ratio * renderer.points_to_pixels(fontsize)
                  
                      # If wrap_width is < 1, just make it 1 character
                      wrap_width = max(1, new_width // pixels_per_char)
                      try:
                          wrapped_text = textwrap.fill(textobj.get_text(), wrap_width)
                      except TypeError:
                          # This appears to be a single word
                          wrapped_text = textobj.get_text()
                      textobj.set_text(wrapped_text)
                  
                  def min_dist_inside(point, rotation, box):
                      """Gets the space in a given direction from "point" to the boundaries of
                      "box" (where box is an object with x0, y0, x1, & y1 attributes, point is a
                      tuple of x,y, and rotation is the angle in degrees)"""
                      from math import sin, cos, radians
                      x0, y0 = point
                      rotation = radians(rotation)
                      distances = []
                      threshold = 0.0001 
                      if cos(rotation) > threshold: 
                          # Intersects the right axis
                          distances.append((box.x1 - x0) / cos(rotation))
                      if cos(rotation) < -threshold: 
                          # Intersects the left axis
                          distances.append((box.x0 - x0) / cos(rotation))
                      if sin(rotation) > threshold: 
                          # Intersects the top axis
                          distances.append((box.y1 - y0) / sin(rotation))
                      if sin(rotation) < -threshold: 
                          # Intersects the bottom axis
                          distances.append((box.y0 - y0) / sin(rotation))
                      return min(distances)
                  
                  if __name__ == '__main__':
                      main()
                  

                  這篇關于matplotlib中換行的文本框?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  python: Two modules and classes with the same name under different packages(python:不同包下同名的兩個模塊和類)
                  Configuring Python to use additional locations for site-packages(配置 Python 以使用站點包的其他位置)
                  How to structure python packages without repeating top level name for import(如何在不重復導入頂級名稱的情況下構造python包)
                  Install python packages on OpenShift(在 OpenShift 上安裝 python 包)
                  How to refresh sys.path?(如何刷新 sys.path?)
                  Distribute a Python package with a compiled dynamic shared library(分發帶有已編譯動態共享庫的 Python 包)

                        • <tfoot id='eZLNX'></tfoot>

                          <small id='eZLNX'></small><noframes id='eZLNX'>

                        • <legend id='eZLNX'><style id='eZLNX'><dir id='eZLNX'><q id='eZLNX'></q></dir></style></legend>

                          • <bdo id='eZLNX'></bdo><ul id='eZLNX'></ul>
                            <i id='eZLNX'><tr id='eZLNX'><dt id='eZLNX'><q id='eZLNX'><span id='eZLNX'><b id='eZLNX'><form id='eZLNX'><ins id='eZLNX'></ins><ul id='eZLNX'></ul><sub id='eZLNX'></sub></form><legend id='eZLNX'></legend><bdo id='eZLNX'><pre id='eZLNX'><center id='eZLNX'></center></pre></bdo></b><th id='eZLNX'></th></span></q></dt></tr></i><div class="nctsfyt" id='eZLNX'><tfoot id='eZLNX'></tfoot><dl id='eZLNX'><fieldset id='eZLNX'></fieldset></dl></div>
                              <tbody id='eZLNX'></tbody>
                            主站蜘蛛池模板: 中式装修设计_全屋定制家具_实木仿古门窗花格厂家-喜迎门 | 不锈钢管件(不锈钢弯头,不锈钢三通,不锈钢大小头),不锈钢法兰「厂家」-浙江志通管阀 | 乐之康护 - 专业护工服务平台,提供医院陪护-居家照护-居家康复 | 飞象网 - 通信人每天必上的网站 全球化工设备网—化工设备,化工机械,制药设备,环保设备的专业网络市场。 | 吲哚菁绿衍生物-酶底物法大肠菌群检测试剂-北京和信同通科技发展有限公司 | 尾轮组_头轮组_矿用刮板_厢式刮板机_铸石刮板机厂家-双驰机械 | 安德建奇火花机-阿奇夏米尔慢走丝|高维|发那科-北京杰森柏汇 | 济南展厅设计施工_数字化展厅策划设计施工公司_山东锐尚文化传播有限公司 | 半自动预灌装机,卡式瓶灌装机,注射器灌装机,给药器灌装机,大输液灌装机,西林瓶灌装机-长沙一星制药机械有限公司 | 恒压供水控制柜|无负压|一体化泵站控制柜|PLC远程调试|MCGS触摸屏|自动控制方案-联致自控设备 | 不锈钢反应釜,不锈钢反应釜厂家-价格-威海鑫泰化工机械有限公司 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 成都办公室装修-办公室设计-写字楼装修设计-厂房装修-四川和信建筑装饰工程有限公司 | 家德利门业,家居安全门,别墅大门 - 安徽家德利门业有限公司 | 硬度计,金相磨抛机_厂家-莱州华煜众信试验仪器有限公司 | 天助网 - 中小企业全网推广平台_生态整合营销知名服务商_天助网采购优选 | 杭州厂房降温,车间降温设备,车间通风降温,厂房降温方案,杭州嘉友实业爽风品牌 | 台湾Apex减速机_APEX行星减速机_台湾精锐减速机厂家代理【现货】-杭州摩森机电 | 济南品牌设计-济南品牌策划-即合品牌策划设计-山东即合官网 | 盐水蒸发器,水洗盐设备,冷凝结晶切片机,转鼓切片机,絮凝剂加药系统-无锡瑞司恩机械有限公司 | 齿轮减速马达一体式_蜗轮蜗杆减速机配电机-德国BOSERL齿轮减速电动机生产厂家 | 浙江红酒库-冰雕库-气调库-茶叶库安装-医药疫苗冷库-食品物流恒温恒湿车间-杭州领顺实业有限公司 | 网站优化公司_北京网站优化_抖音短视频代运营_抖音关键词seo优化排名-通则达网络 | 洛阳永磁工业大吊扇研发生产-工厂通风降温解决方案提供商-中实洛阳环境科技有限公司 | 品牌广告服务平台,好排名,好流量,好生意。 | 周口风机|周风风机|河南省周口通用风机厂 | 铝板冲孔网,不锈钢冲孔网,圆孔冲孔网板,鳄鱼嘴-鱼眼防滑板,盾构走道板-江拓数控冲孔网厂-河北江拓丝网有限公司 | 钢制拖链生产厂家-全封闭钢制拖链-能源钢铝拖链-工程塑料拖链-河北汉洋机械制造有限公司 | 悬浮拼装地板_篮球场木地板翻新_运动木地板价格-上海越禾运动地板厂家 | 标准品网_标准品信息网_【中检计量】| 艺术漆十大品牌_艺术涂料加盟代理_蒙太奇艺术涂料厂家品牌|艺术漆|微水泥|硅藻泥|乳胶漆 | YT保温材料_YT无机保温砂浆_外墙保温材料_南阳银通节能建材高新技术开发有限公司 | 卫生纸复卷机|抽纸机|卫生纸加工设备|做卫生纸机器|小型卫生纸加工需要什么设备|卫生纸机器设备多少钱一台|许昌恒源纸品机械有限公司 | 自动钻孔机-全自动数控钻孔机生产厂家-多米(广东)智能装备有限公司 | 掺铥光纤放大器-C/L波段光纤放大器-小信号光纤放大器-合肥脉锐光电技术有限公司 | 污水提升器,污水提升泵,地下室排水,增压泵,雨水泵,智能供排水控制器-上海智流泵业有限公司 | 楼承板-开闭口楼承板-无锡海逵楼承板 | 影像测量仪_三坐标测量机_一键式二次元_全自动影像测量仪-广东妙机精密科技股份有限公司 | 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 西安中国国际旅行社(西安国旅) | 驾驶人在线_专业学车门户网站 | 密集架-手摇-智能-移动-价格_内蒙古档案密集架生产厂家 |