我一直在使用主机绘制我的图形,我想从x轴取出十进制数字.我的下面的代码有点简化,它代表了我需要的东西.我希望x范围与RUNS矢量完全相同.非常感谢.
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot
import numpy as np
import mpl_toolkits.axisartist as AA
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1])
RUNS = [1,2,3,4,5]
NV = [26.3, 28.4, 28.5, 28.45, 28.5]
host = host_subplot(gs[0], axes_class = AA.Axes)
host.set_xlabel("Iteration")
host.set_ylabel("Stress (MPa)")
sv, = host.plot(RUNS,NV, marker = 'o', color = 'gray')
plt.grid(True)
plt.show()
Run Code Online (Sandbox Code Playgroud) 我看到很多例子解释了如何以不同的大小保存绘图,但没有一个使用 host_subplots。我想将图形保存为当我最大化首先出现绘图的窗口时的样子。以下块是我一直在做的较短版本:
>>> import matplotlib.pyplot as plt
>>> import mpl_toolkits.axisartist as AA
>>> from mpl_toolkits.axes_grid1 import host_subplot
>>>
>>> host = host_subplot(111, axes_class = AA.Axes)
>>> plt.subplots_adjust(right=0.75)
>>>
>>> time = [1, 2, 3, 4, 5]
>>> velocity = [2, 4, 6, 8, 10]
>>> another_variable = [15, 20, 25, 40, 55]
>>>
>>> S1, = host.plot(time,velocity, color = 'r')
>>>
>>> par1 = host.twinx()
>>> S2, = par1.plot(time, another_variable, color = 'g')
>>>
>>> plt.savefig('my_plot.png')
Run Code Online (Sandbox Code Playgroud)
然后我得到了以正常大小保存的图形!提前致谢!