python - 如何堆叠多个图
问题描述
我有一个函数,它接受一个字典(键:字符串,值:二维数字)并在图表中显示字典的值(每个键一种颜色)。
我想直接用这个函数在同一个页面显示四个图,每个图对应一个特定的字典
如果可能的话,我想要一个像这样的脚本:
def my_function(dico):
display picture
def global_function(multiple):
plt.subplot(1,2,1)
my_function(multiple[0])
plt.subplot(1,2,2)
my_function(multiple[1])
解决方案
您可以将这些图绘制为一个图的子图。
import numpy as np
import matplotlib.pyplot as plt
#plotting function
def plot_2_subplots (x1, y1, x2, y2):
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x1, y1)
ax1.set_title('Plot 1')
ax2.plot(x2, y2)
ax2.set_title('Plot 2')
lists_a = sorted(d.items()) # return a list of tuples (sorted by key)
x1, y1 = zip(*lists_a) # unpack into tuple
lists_b = sorted(d.items()) # return a list of tuples (sorted by key)
x2, y2 = zip(*lists_b) # unpack into tuple
#Call plotting function
plot_2_subplots(x1, y1, x2, y2)
推荐阅读
- json - 远程 API 调用的单元测试,它如何与 JSON 对象一起工作
- nlp - 有没有办法确定客户评论是否专门针对特定主题?
- angular - 无法将图像的路径绑定到模板 - Angular
- c# - 在 Microsoft Graph 中区分 SharedMailboxes 和常规邮箱
- php - 覆盖供应商类 Hyn\Tenancy\Database\Connection Laravel 上的函数
- asp.net-core - 使用身份更改 .NET Core 2.2 中的登录/注销页面
- firebase - Firebase 无法从控制台发送密码重置电子邮件
- c - +'0'在C中是什么意思
- c# - ICollectionView 向 ObservableCollection 添加过滤器
- java - MySQL 5.7 与 MySQL 8 中的 UTF-8 Unicode 字符