mercredi 22 juin 2016

How do I move overlapping annotations with matplotlib?


When I graph this, it works. The only problem is that the coordinates that get printed out on the graph overlap (the annotations). Is there a way that I can get the annotations to not overlap?

from urllib.request import urlopen
import json
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, PathPatch
from matplotlib.path import Path
from matplotlib.transforms import Affine2D
import numpy as np
import pylab
import re
import sunpy.time
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import *

dateAndTime = []
xcen = []
ycen = []
sciObj = []
def getNumberOfEntries(theJSON):
    return len(dateAndTime)   

def getInfo(counter, theJSON):
    cont = True
    while cont:
        try:
            dateAndTime.append(theJSON["Events"][counter]["date"])
            xcen.append(("%.2f" % theJSON["Events"][counter]["xCen"]))
            ycen.append(("%.2f" % theJSON["Events"][counter]["yCen"]))
            sciObj.append(theJSON["Events"][counter]["sciObjectives"])
            counter = counter + 1
            getInfo(counter, theJSON)
        except IndexError:
            cont = False
        break

def setXMax(theJSON):      
    xmax = xcen[0]
    for i in range (1, getNumberOfEntries(theJSON)-1):
        if xcen[i] > xmax:
            xmax = xcen[i]
    return float(xmax)+300

def setXMin(theJSON):
    xmin = xcen[0]
    for i in range (1, getNumberOfEntries(theJSON)-1):
        if xcen[i] < xmin:
            xmin = xcen[i]
    xmin = float(xmin)
    return xmin - 300

def setYMax(theJSON):      
    ymax = ycen[0]
    for i in range (1, getNumberOfEntries(theJSON)-1):
        if ycen[i] > ymax:
            ymax = ycen[i]
    return float(ymax)+300

def setYMin(theJSON):
    ymin = ycen[0]
    for i in range (1, getNumberOfEntries(theJSON)-1):
        if ycen[i] < ymin:
            ymin = ycen[i]
    return float(ymin)-300         

# def splitDateAndTime(i):
#     print(sunpy.time.parse_time(dateAndTime[i]))

def sort():
    for i in range(len(dateAndTime)):
        for j in range(len(dateAndTime)-1, i, -1):
            if ( dateAndTime[j] < dateAndTime[j-1]):

                temp1 = dateAndTime[j]
                dateAndTime[j] = dateAndTime[j-1]
                dateAndTime[j-1] = temp1

                temp2 = xcen[j]
                xcen[j] = xcen[j-1]
                xcen[j-1] = temp2

                temp3 = ycen[j]
                ycen[j] = ycen[j-1]
                ycen[j-1] = temp3

                temp4 = sciObj[j]
                sciObj[j] = sciObj[j-1]
                sciObj[j-1] = temp4

This part is the function that graphs the figure:

def plot(theJSON):
    fig, ax = plt.subplots(figsize=(25, 15))
    circle = Circle((0, 0), 980, facecolor='none', edgecolor=(0, 0.8, 0.8), linewidth=3, alpha=0.5)
    ax.add_patch(circle)

    plt.plot(xcen, ycen, 'ro', color = 'blue')
    plt.plot(xcen, ycen, color = 'red')

    plt.xlim([setXMin(theJSON), setXMax(theJSON)])
    plt.ylim([setYMin(theJSON), setYMax(theJSON)])

    for xy in zip(xcen, ycen):
        ax.annotate('(%s, %s)' % xy, xy=xy, textcoords='data') 

    ax.set_xticks(np.arange(setXMin(theJSON), setXMax(theJSON), 50))
    ax.set_yticks(np.arange(setYMin(theJSON), setYMax(theJSON), 50))

    plt.grid()
    plt.show()

def main():
    urlData = "http://www.lmsal.com/hek/hcr?cmd=search-events3&outputformat=json&instrument=IRIS&noaanum=11856&hasData=true"
    webUrl = urlopen(urlData)
    counter = 0                
    data = webUrl.read().decode('utf-8')
    theJSON = json.loads(data)
    getInfo(counter, theJSON)

    sort()

    for i in range (getNumberOfEntries(theJSON)):
        print(dateAndTime[i])
        print("(", xcen[i], ", ", ycen[i], ")")
        print(sciObj[i])
        print(' ')

    plot(theJSON)

main()

Here is a picture of the graph: helpMe


Aucun commentaire:

Enregistrer un commentaire