草庐IT

python - Matplotlib 表格格式化

coder 2023-05-24 原文

似乎无法在文档中找到如何增加单元格的行高,因为文本本身非常狭窄。

感谢任何有关代码的帮助!表格格式似乎没有很好的记录...

    # Plot line width
    matplotlib.rc('lines', linewidth=3)

    ind = np.arange(len(overall))

    fig = pyplot.figure()
    ax = fig.add_subplot(211)
    ax.set_title('Overall Rating of Experience')
    ax.set_ylabel('Score (0-100)')

    # Plot data on chart
    plot1 = ax.plot(ind, overall)
    plot2 = ax.plot(ind, svc_avg)
    plot3 = ax.plot(ind, benchmark)

    ax.yaxis.grid(True, which='major', ls='-', color='#9F9F9F')
    ax.set_ylim([min(overall + svc_avg + benchmark) - 3, 100])
    ax.set_xlim([-.5,1.5])
    ax.get_xaxis().set_ticks([])
    ax.set_position([.25, .3, 0.7, 0.5])

    colLabels = ['July', 'August']
    rowLabels = ['Average', 'Service Average', 'Benchmark']
    cellText = [overall, svc_avg, benchmark]
    the_table = ax.table(cellText=cellText, rowLoc='right',
                         rowColours=colors, rowLabels=rowLabels,
                         colWidths=[.5,.5], colLabels=colLabels,
                         colLoc='center', loc='bottom')

编辑:感谢 Oz 的回答——循环遍历表格的属性可以轻松修改 height 属性:

    table_props = the_table.properties()
    table_cells = table_props['child_artists']
    for cell in table_cells: cell.set_height(0.1)

最佳答案

matplotlib 文档说

Add a table to the current axes. Returns a matplotlib.table.Table instance. For finer grained control over tables, use the Table class and add it to the axes with add_table().

您可以执行以下操作,查看表的属性(它和属于该类表的对象):

print  the_table.properties() # hint it's a dictionary do: type(the_table.properties() <type 'dict'> 

以您认为正确的方式编辑该字典,并更新您的表格,使用:

the_table.update(giveHereYourDictionary)

提示:如果你使用 IPython 或交互式 shell,那么帮助(objectName)就足够了,例如help(the_table) 查看所有对象的方法。 希望这应该会奏效。

好的,我在这里添加了如何处理这类内容的演练。我承认,这不是微不足道的,但我使用 matplotlib 已经 3.5 年了,所以...

在 IPython 中编写代码(我之前说过,但我必须再次强调),它确实有助于检查对象具有的所有属性(键入对象名称,然后键入键):

In [95]: prop=the_table.properties()
In [96]: prop #This is a dictionary, it's not so trivial, but never the less one can understand how dictionaries work...
Out[96]: 
{'agg_filter': None,
 'alpha': None,
 'animated': False,
 'axes': <matplotlib.axes.AxesSubplot at 0x9eba34c>,
 'celld': {(0, -1): <matplotlib.table.Cell at 0xa0cf5ec>,
  (0, 0): <matplotlib.table.Cell at 0xa0c2d0c>,
  (0, 1): <matplotlib.table.Cell at 0xa0c2dec>,
  (0, 2): <matplotlib.table.Cell at 0xa0c2ecc>,
  (1, -1): <matplotlib.table.Cell at 0xa0cf72c>,
  (1, 0): <matplotlib.table.Cell at 0xa0c2fac>,
  (1, 1): <matplotlib.table.Cell at 0xa0cf08c>,
  (1, 2): <matplotlib.table.Cell at 0xa0cf18c>,
  (2, -1): <matplotlib.table.Cell at 0xa0cf84c>,
  (2, 0): <matplotlib.table.Cell at 0xa0cf28c>,
  (2, 1): <matplotlib.table.Cell at 0xa0cf3ac>,
  (2, 2): <matplotlib.table.Cell at 0xa0cf4cc>},
 'child_artists': [<matplotlib.table.Cell at 0xa0c2dec>,
  <matplotlib.table.Cell at 0xa0cf18c>,
  <matplotlib.table.Cell at 0xa0c2d0c>,
  <matplotlib.table.Cell at 0xa0cf84c>,
  <matplotlib.table.Cell at 0xa0cf3ac>,
  <matplotlib.table.Cell at 0xa0cf08c>,
  <matplotlib.table.Cell at 0xa0cf28c>,
  <matplotlib.table.Cell at 0xa0cf4cc>,
  <matplotlib.table.Cell at 0xa0cf5ec>,
  <matplotlib.table.Cell at 0xa0c2fac>,
  <matplotlib.table.Cell at 0xa0cf72c>,
  <matplotlib.table.Cell at 0xa0c2ecc>],
 'children': [<matplotlib.table.Cell at 0xa0c2dec>,
  <matplotlib.table.Cell at 0xa0cf18c>,
  ...snip snap ...
  <matplotlib.table.Cell at 0xa0cf72c>,
  <matplotlib.table.Cell at 0xa0c2ecc>],
 'clip_box': TransformedBbox(Bbox(array([[ 0.,  0.],
       [ 1.,  1.]])), CompositeAffine2D(BboxTransformTo(Bbox(array([[ 0.,  0.],
       [ 1.,  1.]]))), BboxTransformTo(TransformedBbox(Bbox(array([[ 0.25,  0.3 ],
       [ 0.95,  0.8 ]])), BboxTransformTo(TransformedBbox(Bbox(array([[ 0.,  0.],
       [ 8.,  6.]])), Affine2D(array([[ 80.,   0.,   0.],
       [  0.,  80.,   0.],
       [  0.,   0.,   1.]])))))))),
 'clip_on': True,
 'clip_path': None,
 'contains': None,
 'figure': <matplotlib.figure.Figure at 0x9eaf56c>,
 'gid': None,
 'label': '',
 'picker': None,
 'rasterized': None,
 'snap': None,
 'transform': BboxTransformTo(TransformedBbox(Bbox(array([[ 0.25,  0.3 ],
       [ 0.95,  0.8 ]])), BboxTransformTo(TransformedBbox(Bbox(array([[ 0.,  0.],
       [ 8.,  6.]])), Affine2D(array([[ 80.,   0.,   0.],
       [  0.,  80.,   0.],
       [  0.,   0.,   1.]])))))),
 'transformed_clip_path_and_affine': (None, None),
 'url': None,
 'visible': True,
 'zorder': 0}

# we now get all the cells ...
 [97]: cells = prop['child_artists']

In [98]: cells
Out[98]: 
[<matplotlib.table.Cell at 0xa0c2dec>,
 <matplotlib.table.Cell at 0xa0cf18c>,
... snip snap...
 <matplotlib.table.Cell at 0xa0cf72c>,
 <matplotlib.table.Cell at 0xa0c2ecc>]

In [99]:cell=cells[0]
In [100]: cell # press tab here to see cell's attributes

Display all 122 possibilities? (y or n)
cell.PAD
cell.add_callback
...snip snap ...
cell.draw
cell.eventson
cell.figure
...snip snap ...
In [100]: cell.set_h
cell.set_hatch   cell.set_height 

# this looks promising no? Hell, I love python ;-)
wait, let's examine something first ...
In [100]: cell.get_height()
Out[100]: 0.055555555555555552
In [101]: cell.set_height(0.1) # we just 'doubled' the height...
In [103]: pyplot.show()

和 TA DA:

现在,我要求您使用 for 循环更改所有单元格的高度。 应该不会那么难。 赢得那笔赏金会很高兴;-)

关于python - Matplotlib 表格格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9932072/

有关python - Matplotlib 表格格式化的更多相关文章

  1. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  2. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  3. ruby-on-rails - 将 Ruby 中的日期/时间格式化为 YYYY-MM-DD HH :MM:SS - 2

    这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build

  4. ruby - 我可以将我的 README.textile 以正确的格式放入我的 RDoc 中吗? - 2

    我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:

  5. ruby - 是否有用于序列化和反序列化各种格式的对象层次结构的模式? - 2

    给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最

  6. Python 相当于 Perl/Ruby ||= - 2

    这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Pythonconditionalassignmentoperator对于这样一个简单的问题表示歉意,但是谷歌搜索||=并不是很有帮助;)Python中是否有与Ruby和Perl中的||=语句等效的语句?例如:foo="hey"foo||="what"#assignfooifit'sundefined#fooisstill"hey"bar||="yeah"#baris"yeah"另外,类似这样的东西的通用术语是什么?条件分配是我的第一个猜测,但Wikipediapage跟我想的不太一样。

  7. ruby-on-rails - Prawn - 表格单元格内的链接 - 2

    我正在尝试用Prawn生成PDF。在我的PDF模板中,我有带单元格的表格。在其中一个单元格中,我有一个电子邮件地址:cell_email=pdf.make_cell(:content=>booking.user_email,:border_width=>0)我想让电子邮件链接到“mailto”链接。我知道我可以这样链接:pdf.formatted_text([{:text=>booking.user_email,:link=>"mailto:#{booking.user_email}"}])但是将这两行组合起来(将格式化文本作为内容)不起作用:cell_email=pdf.make_c

  8. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

    什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

  9. ruby-on-rails - 事件管理员日期过滤器日期格式自定义 - 2

    是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s

  10. 华为OD机试用Python实现 -【明明的随机数】 2023Q1A - 2

    华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o

随机推荐