Mar*_*ark 8 python scatter matplotlib limit
我一直在使用下面的代码来绘制运行4个函数所花费的时间.x轴表示执行次数,而y轴表示运行函数所花费的时间.
我想知道你是否可以帮我完成以下任务:
1)设置x轴的限制,以便仅显示正值(x表示每个功能执行的次数,因此始终为正)
2)为4个功能创建一个图例
谢谢,
标记
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)
Run Code Online (Sandbox Code Playgroud)
Joe*_*ton 22
您需要调用legend图例才能显示.该labelkwarg只设置_label有问题的艺术家对象的属性.它是为了方便起见,因此图例中的标签可以与绘图命令明确关联.如果没有显式调用,它不会将图例添加到图中ax.legend(...).此外,您想要ax.set_xlim,而不是ax.xlim调整xaxis限制.看看ax.axis也好.
听起来你想要这样的东西:
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)
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
30045 次 |
| 最近记录: |