Author Archive

Animationen erzeugen mit matplotlib und imagemagick

Hier ein Beispiel, wie man Bilder für eine Animation mit matplotlib erstellen kann, adaptiert von im Netz gefundenen Code

Der folgende Python-Code erzeugt 720 einzelne Bilder und legt diese im Dateisystem ab. Mittels magick -quality 100 *.png outputfile.mpeg werden dann die Bilder zu einem MPEG-Video kombiniert. Hinweis: Nur unter Windows heißt der Befehl „magick“ da „convert“ auch ein Systemprogramm ist.

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from mpl_toolkits.mplot3d import Axes3D

df = sns.load_dataset('iris')
sns.set(style = "darkgrid")

fig = plt.figure()
fig.set_size_inches(16, 9)

ax = fig.add_subplot(111, projection = '3d')

x = df['sepal_width']
y = df['sepal_length']
z = df['petal_width']

ax.set_xlabel("sepal_width")
ax.set_ylabel("sepal_lesngth")
ax.set_zlabel("petal_width")

c = {'setosa':'red', 'versicolor':'blue', 'virginica':'green'}

ax.scatter(x, y, z,c=df['species'].apply(lambda x: c[x]))

for angle in range(0, 720):
    ax.view_init((angle+1)/10, angle)
    plt.draw()
    plt.savefig('r:/'+str(angle).zfill(3)+'.png')

Eine kürzere Version der Animation habe ich unter https://www.youtube.com/watch?v=gdgvXpq4k1w abgelegt.

Hinweise zu anderen Konvertierungsprogrammen gibt es unter anderem hier: https://www.andrewnoske.com/wiki/Convert_an_image_sequence_to_a_movie

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

CSV-Dateien mit speziellen Spaltentrennern in Python laden

Um einige Klassifikations-Algorithmen in Python ausprobieren zu können habe ich heute die Swiss Banknote Data von Flury und Riedwyl benötigt. Die Daten sind im Netz z.B. unter http://www.statistics4u.com/fundstat_eng/data_fluriedw.html verfügbar, ich wollte sie aber nicht manuell einladen müssen.

Mit dem folgenden Code, adaptiert von https://hackersandslackers.com/scraping-urls-with-beautifulsoup/, kann man die Daten lokal abspeichern und dann in einen pandas Dataframe einladen.

import pandas as pd
import requests
from bs4 import BeautifulSoup
 
headers = {
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'GET',
    'Access-Control-Allow-Headers': 'Content-Type',
    'Access-Control-Max-Age': '3600',
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'
}
 
url = "http://www.statistics4u.com/fundstat_eng/data_fluriedw.html"
req = requests.get(url, headers)
soup = BeautifulSoup(req.content, 'html.parser')
 
a=soup.find('pre').contents[0]
 
with open('banknote.csv','wt') as data:
    data.write(a)
 
df = pd.read_csv('banknote.csv',engine='python',skiprows=5,delim_whitespace=True)
print(df)

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Jupyter Notebooks aus dem lokalen Netz nutzen

Ich habe heute auf einer meiner Linux-Maschinen Jupyter Notebook installiert. Um die — für die Arbeit im lokalen Netz lästigen — Sicherheitsabfragen zu umgehen, habe ich mir ausgehend von https://stackoverflow.com/questions/41159797/how-to-disable-password-request-for-a-jupyter-notebook-session ein kleines Startskript geschrieben:

#! /bin/bash
jupyter notebook --ip='*' --NotebookApp.token='' --NotebookApp.password=''

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

pandas auf der GPU

Mit cudf gibt es ein Paket, das pandas Datenstrukturen auf nvidia-Grafikkarten verarbeiten kann. Einen i7 3770 mit 24 GB RAM habe ich jetzt mit einer CUDA-fähigen Grafikkarte (Typ Quadro P400) ausgestattet, damit ich damit rumspielen arbeiten kann. Unter https://towardsdatascience.com/heres-how-you-can-speedup-pandas-with-cudf-and-gpus-9ddc1716d5f2 findet man passende Beispiele, diese habe ich in einem Jupyter-Notebook laufenlassen.

Ein Geschwindigkeitszuwachs ist erkennbar, insbesondere bei der Matrix-Größe aus dem verlinkten Beispiel war die CUDA-Variante mehr als 3x so schnell wie die CPU-Variante. Das Merge mit der vollen Matrix-Größe lief bei mir leider nicht, da limitieren vermutlich die 2 GB RAM, die die P400 bietet.

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Mehr zu Scatterplots mit Seaborn

This entry is part 2 of 3 in the series Seaborn

Im letzten Beitrag hatten wir mit hue die Zugehörigkeit der Iris Data Orchideen dargestellt, Seaborn besitzt aber mit style und size noch weitere Möglichkeiten der Unterscheidung. style nutzt dabei verschiedene Symbole, size unterschiedliche Punktgrößen. Die verschiedenen Optionen können auch kombiniert werden.

import seaborn as sns
sns.set(style = "darkgrid")
iris=sns.load_dataset('iris')
 
sns.scatterplot(
    x=iris['sepal_width'],
    y=iris['sepal_length'],
    style=iris['species'],
    legend=False
)

import seaborn as sns
sns.set(style = "darkgrid")
iris=sns.load_dataset('iris')
 
sns.scatterplot(
    x=iris['sepal_width'],
    y=iris['sepal_length'],
    size=iris['species'],
    legend=False
)

import seaborn as sns
sns.set(style = "darkgrid")
iris=sns.load_dataset('iris')
 
sns.scatterplot(
    x=iris['sepal_width'],
    y=iris['sepal_length'],
    hue=iris['species'],
    style=iris['species'],
    size=iris['species'],    
    legend=False
)

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Scatterplots mit Python und Seaborn

This entry is part 1 of 3 in the series Seaborn

Hier ein einfaches Beispiel für einen Scatterplot mit Python und dem Seaborn Modul. Das Beispiel nutzt den bekannten Iris-Datensatz von R. Fisher, der gut für Klassifikationstechniken genutzt werden kann.

import seaborn as sns
 
iris=sns.load_dataset('iris')
 
sns.scatterplot(x=iris['sepal_width'],y=iris['sepal_length'],hue=iris['species'])

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Mit Python suchen und ersetzen in CSV-Dateien (mit pandas)

Nachdem wir uns im letzten Artikel angeschaut hatten, wie man mit openpyxl Funktionen Felder in CSV-Dateien mit Werten aus Excel-Dateien ersetzen kann, heute nun die pandas Implementierung dessen.

Sie nutzt auch openpyxl zum Einlesen der Excel-Datei, da xlrd, das bisher von pandas genutzte Modul für Excel-Dateien, den Support für XLSX Formate eingestellt hat.

Die Arbeitsweise des Codes ist recht einfach. pandas liest die Datei, da die Tabelle nicht links oben anfängt, werden die erste Zeile und Spalte ignoriert und die Spalten passend benannt. Dann iterieren wird durch den Dataframe und ersetzen munter…

import pandas as pd
 
path = "python_test.xlsx"
df = pd.read_excel(path,engine='openpyxl',
                   sheet_name='Tabelle2',skiprows=1,
                   usecols={1,2},header=None)
 
df = df.rename(columns={1: "Key", 2: "Value"})
 
with open('Python_test.txt') as input_file:
    text = input_file.read()
 
    for index, row in df.iterrows():
        text = text.replace(row['Key'] ,str(row['Value']))
 
    with open('Python_test_output_pd.txt','w') as output_file:
        output_file.write(text)

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Mit Python suchen und ersetzen in CSV Dateien

Nachdem wir bereits mit Excel und VBA Platzhalter in CSV Dateien gesucht und mit Inhalten ersetzt haben heute das ganze mit Python und OpenPyxl.

Ausgangspunkt ist eine Exceldatei „python_test.xlsx“ mit einer Named Range „Felder“ im Tabellenblatt „Tabelle2“.

Mit der openpyxl Bibliothek laden wir das Excel-Blatt und holen uns die Inhalte der Range in ein Dictionary. Jeden der Keys aus dem Dictionary suchen wir dann in der CSV Datei und ersetzen ihn gegen den Wert aus der Excel-Datei.

# -*- coding: utf-8 -*-
import openpyxl 
 
path = "python_test.xlsx"
workbook = openpyxl.load_workbook(path) 
 
def get_sheet_and_location(workbook, named_range):
    x = list(workbook.defined_names['Felder'].destinations)[0]
    return x[0], x[1].replace('$','').split(':')[0],x[1].replace('$','').split(':')[1]
 
 
sheet, start, stop = get_sheet_and_location(workbook,'Felder')
worksheet = workbook[sheet]
rng=worksheet[start:stop] 
 
replacements = {}
 
for row in rng:
    c1, c2 = row
    replacements[c1.value] = c2.value
 
 
 
with open('Python_test.txt') as input_file:
 
    text = input_file.read()
 
    for key in replacements:
        text = text.replace(key,str(replacements[key]))
 
    with open('Python_test_output.txt','w') as output_file:
        output_file.write(text)

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Prägestempel mit OpenSCAD entwerfen

Um meine Kreationen aus Leder zu „branden“, habe ich mir heute in OpenSCAD einen kleinen Stempel entworfen, den ich jetzt bei Shapeways in Kupfer sintern lasse. Hier die Datei:

rotate(a=[0,180,0]) {
    translate([0,0,-5]){
        linear_extrude(
            height = 3, 
            center = false, 
            convexity = 10, 
            slices = 20, 
            scale = 1.0, 
            $fn = 25) {
            text("UweZ",font="Helvetica", $fn=100);
        }
    }
}
 
translate([-34.5,-1.5,1.25]){
cube([35,13,2]);
}

in der Vorschau sieht das dann so aus:

Ob die Form optimal ist, wird sich im Januar rausstellen. Vermutlich muss ich noch die Buchstaben zulaufen lassen, d.h. an der Bodenplatte minimal größer gestalten.

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Farbige Boxen in LaTeX mit tcolorbox

Hier einige Beispiele für farbige Boxen mit dem tcolorbox Paket, entnommen der Paketbeschreibung

\documentclass[12pt,ngerman]{scrartcl}
 
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{babel}
\usepackage{graphicx}
\usepackage{csquotes}
\usepackage{paralist}
\usepackage{xcolor}
\usepackage{blindtext}
\usepackage{microtype}
 
\usepackage{empheq}
\usepackage[most]{tcolorbox}
\tcbuselibrary{theorems}
 
\newtcbtheorem[number within=section]{mytheo}{My Theorem}%
{colback=green!5,colframe=green!35!black,fonttitle=\bfseries}{th}
 
\begin{document}
 
 
\begin{tcolorbox}
Hallo Welt!
\end{tcolorbox}
 
 
\begin{tcolorbox}
\blindtext
\end{tcolorbox}
 
\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black,title=My nice heading]
This is another \textbf{tcolorbox}.
\tcblower
Here, you see the lower part of the box.
\end{tcolorbox}
 
\begin{mytheo}{This is my title}{theoexample}
This is the text of the theorem. The counter is automatically assigned and,
in this example, prefixed with the section number. This theorem is numbered with
\ref{th:theoexample}, it is given on page~\pageref{th:theoexample}.
\end{mytheo}
 
 
\begin{equation}
\tcbset{fonttitle=\footnotesize}
\tcboxmath[colback=yellow!25!white,colframe=blue]{ a^2 = 16 }
\quad \Rightarrow \quad
\tcboxmath[colback=blue!25!white,colframe=red,title=Implication]%
{ a = 4 ~\vee~ a=-4. }
\end{equation}
 
 
\newtcbox{\otherbox}[1][]{nobeforeafter,math upper,tcbox raise base,
enhanced,frame hidden,boxrule=0pt,interior style={top color=green!10!white,
bottom color=green!10!white,middle color=green!50!yellow},
fuzzy halo=1pt with green,#1}
\begin{empheq}[box=\otherbox]{align}
a&=\sin(z)\\
E&=mc^2 + \int_a^b x\, dx
\end{empheq}
\begin{equation}
\tcbhighmath{E} = \otherbox{mc^2}
\end{equation}
 
 
\newtcolorbox{mybox}[2][]{colback=red!5!white,
colframe=red!75!black,fonttitle=\bfseries,
colbacktitle=red!85!black,enhanced,
attach boxed title to top center={yshift=-2mm},
title=#2,#1}
\begin{mybox}[colback=yellow]{Hello there}
This is my own box with a mandatory title
and options.
\end{mybox}
 
\begin{tcolorbox}[enhanced,
size=minimal,auto outer arc,
width=2.1cm,octogon arc,
colback=red,colframe=white,colupper=white,
fontupper=\fontsize{6mm}{6mm}\selectfont\bfseries\sffamily,
halign=center,valign=center,
square,arc is angular,
borderline={0.2mm}{-1mm}{red} ]
STOP
\end{tcolorbox}
 
\begin{tcolorbox}[enhanced,
size=minimal,auto outer arc,
width=2.1cm,octogon arc,
colback=green,colframe=white,colupper=black,
fontupper=\fontsize{6mm}{6mm}\selectfont\bfseries\sffamily,
halign=center,valign=center,
square,arc is angular,
borderline={0.2mm}{-1mm}{green}]
GO
\end{tcolorbox}
\end{document}

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website