syn*_*tik 98 python plot matplotlib
我matplotlib用来绘制Python的数据(使用plot和errorbar函数).我必须绘制一组完全独立且独立的图,然后调整它们的ylim值,以便可以轻松地进行视觉比较.
如何ylim从每个绘图中检索值,以便我可以分别获取下ylim值和上ylim值的min和max,并调整绘图以便可以直观地比较它们?
当然,我可以分析数据并提出我自己的自定义ylim值...但我想用matplotlib它为我做这件事.关于如何轻松(和有效)地做到这一点的任何建议?
这是我使用的Python函数matplotlib:
import matplotlib.pyplot as plt
def myplotfunction(title, values, errors, plot_file_name):
# plot errorbars
indices = range(0, len(values))
fig = plt.figure()
plt.errorbar(tuple(indices), tuple(values), tuple(errors), marker='.')
# axes
axes = plt.gca()
axes.set_xlim([-0.5, len(values) - 0.5])
axes.set_xlabel('My x-axis title')
axes.set_ylabel('My y-axis title')
# title
plt.title(title)
# save as file
plt.savefig(plot_file_name)
# close figure
plt.close(fig)
Run Code Online (Sandbox Code Playgroud)
Ada*_*hes 31
ymin, ymax = axes.get_ylim()
Run Code Online (Sandbox Code Playgroud)
如果您正在使用该plt结构,为什么甚至打扰轴?这应该工作:
def myplotfunction(title, values, errors, plot_file_name):
# plot errorbars
indices = range(0, len(values))
fig = plt.figure()
plt.errorbar(tuple(indices), tuple(values), tuple(errors), marker='.')
plt.xlim([-0.5, len(values) - 0.5])
plt.xlabel('My x-axis title')
plt.ylabel('My y-axis title')
# title
plt.title(title)
# save as file
plt.savefig(plot_file_name)
# close figure
plt.close(fig)
Run Code Online (Sandbox Code Playgroud)
或者情况不是这样吗?
Tho*_*ves 18
利用上面的好答案并假设您只使用 plt 作为
import matplotlib.pyplot as plt
Run Code Online (Sandbox Code Playgroud)
然后您可以使用plt.axis()以下示例中的所有四个绘图限制。
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8] # fake data
y = [1, 2, 3, 4, 3, 2, 5, 6]
plt.plot(x, y, 'k')
xmin, xmax, ymin, ymax = plt.axis()
s = 'xmin = ' + str(round(xmin, 2)) + ', ' + \
'xmax = ' + str(xmax) + '\n' + \
'ymin = ' + str(ymin) + ', ' + \
'ymax = ' + str(ymax) + ' '
plt.annotate(s, (1, 5))
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
93494 次 |
| 最近记录: |