散点图中的轴范围 [英] axis range in scatter graphs

查看:109
本文介绍了散点图中的轴范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用下面的代码来绘制运行4个函数所花费的时间. x轴表示执行次数,而y轴表示花费的时间 运行功能.

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)设置x轴的极限,以便仅显示正值(x代表 每个函数执行的次数,因此始终为正)

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)为4个功能创建图例

2) create a legend for the 4 functions

谢谢

标记

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)

推荐答案

您需要致电 ,即可显示图例. label kwarg仅在所讨论的艺术家对象上设置_label属性.它在这里是为了方便起见,因此图例中的标签可以与绘图命令明确关联.如果不显式调用ax.legend(...),它将不会在图例中添加图例.另外,您希望ax.set_xlim而不是ax.xlim来调整xaxis限制.还要查看 ax.axis .

You need to call legend for the legend to appear. The label 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 calling ax.legend(...). Also, you want ax.set_xlim, not ax.xlim to adjust the xaxis limits. Have a look at ax.axis as well.

听起来您想要这样的东西:

It sounds like you want something like this:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

# Generate some data
x = np.arange(0, 22, 2)
f1, f2, f3, f4 = np.cumsum(np.random.random((4, x.size)) - 0.5, axis=1)

# It's much more convenient to just use pyplot's factory functions...
fig, ax = plt.subplots()

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')

colors = ['tomato', 'violet', 'blue', 'green']
labels = ['Thing One', 'Thing Two', 'Thing Three', 'Thing Four']
for func, color, label in zip([f1, f2, f3, f4], colors, labels):
    ax.plot(x, func, 'o', color=color, markersize=10, label=label)

ax.legend(numpoints=1, loc='upper left')
ax.set_xlim([0, x.max() + 1])

fig.savefig('performance.png', dpi=100)

这篇关于散点图中的轴范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆