plotting cartesian "high school"-style axes


I'm trying to draw something like

Below is my current attempt.

Four questions:
1. The y-axis label is vertical: how do I make it horizontal?
2. How can I move the axis numbering to be in the centre of the graph,
rather than around the outside?
3. Most of the mess below is to try to get the arrow heads not to be
stretched. Is there a better way to draw those in "screen space" rather than
"graph space"?
4. The axis label positioning is ugly too, is there a better way to align
that text's midpoint?

Answers to any of these will be greatfully received. :slight_smile:

Or, if I'm going about this all wrong and I missed an easy helper function,
that'd be even better!



from __future__ import division
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

def cartesian(w,h):
    fig = plt.figure(figsize=(5,5))
    ax = plt.axes()
    arrowExtraW = w/6
    arrowExtraH = h/6
    box = dict(edgecolor='white', facecolor='white', pad=5)

    plt.xlabel("$x$", bbox=box)
    ax.xaxis.set_label_coords(1.03, 0.515)

    plt.ylabel("$y$", bbox=box)
    ax.yaxis.set_label_coords(0.515, 1.03)

    plt.axis([-w-arrowExtraW, w+arrowExtraW, -h-arrowExtraH, h+arrowExtraH])
    plt.arrow(-w, 0, w*2, 0, head_width=arrowExtraH/1.5,
head_length=arrowExtraW, edgecolor="black",
            facecolor="black", label="$x$")
    plt.arrow(w, 0, -w*2, 0, head_width=arrowExtraH/1.5,
head_length=arrowExtraW, edgecolor="black", facecolor="black")
    plt.arrow(0, -h, 0, h*2, head_width=arrowExtraW/1.5,
head_length=arrowExtraH, edgecolor="black",
            facecolor="black", label="$y$")
    plt.arrow(0, h, 0, -h*2, head_width=arrowExtraW/1.5,
head_length=arrowExtraH, edgecolor="black", facecolor="black")

cartesian(20, 6)

View this message in context:"high-school"-style-axes-tp22217764p22217764.html
Sent from the matplotlib - users mailing list archive at