简体   繁体   English

使用 matplotlib 将语法样式文本添加到 Python 中的 plot

[英]Adding a syntax style text to a plot in Python using matplotlib

I'm wanting to incorporate a text box with code style highlight in a plot in matplotlib, and I'm wondering whether there's a method to do so?我想在 matplotlib 的 plot 中合并一个带有代码样式突出显示的文本框,我想知道是否有这样做的方法? Thank you in advance for your consideration and suggestions.提前感谢您的考虑和建议。

Yes, I can try like this:是的,我可以这样尝试:

import matplotlib.pyplot as plt

#create data
x = [3, 6, 8, 12, 14]
y = [4, 9, 14, 12, 9]

#create scatterplot
plt.scatter(x, y)

font = {'family': 'serif',
        'color':  'red',
        'weight': 'bold',
        'size': 10
        }

box = {'facecolor': 'none',
       'edgecolor': 'green',
       'boxstyle': 'round'
      }

#add text with custom font

code = """
import numpy as np 
import pandas as pd

"""
#add text with custom font
plt.text(6, 9.5, code, fontdict=font, bbox=box)

Output look like: Output 看起来像:

输出

Reference: https://www.statology.org/matplotlib-add-text/参考: https://www.statology.org/matplotlib-add-text/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM