Ostatnia aktualizacja: 2022-06-06 07:50:58
Matplotlib
Import
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
Galerie wykresów
https://matplotlib.org/gallery/index.html
https://python-graph-gallery.com/
https://github.com/rasbt/matplotlib-gallery
https://seaborn.pydata.org/examples/index.html
import matplotlib.pyplot as plt
= [0, 7, 4, 5,8,-9]
x
plt.plot(x)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
= np.linspace(0, 2, 100)
x
='linear')
plt.plot(x, x, label**2, label='quadratic')
plt.plot(x, x**3, label='cubic')
plt.plot(x, x
'x label')
plt.xlabel('y label')
plt.ylabel(
"Simple Plot")
plt.title(
plt.legend()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
= np.arange(14)
x = np.cos(5 * x)
y
+ 2, 'blue', linestyle="-", label="niebieski")
plt.plot(x, y
+ 1, 'red', linestyle=":", label="czerwony")
plt.plot(x, y
'green', linestyle="--", label="zielony")
plt.plot(x, y,
='Legenda:')
plt.legend(title plt.show()
import matplotlib.pyplot as plt
import numpy as np
= np.arange(0, 10, 0.2)
x = np.sin(x)
y = plt.subplots()
fig, ax
ax.plot(x, y) plt.show()
import matplotlib.pyplot as plt
= plt.figure()
fig = fig.add_subplot(111)
ax 1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3)
ax.plot([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], color='darkgreen', marker='^')
ax.scatter([0.5, 4.5)
ax.set_xlim( plt.show()
Kolory
- https://matplotlib.org/stable/gallery/color/named_colors.html
- https://pl.wikipedia.org/wiki/Lista_kolor%C3%B3w
Markery https://matplotlib.org/stable/api/markers_api.html
import matplotlib.pyplot as plt
1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3)
plt.plot([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], color='darkgreen', marker='^')
plt.scatter([0.5, 4.5)
plt.xlim( plt.show()
import numpy as np
import matplotlib.pyplot as plt
= np.arange(0, 10)
x = x ^ 2
y # Labeling the Axes and Title
"Graph Drawing")
plt.title("Time")
plt.xlabel("Distance")
plt.ylabel(
# Formatting the line colors
'r')
plt.plot(x, y,
# Formatting the line type
'>')
plt.plot(x, y,
# save in pdf formats
'timevsdist.pdf', format='pdf') plt.savefig(
import numpy as np
import matplotlib.pyplot as plt
= np.arange(-5, 5, 0.1)
x = x[x < 0]
x1 = 1 / x1
y1
plt.plot(x1, y1)= x[x > 0]
x2 = 1 / x2
y2
plt.plot(x2, y2)-10, 10)
plt.ylim(=0, linestyle="--")
plt.axhline(y=0, linestyle=":")
plt.axvline(x plt.show()
import numpy as np
import matplotlib.pyplot as plt
= np.arange(-3, 3, 0.1)
x = x ** 2 + 2 * x
y
plt.plot(x, y)=[-1, 5], text="cos tam")
plt.annotate(xy-2, 1, 2], color="red")
plt.xticks(["abc", color="green")
plt.ylabel( plt.show()
import numpy as np
from matplotlib import pyplot as plt
= np.arange(0, 10)
x = x ^ 2
y = x ^ 3
z = x ^ 4
t # Labeling the Axes and Title
"Graph Drawing")
plt.title("Time")
plt.xlabel("Distance")
plt.ylabel(
plt.plot(x, y)
# Annotate
=[2, 1], text='Second Entry')
plt.annotate(xy=[4, 6], text='Third Entry')
plt.annotate(xy# Adding Legends
plt.plot(x, z)
plt.plot(x, t)'Race1', 'Race2', 'Race3'], loc=4)
plt.legend([ plt.show()
from matplotlib import pyplot as plt
= [1, -3, 4, 5, 6]
x = [2, 6, -4, 1, 2]
y = [70, 60, 1, 50, 2]
area =">", color="brown", alpha=0.5, s=area)
plt.scatter(x, y, marker plt.show()
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
19680801)
np.random.seed(
= 50
N = np.random.rand(N)
x = np.random.rand(N)
y = np.random.rand(N)
colors = (30 * np.random.rand(N)) ** 2 # 0 to 15 point radii
area
=area, c=colors, alpha=0.5)
plt.scatter(x, y, s plt.show()
import matplotlib.pyplot as plt
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
= ['Frogs', 'Hogs', 'Dogs', 'Logs']
labels = [15, 30, 45, 10]
sizes = [0, 0.1, 0, 0] # only "explode" the 2nd slice (i.e. 'Hogs')
explode
= plt.subplots()
fig1, ax1 =explode, labels=labels, autopct='%1.1f%%',
ax1.pie(sizes, explode=True, startangle=90)
shadow'equal') # Equal aspect ratio ensures that pie is drawn as a circle.
ax1.axis(
plt.show()
Wersja prostsza:
import matplotlib.pyplot as plt
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
= ['Frogs', 'Hogs', 'Dogs', 'Logs']
labels = [15, 30, 45, 10]
sizes = [0, 0.1, 0, 0] # only "explode" the 2nd slice (i.e. 'Hogs')
explode
=explode, labels=labels, autopct='%1.1f%%',
plt.pie(sizes, explode=True, startangle=90)
shadow'equal')
plt.axis(
plt.show()
import numpy as np
import matplotlib.pyplot as plt
= np.arange(0,3,0.1)
x 2, 2, 1)
plt.subplot(
plt.plot(x, x)2, 2, 2)
plt.subplot(* 2)
plt.plot(x, x 2, 2, 3)
plt.subplot(* x)
plt.plot(x, x 2, 2, 4)
plt.subplot(** 3)
plt.plot(x, x
plt.tight_layout() plt.show()
import matplotlib.pyplot as plt
import numpy as np
= np.linspace(0, np.pi * 2, 100)
x 3, 1, 1)
plt.subplot('r')
plt.plot(x, np.sin(x), True)
plt.grid(0, np.pi * 2)
plt.xlim(3, 1, 2)
plt.subplot('g')
plt.plot(x, np.cos(x), True)
plt.grid(0, np.pi * 2)
plt.xlim(3, 1, 3)
plt.subplot('r', x, np.cos(x), 'g')
plt.plot(x, np.sin(x), True)
plt.grid(0, np.pi * 2)
plt.xlim(
plt.tight_layout()"fig3.png", dpi=72)
plt.savefig( plt.show()
import numpy as np
import matplotlib.pyplot as plt
= plt.subplots()
fig, ax1 = np.arange(0.01, 10.0, 0.01)
x = x ** 2
y 'r')
ax1.plot(x, y, = ax1.twinx()
ax2 = np.sin(x)
y2
ax2.plot(x, y2)
fig.tight_layout() plt.show()
import numpy as np
import matplotlib.pyplot as plt
= plt.subplots()
fig, ax1 = np.arange(0.01, 10.0, 0.01)
t = np.exp(t)
s1 'b-')
ax1.plot(t, s1, 'time (s)')
ax1.set_xlabel(
'exp', color='b')
ax1.set_ylabel('y', colors='b')
ax1.tick_params(
= ax1.twinx()
ax2 = np.sin(2 * np.pi * t)
s2 'r.')
ax2.plot(t, s2, 'sin', color='r')
ax2.set_ylabel('y', colors='r')
ax2.tick_params(
fig.tight_layout() plt.show()
import numpy as np
import matplotlib.pyplot as plt
= [10, 15, 18, 22, 27]
wys = np.arange(0, len(wys))
x = ["black", "red", "green", "yellow", "pink"]
k =k, width=0.75)
plt.bar(x, wys, color= ["Kategoria A", "Kategoria B", "Kategoria C", "Kategoria D", "Kategoria E"]
etyk =45)
plt.xticks(x, etyk, rotation= [20, 30, 40, 50, 60]
y2
plt.plot(x, y2)"tytulik")
plt.title(
plt.tight_layout() plt.show()
import numpy as np
import matplotlib.pyplot as plt
= [3, 12, 5, 18, 45]
height = ('A', 'B', 'C', 'D', 'E')
bars = np.arange(len(bars))
y_pos =['black', 'red', 'green', 'blue', 'cyan'])
plt.bar(y_pos, height, color
plt.xticks(y_pos, bars) plt.show()
import numpy as np
import matplotlib.pyplot as plt
= [[30, 25, 50, 20],
data 40, 23, 51, 17],
[35, 22, 45, 19]]
[= np.arange(4)
X
+ 0.00, data[0], color='b', width=0.25, label="A")
plt.bar(X + 0.25, data[1], color='g', width=0.25, label="B")
plt.bar(X + 0.50, data[2], color='r', width=0.25, label="C")
plt.bar(X = np.arange(2015,2019)
labelsbar +0.25,labelsbar)
plt.xticks(X
plt.legend() plt.show()
import numpy as np
import matplotlib.pyplot as plt
= 5
N
= (20, 35, 30, 35, 27)
boys = (25, 32, 34, 20, 25)
girls = np.arange(N)
ind = 0.35
width
="boys")
plt.bar(ind, boys, width, label=boys, label="girls")
plt.bar(ind, girls, width,bottom
'Contribution')
plt.ylabel('Contribution by the teams')
plt.title('T1', 'T2', 'T3', 'T4', 'T5'))
plt.xticks(ind, (0, 81, 10))
plt.yticks(np.arange(
plt.legend() plt.show()
import numpy as np
import matplotlib.pyplot as plt
= [3, 12, 5, 18, 45]
width = ('A', 'B', 'C', 'D', 'E')
bars = np.arange(len(bars))
x_pos =['black', 'red', 'green', 'blue', 'cyan'])
plt.barh(x_pos, width, color
plt.yticks(x_pos, bars) plt.show()
import numpy as np
import matplotlib.pyplot as plt
= [[30, 25, 50, 20],
data 40, 23, 51, 17],
[35, 22, 45, 19]]
[= np.arange(4)
Y
+ 0.00, data[0], color='b', height=0.25, label="A")
plt.barh(Y + 0.25, data[1], color='g', height=0.25, label="B")
plt.barh(Y + 0.50, data[2], color='r', height=0.25, label="C")
plt.barh(Y = np.arange(2015,2019)
labelsbar + 0.25, labelsbar)
plt.yticks(Y
plt.legend() plt.show()
import numpy as np
import matplotlib.pyplot as plt
= 5
N
= (20, 35, 30, 35, 27)
boys = (25, 32, 34, 20, 25)
girls = np.arange(N)
ind = 0.35
height
="boys")
plt.barh(ind, boys, height, label=boys, label="girls")
plt.barh(ind, girls, height, left
'Contribution')
plt.xlabel('Contribution by the teams')
plt.title('T1', 'T2', 'T3', 'T4', 'T5'))
plt.yticks(ind, (0, 81, 10))
plt.xticks(np.arange(
plt.legend() plt.show()
import matplotlib.pyplot as plt
= [1, 4, 5, 6, 3, 9, 7, 20]
dane
plt.boxplot(dane) plt.show()
import matplotlib.pyplot as plt
import numpy as np
# Creating dataset
10)
np.random.seed(= np.random.normal(100, 20, 200)
data
# Creating plot
plt.boxplot(data)
# show plot
plt.show()
import matplotlib.pyplot as plt
= [1, 1, 2, 3, 3, 5, 7, 8, 9, 10,
x 10, 11, 11, 13, 13, 15, 16, 17, 18, 18]
=4)
plt.hist(x, bins plt.show()
import pandas as pd
import matplotlib.pyplot as plt
= [20, 22, 25, 27, 21, 23, 37, 31, 61, 45, 41, 32]
ages = [18, 25, 35, 60, 100]
bins = pd.cut(ages, [18, 26, 36, 61, 100], right=False)
cats2 print(cats2)
= ['Youth', 'YoungAdult',
group_names 'MiddleAged', 'Senior']
=pd.cut(ages, bins, labels=group_names)
data
plt.hist(data) plt.show()
import pandas as pd
import matplotlib.pyplot as plt
= [20, 22, 25, 27, 21, 23, 37, 31, 61, 45, 41, 32]
ages = [18, 25, 35, 60, 100]
bins =bins)
plt.hist(ages, bins plt.show()
import matplotlib.pyplot as plt
= [1, 1, 2, 3, 3, 5, 7, 8, 9, 10,
x 10, 11, 11, 13, 13, 15, 14, 12, 18, 18]
=[0,5,10,15,20])
plt.hist(x, bins0,5,10,15,20])
plt.xticks([ plt.show()
import matplotlib.pyplot as plt
= [1, 1, 2, 3, 3, 5, 7, 8, 9, 10,
x 10, 11, 11, 13, 13, 15, 14, 12, 18, 18]
=[0,5,10,15,20], cumulative=True)
plt.hist(x, bins0,5,10,15,20])
plt.xticks([ plt.show()
import matplotlib.pyplot as plt
= [1, 1, 2, 3, 3, 5, 7, 8, 9, 10,
x 10, 11, 11, 13, 13, 15, 14, 12, 18, 18]
=[0,5,10,15,20], density=True)
plt.hist(x, bins0,5,10,15,20])
plt.xticks([ plt.show()
Wykres warstwowy:
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.html
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
123)
np.random.seed(
= pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df
df.plot.area() plt.show()
Wykres pierścieniowy:
import matplotlib.pyplot as plt
import numpy as np
345)
np.random.seed(= np.random.randint(20, 100, 6)
data = sum(data)
total = data/total*100
data_per = (0.2, 0, 0, 0, 0, 0)
explode = explode, labels = [round(i,2) for i in (list(data_per))])
plt.pie(data_per, explode = plt.Circle( (0,0), 0.7, color='white')
circle =plt.gcf()
p
p.gca().add_artist(circle) plt.show()
Helisa:
\[\begin{cases} x=a\cos (t) \\ y=a\sin(t) \\ z=at \end{cases}\]
import numpy as np
import matplotlib.pyplot as plt
= plt.figure()
fig = plt.axes(projection='3d')
ax = np.linspace(0, 15, 1000)
t = 3
a = a * np.sin(t)
xline = a * np.cos(t)
yline = a * t
zline
ax.plot3D(xline, yline, zline) plt.show()
Torus
\[p(\alpha,\ \beta)=\Big((R+r\cos \alpha)\cos \beta,\ (R+r\cos \alpha)\sin \beta,\ r\sin \alpha\Big)\]
import numpy as np
import matplotlib.pyplot as plt
= plt.figure()
fig = plt.axes(projection='3d')
ax = 1
r = 5
R = np.arange(0, 2 * np.pi, 0.1)
alpha = np.arange(0, 2 * np.pi, 0.1)
beta = np.meshgrid(alpha, beta)
alpha, beta = (R + r * np.cos(alpha)) * np.cos(beta)
x = (R + r * np.cos(alpha)) * np.sin(beta)
y = r * np.sin(alpha)
z
ax.plot_wireframe(x, y, z) plt.show()
Źródło: