如何在另一个python图表中添加不同的图形(作为插图) [英] How to add different graphs (as an inset) in another python graph
问题描述
我想制作一个这样的图表:
问题是,我从一些外部文件获取数据,并且可以创建背景图,但我有不知道如何在已有的图表中添加另一个图表,并更改数据以在两者中获得不同的结果:
下面我将代码I我正在使用做背景图。
希望有人可以提供帮助。
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
plt.rc('text',usetex = True)
font = {'family':'serif','size':16}
plt.rc('font',** font)
plt.rc('legend',** {'fontsize':14})
matplotlib.rcParams ['text.latex .preamble'] = [r'\usepackage {amsmath}']
data = np.loadtxt(r'C:\ ... \file.txt')
plt.plot(data [:,0],data [:,6],linewidth = 3,label ='B $ _0 $ = 1.5 T d',linestyle =' - ',color ='black')
plt.show()
有很多方法可以做到这一点,这取决于你想插入的关系。
如果你只想插入一个没有固定关系的图形更大的图表,只需执行以下操作:
import matplotlib.pyplot as plt
fig,ax1 = plt.su bplots()
#这些数字的单位是百分比。 (0,0是左下角)
left,bottom,width,height = [0.25,0.6,0.2,0.2]
ax2 = fig.add_axes([left,bottom,width,height])
ax1.plot(范围(10),color ='red')
ax2.plot(范围(6)[:: - 1],color ='green')
plt.show()
如果你想在两者之间有某种关系,看一看在这里的一些例子: http://matplotlib.org/ 1.3.1 / mpl_toolkits / axes_grid / users / overview.html#insetlocator
如果您希望插入内容为放大版本,(比如说,正好是原来的两倍),它会在交互式地平移/缩放时自动更新。
然而,对于简单的插页,只需创建一个新的轴正如我在上面的示例中所示。
I'd like to make a graph like that:
the problem is, I've got the data from some external files, and I can make the background graph, but I have no idea how to add another graph inside of the one that I already have and change the data to have different results in both of them:
Below I am adding the code I am using to do the background graph. Hope someone can help.
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
plt.rc('text',usetex=True)
font = {'family':'serif','size':16}
plt.rc('font',**font)
plt.rc('legend',**{'fontsize':14})
matplotlib.rcParams['text.latex.preamble']=[r'\usepackage{amsmath}']
data=np.loadtxt(r'C:\...\file.txt')
plt.plot(data[:,0],data[:,6],linewidth = 3,label='B$_0$ = 1.5 T d',linestyle= '--', color='black')
plt.show()
There's more than one way do to this, depending on the relationship that you want the inset to have.
If you just want to inset a graph that has no set relationship with the bigger graph, just do something like:
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
# These are in unitless percentages of the figure size. (0,0 is bottom left)
left, bottom, width, height = [0.25, 0.6, 0.2, 0.2]
ax2 = fig.add_axes([left, bottom, width, height])
ax1.plot(range(10), color='red')
ax2.plot(range(6)[::-1], color='green')
plt.show()
If you want to have some sort of relationship between the two, have a look at some of the examples here: http://matplotlib.org/1.3.1/mpl_toolkits/axes_grid/users/overview.html#insetlocator
This is useful if you want the inset to be a "zoomed in" version, (say, at exactly twice the scale of the original) that will automatically update as you pan/zoom interactively.
For simple insets, though, just create a new axes as I showed in the example above.
这篇关于如何在另一个python图表中添加不同的图形(作为插图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!