简体   繁体   中英

Add Text Panel in Matplotlib Figure

Is it possible to add some kind of panel next to a plot where only text can be written. Here is a picture describing what I want to do. Plot and panel

As in the picture, I also want to change the color, weight and style of the text.

I've tried using a matplotlib.widgets.Textbox but it didn't work. Also tried to add a subplot, set the axis off and write but don't know how to change the style of the different part of my Panel.

I want to use only matplolib for that if possible.

Thanks

Here is an example how you could implement this chart. More information about available text properties you can find here

import numpy as np
import matplotlib.pyplot as plt
def f(x):
    return (x**2)*exp(-(x**2))
def f3(x):
    return (x**4)*exp(-(x**2))


x = np.arange(0, 3, 0.05)
f2 = np.vectorize(f)
f4 = np.vectorize(f3)


fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12,7))
fig.suptitle('Horizontally stacked subplots', fontsize=20)
ax1.plot(x, f2(x), 'red', label='(t^2)*exp(-(t^2))')
ax1.plot(x, f4(x), 'darkblue', linestyle='dotted', linewidth='4', label='(t^4)*exp(-(t^2))')


ax1.set(title="2 lines chart", xlabel="t", ylabel="y")
ax1.legend(loc="upper right")


# Set both x- and y-axis limits to [0, 10] instead of default [0, 1]


ax2.axis([0, 10, 0, 10])
ax2.tick_params(axis='x', colors='white')
ax2.tick_params(axis='y', colors='white')

ax2.text(0.5, 9, 'Topic 1', weight='bold', fontsize=15,
         bbox={'facecolor': 'red', 'alpha': 0.3, 'pad': 10})
ax2.text(0.5, 7.5, '- Bullet pont 1\n- Bullet point 2', fontsize=12 )


ax2.text(0.5, 5.5, 'Topic 2', weight='bold', fontsize=15,)
ax2.text(0.5, 4.5, '- Bullet pont 1\n- Bullet point 2', fontsize=12 )

ax2.text(2, 3, r'a function to plot: $t^2*exp(-t^2)$', fontsize=12)

ax2.text(0.5, 2, "colored text", fontsize=12, color='darkblue')




plt.show()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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