# How to plot graphs of functions¶

Useful tutorial: https://matplotlib.org/api/colors_api.html

Here is a python script:

#-*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C, S = np.cos(X), np.sin(X)

# Changing colors and line widths
plt.figure(figsize=(10, 6), dpi=80)
plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-")
plt.plot(X, S, color="red",  linewidth=2.5, linestyle="-")

# Setting limits Current limits of the figure are a bit too tight
# and we want to make some space in order to clearly see all data points.
plt.xlim(X.min() * 1.1, X.max() * 1.1)
plt.ylim(C.min() * 1.1, C.max() * 1.1)

# Setting ticks
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])
plt.yticks([-1, 0, +1])

# Moving spines
# Spines are the lines connecting the axis tick marks and noting the
# boundaries of the data area. They can be placed at arbitrary positions
# and until now, they were on the border of the axis. We’ll change that
# since we want to have them in the middle. Since there are four of them
# (top/bottom/left/right), we’ll discard the top and right by setting
# their color to none and we’ll move the bottom and left ones to
# coordinate 0 in data space coordinates.

ax = plt.gca()  # gca stands for 'get current axis'
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

# Setting tick labels:
# Ticks are now properly placed but their label
# is not very explicit. We could guess that 3.142 is π but it would be
# better to make it explicit. When we set tick values, we can also
# provide a corresponding label in the second argument list.
# Note that we’ll use latex to allow for nice rendering of the label.
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
plt.yticks([-1, 0, +1],
[r'$-1$', r'', r'$+1$'])

plt.plot(X, C)
plt.plot(X, S)

plt.show()
1,1           Top



# Was bei der Einbindung zu beachten ist:¶

Jedes Bild besteht aus mehreren Dokumenten. Dem generischen/ sprachunabhängigen (xxx.meta.xml) und je einem Paar (xxx.content_de.png, xxx_de.meta.xml und xxx.content_en.de, xxx.meta_en.xml) von sprachabhängigen. In Artikeln wird der Link auf das generische eingetragen. Dann wird automatisch die richtige Sprache ausgewählt.

Add picture from clipboard (Maximum size: 500 MB)