matplotlib散点图中的标记点

时间:2017-07-09 15:49:38

标签: python matplotlib plot

编辑:这个问题不重复,我不想绘制数字而不是点数,我想在我的点旁边绘制数字。

我正在使用matplotlib创建一个情节。绘制[[3,9],[4,8],[5,4]]

有三点

我可以轻松地用它们制作散点图

import matplotlib.pyplot as plt

allPoints = [[3,9],[4,8],[5,4]]

f, diagram = plt.subplots(1)

for i in range(3):
    xPoint =  allPoints[i][0]
    yPoint =  allPoints[i][1]
    diagram.plot(xPoint, yPoint, 'bo')

产生这个情节:

plot

我想用数字1,2,3标记每个点。

根据this SO回答,我尝试使用注释来标记每个点。

import matplotlib.pyplot as plt

allPoints = [[1,3,9],[2,4,8],[3,5,4]]

f, diagram = plt.subplots(1)

for i in range(3):
    pointRefNumber = allPoints[i][0]
    xPoint =  allPoints[i][1]
    yPoint =  allPoints[i][2]
    diagram.annotate(pointRefNumber, (xPoint, yPoint))

这会产生一个空白图。我正在密切关注另一个答案,但它没有产生任何情节。我在哪里弄错了?

2 个答案:

答案 0 :(得分:4)

我解决了自己的问题。我需要绘制点然后注释它们,注释没有内置绘图。

public void playSound(View view){
        if (null != mediaPlayer) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }

        switch(view.getId())
        {
            case R.id.button1:
                mediaPlayer = MediaPlayer.create(this, R.raw.doyouspeakenglish);
                break;
            case R.id.button2:
                mediaPlayer = MediaPlayer.create(this, R.raw.goodevening);
                break;
            case R.id.button3:
                mediaPlayer = MediaPlayer.create(this, R.raw.hello);
                break;
            case R.id.button4:
                mediaPlayer = MediaPlayer.create(this, R.raw.howareyou);
                break;
            case R.id.button5:
                mediaPlayer = MediaPlayer.create(this, R.raw.ilivein);
                break;
            case R.id.button6:
                mediaPlayer = MediaPlayer.create(this, R.raw.mynameis);
                break;
            case R.id.button7:
                mediaPlayer = MediaPlayer.create(this, R.raw.please);
                break;
            case R.id.button8:
                mediaPlayer = MediaPlayer.create(this, R.raw.welcome);
                break;
            default:
                throw new RuntimeException("Unknown button ID");
        }
        i++;
    Log.i("klik", Integer.toString(i));
        mediaPlayer.start();
}

编辑添加fontsize选项,就像在Gregoux的回答

中一样

答案 1 :(得分:4)

你可以这样做:

import matplotlib.pyplot as plt

points = [[3,9],[4,8],[5,4]]

for i in range(len(points)):
    x = points[i][0]
    y = points[i][1]
    plt.plot(x, y, 'bo')
    plt.text(x * (1 + 0.01), y * (1 + 0.01) , i, fontsize=12)

plt.xlim((0, 10))
plt.ylim((0, 10))
plt.show()

scatter_plot