python的matplotlib中范围的散点图

2024-03-28 09:58:32 发布

您现在位置:Python中文网/ 问答频道 /正文

到目前为止我有这个图表,有点难看。每种类型的标记都是一种算法的精度。你知道吗

scatter plot

这有两个问题:

  1. 我希望在内容和轴之间有空格,但是只显示y[0,1]的记号,并且让x轴不显示负值(没有负时间之类的东西)。你知道吗
  2. 我想将x值显示为它们的日志转换,但是保留原始值,这样您就可以看到实际值。你知道吗

对于#1,我尝试过玩xticks,如你在下面看到的,但没有太大的成功。你知道吗

对于#2,将数据放入np.log10()中很简单,但随后轴记号也进行了对数变换。我觉得应该有一个简单的方法来做这个日志显示(似乎是一个很正常的事情做)?你知道吗

以下是我目前的代码:

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

# create fake data
data = {}
data['A'] = []
data['B'] = []
data['C'] = []

n = 5
data['A'] = zip(np.random.uniform(0, 10000, size=n), np.random.uniform(0, 0.6, size=n))
data['B'] = zip(np.random.uniform(0, 200, size=n), np.random.uniform(0, 0.6, size=n))
data['C'] = zip(np.random.uniform(0, 5000, size=n), np.random.uniform(0, 0.6, size=n))

# make graph
markers = ['+', '*', 'x']
colors = ['b', 'r', 'g']
fig = plt.figure()
ax1 = fig.add_subplot(111)
plots = []
labels = []

# extract data
i = 0
for algorithm in ['A', 'B', 'C']:
    results = data[algorithm]
    testing = np.array([float(x[1]) for x in results if x > 0.0])
    ts = np.array([int(x[0]) for x in results if x > 0.0])
    color = colors[i]
    marker = markers[i]
    plot = ax1.scatter(ts, testing, color=color, marker=marker, s=10)
    plots.append(plot)
    labels.append(algorithm)
    i += 1

# set axis and title
ax1.legend(plots, labels, loc='lower right')
ax1.set_xlabel("Time (sec)")
ax1.set_ylabel("Testing Accuracy")
ax1.set_title("Time versus testing accuracy")

# set axis limits
xticks, xticklabels = plt.xticks()
xmin = (3*xticks[0] - xticks[1])/2.
xmax = (3*xticks[-1] - xticks[-2])/2.
plt.xlim(xmin, xmax)
plt.xticks(xticks)

plt.ylim(0.0, 1.0)

# save to disk
plt.savefig("scatter.eps")

Tags: importfordatasizelabelsasnpplt