散点图中的轴范围
我一直在使用下面的代码来绘制运行 4 个函数所花费的时间。 x 轴代表执行次数,y 轴代表花费的时间 运行一个函数。
我想知道您是否可以帮助我完成以下任务:
1)设置 x 轴的限制,以便仅显示正值(x 代表 每个函数执行的次数,因此始终为正)
2)为 4 个函数创建图例
谢谢,
Mark
import matplotlib
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.mlab as mlab
r = mlab.csv2rec('performance.csv')
fig = Figure(figsize=(9,6))
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.set_title("Function performance",fontsize=14)
ax.set_xlabel("code executions",fontsize=12)
ax.set_ylabel("time(s)",fontsize=12)
ax.grid(True,linestyle='-',color='0.75')
ax.scatter(r.run,r.function1,s=10,color='tomato');
ax.scatter(r.run,r.function2,s=10,color='violet');
ax.scatter(r.run,r.function3,s=10,color='blue');
ax.scatter(r.run,r.function4,s=10,color='green');
canvas.print_figure('performance.png',dpi=700)
I have been using the code below to plot the time spent to run 4 functions. The x axis represents the number of executions whereas the y axis represents the time spent
running a function.
I was wondering if you could help me accomplish the following:
1) set the limits of the x axis so that only positive values are shown (x represents
the number of times each function was executed and is, therefore, always positive)
2) create a legend for the 4 functions
Thank you,
Mark
import matplotlib
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.mlab as mlab
r = mlab.csv2rec('performance.csv')
fig = Figure(figsize=(9,6))
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.set_title("Function performance",fontsize=14)
ax.set_xlabel("code executions",fontsize=12)
ax.set_ylabel("time(s)",fontsize=12)
ax.grid(True,linestyle='-',color='0.75')
ax.scatter(r.run,r.function1,s=10,color='tomato');
ax.scatter(r.run,r.function2,s=10,color='violet');
ax.scatter(r.run,r.function3,s=10,color='blue');
ax.scatter(r.run,r.function4,s=10,color='green');
canvas.print_figure('performance.png',dpi=700)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要调用
legend
才能显示图例。label
kwarg 仅在相关艺术家对象上设置_label
属性。它的存在是为了方便,以便图例中的标签可以清楚地与绘图命令相关联。如果不显式调用 ax.legend(...),它不会将图例添加到绘图中。另外,您需要使用ax.set_xlim
而不是ax.xlim
来调整 xaxis 限制。看看ax.axis
也是如此。听起来你想要这样的东西:
You need to call
legend
for the legend to appear. Thelabel
kwarg only sets the_label
attribute on the artist object in question. It's there for convenience, so that the label in the legend can be clearly associated with the plotting command. It won't add the legend to the plot without explicitly callingax.legend(...)
. Also, you wantax.set_xlim
, notax.xlim
to adjust the xaxis limits. Have a look atax.axis
as well.It sounds like you want something like this: