简体   繁体   English

Matplotlib,如何在plot中得到一个plot?

[英]Matplotlib, how to get a plot inside a plot?

I want to create a subplot inside the first subplot named "logo".我想在第一个名为“logo”的子图中创建一个子图。 This time I generated my subplots with mosaic (ax_dict = fig.subplot_mosaic(layout)) How to generate a subplot inside the first subplot?这次我生成了带有马赛克的子图 (ax_dict = fig.subplot_mosaic(layout)) 如何在第一个子图中生成一个子图?

在此处输入图像描述

import numpy as np
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import ttk
from tkinter import *
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)


root = tk.Tk()
root.title("Transactions")
root.geometry("1920x1080")        
tabControl = ttk.Notebook(root) 
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tab3 = ttk.Frame(tabControl)
tabControl.add(tab1, text ='Home')
tabControl.add(tab2, text ='Scatter Plot')
tabControl.add(tab3, text ='Histogram')
#LABEL-FRAME
ontop = tk.Frame(root, background="#ccc5b9",height=33)
ontop.pack(fill=BOTH)
        
#BUTTON-FRAME  
top = tk.Frame(root, background="#e9ecef")                     
top.pack(fill=X)
tabControl.pack(expand = FALSE, fill ="both")
        
#TABLE-FRAME
bottom = tk.Frame(root, background="#adb5bd")        
bottom.pack(in_=tab1,fill=X) 

s = ttk.Style()
s.theme_use('default')
s.configure('TNotebook.Tab', background="green3")
s.map("TNotebook", background= [("selected", "green3")])       
     
layout = [["logo", "logo","logo"],["logo", "logo","logo"],["bar", "bar","bar"],["bar", "bar","bar"],["alpha","beta","edge"]]

fig = plt.figure(figsize = (5,9))

ax_dict = fig.subplot_mosaic(layout)

ax_dict["logo"].annotate(
    xy = (3.3, 95),
    text = "0-1000 €",
    size = 10
)


ax_dict["bar"].annotate(
    xy = (1.5,.5),
    text = "bar",
    ha = "left",
    va = "center",
    size = 10
)

ax_dict["edge"].annotate(
    xy = (.5,.5),
    text = "edge",
    ha = "left",
    va = "center",
    size = 10
) 
ax_dict["alpha"].annotate(
    xy = (.5,.5),
    text = "alpha",
    ha = "center",
    va = "center",
    size = 10
) 
ax_dict["beta"].annotate(
    xy = (.5,.5),
    text = "beta",
    ha = "center",
    va = "center",
    size = 10
) 



ax_dict["logo"].set_xlim([-10,1010])
ax_dict["bar"].set_xlim([900,100100])

ax_dict["alpha"].set_xlim([99000,1001000])#100T-1M
ax_dict["beta"].set_xlim([990000,10010000])#1M-10M
ax_dict["edge"].set_xlim([9900000,100100000])#10M-100M

ax_dict["logo"].set_xticks([-10,50,100,150,200,250,300,350,400,450,500,550,600,660,700,750,800,850,900,950,1010])
ax_dict["bar"].set_xticks([1000,5000, 10000,15000,20000,25000,30000,35000,40000,45000,50000,55000,60000,65000,70000,75000,80000,85000,90000,95000,100000])

ax_dict["alpha"].set_xticks([100000,200000,300000,400000,500000,600000,700000,800000,900000,1000000])
ax_dict["beta"].set_xticks([1000000,2000000,3000000,4000000,5000000,6000000,7000000,8000000,9000000,10000000])
ax_dict["edge"].set_xticks([10000000,20000000,30000000,40000000,50000000,60000000,70000000,80000000,90000000,100000000])

ax_dict["logo"].set_xticklabels(["0-10","50","100","150", "200","250","300","350","400","450","500","550","600","650","700","750","800","850","900","950","1000"],fontsize=6)
ax_dict["bar"].set_xticklabels(["1k","5k", "10k","15k","20k","25k","30k","35k","40k","45k","50k","55k","60k","65k","70k","75k","80k","85k","90k","95k","100k"],fontsize=8)
ax_dict["alpha"].set_xticklabels(["100k","200k","300k","400k","500k","600k","700k","800k","900k", "1M"],fontsize=8)
ax_dict["beta"].set_xticklabels(["1M","2M","3M","4M","5M","6M","7M","8M","9M","10M"],fontsize=8)
ax_dict["edge"].set_xticklabels(["10M","20M","30M","40M","50M","60M","70M","80M","90M","100M"],fontsize=8)


ax_dict["logo"].grid(visible = True, ls = ":", color = "gray")
ax_dict["edge"].grid(visible = True, ls = ":", color = "red")
ax_dict["bar"].grid(visible = True, ls = ":", color = "gray")
ax_dict["alpha"].grid(visible = True, ls = ":", color = "blue")
ax_dict["beta"].grid(visible = True, ls = ":", color = "green")

ax_dict["logo"].set_yticks([0,10,20,30,40,50,60,70,80,90,100])
ax_dict["bar"].set_yticks([0,10,20,30,40,50,60,70,80,90,100])
ax_dict["alpha"].set_yticks([0,1,2,3,4,5,6,7,8,9,10])
ax_dict["beta"].set_yticks([0,1,2,3,4,5,6,7,8,9,10])
ax_dict["edge"].set_yticks([0,1,2,3,4,5,6,7,8,9,10])

    
        
figure_canvas = FigureCanvasTkAgg(fig, tab2)
figure_canvas.draw()
figure_canvas.get_tk_widget().pack(expand=TRUE, fill=BOTH) 
fig.set_tight_layout(True)


root.mainloop()

What did you try and what were you expecting?你尝试了什么,你期待什么? I searched the web, but found nothing for this special case.我搜索了 web,但没有找到这种特殊情况。

from mpl_toolkits.axes_grid1.inset_locator import inset_axes
axins =inset_axes(ax_dict["logo"], width="70%", height=1.8, loc=1)

axins is then the axes inside the plot "logo", which was defined through axins 然后是 plot “徽标”内的轴,它是通过定义的

ax_dict = fig.subplot_mosaic(layout)

ax_dict["logo"].annotate(
    xy = (3.3, 95),
    text = "0-1000 €",
    size = 10
)

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

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