Python 合并地物或子地块对象
我确实希望通过单独的函数使用mathplotlib创建两个图像对象。我想把这些图像合并到一个图像中 例如:Python 合并地物或子地块对象,python,matplotlib,Python,Matplotlib,我确实希望通过单独的函数使用mathplotlib创建两个图像对象。我想把这些图像合并到一个图像中 例如: #!/usr/bin/env python3 import matplotlib.pyplot as plt def plot1(): fig = plt.figure() plt.plot([1, 2], [1, 2], '-',color=(0,100/256,170/256)) return fig def plot2(): fig = plt.fi
#!/usr/bin/env python3
import matplotlib.pyplot as plt
def plot1():
fig = plt.figure()
plt.plot([1, 2], [1, 2], '-',color=(0,100/256,170/256))
return fig
def plot2():
fig = plt.figure()
plt.plot([1, 2], [0, 3], '-',color=(0.5,0.5,0.5))
return fig
fig = plt.figure()
fig1 = plot1
fig2 = plot2
产生两幅图像:
fig1.show()
fig2.show()
但如何将这些结合起来呢
fig(fig1,fig2); fig.show()
挑战在于我不想直接访问(x,y)值——只通过函数。比如:
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)
fig.show()
我不会这么做的,因为我的知识基础很小,我到目前为止。谢谢您的帮助。如果“合并”是指在一个图形中绘制两条线/函数,只需定义一次您的
plt.figure
对象即可。打印后,不需要返回任何对象,因为打印将在函数外部定义的地物对象中完成
import matplotlib.pyplot as plt
fig = plt.figure()
def plot1():
plt.plot([1, 2], [1, 2], '-',color=(0,100/256,170/256))
return
def plot2():
plt.plot([1, 2], [0, 3], '-',color=(0.5,0.5,0.5))
return
plot1()
plot2()
另一个选择是
fig, axes = plt.subplots()
然后使用轴
在函数内部绘制
axes.plot([1, 2], [1, 2], '-',color=(0,100/256,170/256))
这将进一步允许您使用axis实例axes
修改图表/打印属性
使用函数按自己的方式进行操作
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)
fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
def plot1(ax): # ax now points to ax1
ax.plot(x, y)
ax.set_title('Sharing Y axis')
def plot2(ax): # ax now points to ax2
ax.scatter(x, y)
plot1(ax1) # Pass the first axis instance
plot2(ax2) # Pass the second axis instance
fig.show()
Thx很多--让我的一天过得很愉快Thx很多--让我的一天过得很愉快