Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Matplotlib创建相邻图形 如何使用matplotlib使第一个图像看起来像第二个图像?_Python_Python 3.x_Matplotlib - Fatal编程技术网

Python 使用Matplotlib创建相邻图形 如何使用matplotlib使第一个图像看起来像第二个图像?

Python 使用Matplotlib创建相邻图形 如何使用matplotlib使第一个图像看起来像第二个图像?,python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,蓝色图形中的每个“列”表示与相应的绿色图形“列”相反的内容。我认为这种格式提供了信息 编辑: 这段代码应该让你知道我在做什么 import tkinter as tk import numpy as np from matplotlib.figure import Figure from matplotlib.font_manager import FontProperties from matplotlib.backends.backend_tkagg import FigureCanv

蓝色图形中的每个“列”表示与相应的绿色图形“列”相反的内容。我认为这种格式提供了信息

编辑: 这段代码应该让你知道我在做什么

import tkinter as tk
import numpy as np

from matplotlib.figure import Figure
from matplotlib.font_manager import FontProperties
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

infoFrame = tk.Frame(tk.Tk(), width=1200, height=750, padx=5, pady=5)
infoFrame.grid()

graphCanvas = tk.Canvas(infoFrame)
graphCanvas.grid(columnspan=5, rowspan=2, row=1)

infoGraph = Figure(figsize=(7, 6), dpi=100)  
firstGraph = infoGraph.add_subplot(2, 1, 2, axisbg="#9DDEFF")
secondGraph = infoGraph.add_subplot(2, 1, 1, axisbg="#B2F0B2")

entries = ["one", "two"]
types = ["x", "y"]
_tkColors = ["black", "yellow", "magenta", "cyan", "red", "green", "blue"]
index = np.arange(len(types))
width = 0.3
firstLabelData = []
secondLabelData = []
iterator = 0
barData = {'interval': 1, 'data': 
            {'one': {'std': [0.0, 0.0], 'sum': [5, 4], 'mean': [5.0, 4.0]}, 
            'two': {'std': [0.0, 0.0], 'sum': [14, 2], 'mean': [14.0, 2.0]}}}

for entry in entries:
    firstPlot = firstGraph.bar(index+(width*iterator), barData["data"][entry]["sum"], width,
                         color=_tkColors[iterator % len(_tkColors)], yerr=barData["data"][entry]["std"])
    secondPlot = secondGraph.bar(index+(width*iterator), barData["data"][entry]["sum"], width,
                         color=_tkColors[iterator % len(_tkColors)], yerr=barData["data"][entry]["std"])

    firstLabelData.append(firstPlot[0])
    secondLabelData.append(secondPlot[0])

    iterator += 1


firstGraph.text(3.6, 18, "Inverse Graph 1", weight="bold")
firstGraph.set_xlabel("Over " + str(30) + " Iterations")
firstGraph.invert_yaxis()
secondGraph.text(3.5, 18, "Graph 1", weight="bold")

fontP = FontProperties()
fontP.set_size("small")

secondGraph.legend(tuple(firstLabelData), tuple(entries), prop=fontP, loc=2)

graph = FigureCanvasTkAgg(infoGraph, master=graphCanvas)
graph.show()
graph._tkcanvas.pack(side=tk.TOP, expand=1)

infoFrame.mainloop()
比如:

fig, ax = plt.subplots()


ax.set_ylim([-5, 5])
ax.axhspan(0, 5, color='b', alpha=.5, zorder=-5)
ax.axhspan(-5, 0, color='r', alpha=.5, zorder=-5)
for j, c in enumerate(['k', 'y', 'm']):
    t = np.random.rand(10)
    b = -np.random.rand(10)
    h = -b + t
    ax.bar(.3*j + np.arange(10), h, bottom=b,  color=c, width=.3)
这有点脆弱,因为彩色背景在垂直方向上是有限的。我怀疑有更好的方法来制作半无限面片,但我想不出一个好办法


如果你真的想用两个独立的轴来做这件事,像这样的东西可能会有用:

fig = plt.figure()
top_axes = fig.add_axes([.1, .5, .8, .4], axisbg="#9DDEFF")
bottom_axes = fig.add_axes([.1, .1, .8, .4], sharex=top_axes, axisbg="#B2F0B2")
bottom_axes.invert_yaxis()
top_axes.xaxis.set_visible(False)

for j, c in enumerate(['k', 'y', 'm']):
    b = np.random.rand(10)
    t = np.random.rand(10)
    top_axes.bar(.3*j + np.arange(10), t,  color=c, width=.3)
    bottom_axes.bar(.3*j + np.arange(10), b,  color=c, width=.3)
yaxis上的0标签存在一些愚蠢之处(因为它是双重绘制的),但这应该不太难修复(可能需要一个奇特的格式化程序)


蓝色图形中的每个“列”都表示类似于相应绿色图形“列”的倒数。我认为这种格式很有用。你应该包含一些你尝试过的最小的可运行代码(包括一些合成数据)。我会使用
axvspan
来获得颜色,然后用
top
bottom
kwargs调用
bar
。~您没有发布相关代码。~nm,因此位以不同的速率刷新。现在,由于没有人会阅读那么多代码,所以它已经走得太远了。(我们就像这里的金发姑娘一样。)很好。我会弄明白的!顶部的数据与底部的数据完全分开(两者都是正值)。这就好像前一天收集了顶部,第二天收集了底部。考虑到这一点,你有什么建议?啊,我错过了缺失的负面信号。您可以使用格式化程序来显示刻度的绝对值。