Grafiklar. Matplotlib moduli Matplotlib da graphika - import matplotlib.pyplot as plt
- plt.plot([1, 2, 3, 4, 5], [1, 2, 3, 4, 5])
- plt.show()
- import numpy as np
- # Независимая (x) и зависимая (y) переменные
- x = np.linspace(0, 10, 50)
- y = x
- # Построение графика
- plt.title("Линейная зависимость y = x") # заголовок
- plt.xlabel("x") # ось абсцисс
- plt.ylabel("y") # ось ординат
- plt.grid() # включение отображение сетки
- plt.plot(x, y) # построение графика
y = f(x) кўринишли функцияларнинг графигини чизиш - Ушбу графикни ҳосил қилиш учун Python да қуйидаги кўринишли код ёзамиз:
- import matplotlib.pyplot as plt
- import numpy as np
- # Prepare the
- datax = np.linspace(0, 10, 100)
- # Plot the
- dataplt.plot(x, x, label='linear')
- # Add a l
- egendplt.legend()
- # Show the
- plotplt.show()
import matplotlib.pyplot as plt # x axis values x = [1,2,3] # corresponding y axis values y = [2,4,1] # plotting the points plt.plot(x, y) # naming the x axis plt.xlabel('x - axis') # naming the y axis plt.ylabel('y - axis') # giving a title to my graph plt.title('My first graph!') # function to show the plot plt.show() import matplotlib.pyplot as plt - import matplotlib.pyplot as plt
- # line 1 points
- x1 = [1,2,3]
- y1 = [2,4,1]
- # plotting the line 1 points
- plt.plot(x1, y1, label = "line 1")
- # line 2 points
- x2 = [1,2,3]
- y2 = [4,1,3]
- plt.plot(x2, y2, label = "line 2") # plotting the line 2 points
- plt.xlabel('x - axis') # naming the x axis
- plt.ylabel('y - axis') # naming the y axis
- plt.title('Two lines on same graph!') # giving a title to my graph
- plt.legend() # show a legend on the plot
- plt.show() # function to show the plot
import matplotlib.pyplot as plt - import matplotlib.pyplot as plt
- # x axis values
- x = [1,2,3,4,5,6]
- # corresponding y axis values
- y = [2,4,1,5,2,6]
- # plotting the points
- plt.plot(x, y, color='green', line, linewidth = 3, marker='o', markerfacecolor='blue', markersize=12)
- # setting x and y axis range
- plt.ylim(1,8)
- plt.xlim(1,8)
- plt.xlabel('x - axis') # naming the x axis
- plt.ylabel('y - axis') # naming the y axis
- plt.title('Some cool customizations!') # giving a title to my graph
- plt.show() # function to show the plot
Гистограмма - import matplotlib.pyplot as plt
- # x-coordinates of left sides of bars
- left = [1, 2, 3, 4, 5]
- height = [10, 24, 36, 40, 5] # heights of bars
- tick_label = ['one', 'two', 'three', 'four', 'five'] # labels for bars
- # plotting a bar chart
- plt.bar(left, height, tick_label = tick_label, width = 0.8, color = ['red', 'green'])
- plt.xlabel('x - axis') # naming the x-axis
- plt.ylabel('y - axis') # naming the y-axis
- plt.title('My bar chart!') # plot title
- plt.show() # function to show the plot
Круговая диаграмма - import matplotlib.pyplot as plt
- # defining labels
- activities = ['eat', 'sleep', 'work', 'play']
- # portion covered by each label
- slices = [3, 7, 8, 6]
- colors = ['r', 'y', 'g', 'b'] # color for each label
- # plotting the pie chart
- plt.pie(slices, labels = activities, colors=colors, startangle=90, shadow = True, explode = (0, 0, 0.1, 0),
- radius = 1.2, autopct = '%1.1f%%')
- plt.legend() # plotting legend
- plt.show() # showing the plot
- # importing the required modules
- import matplotlib.pyplot as plt
- import numpy as np
- # setting the x - coordinates
- x = np.arange(0, 2*(np.pi), 0.1)
- # setting the corresponding y - coordinates
- y = np.sin(x)
- plt.plot(x, y) # plotting the points
- plt.show() # function to show the plot
- import numpy as np
- import matplotlib.pyplot as plt
- X = np.linspace(-np.pi, np.pi, 256)
- C, S = np.cos(X), np.sin(X)
- plt.plot(X, C)
- plt.plot(X, S)
- plt.show()
3D Plots - from mpl_toolkits.mplot3d import Axes3D
- fig = plt.figure()
- ax = Axes3D(fig)
- X = np.arange(-4, 4, 0.25)
- Y = np.arange(-4, 4, 0.25)
- X, Y = np.meshgrid(X, Y)
- R = np.sqrt(X**2 + Y**2)
- Z = np.sin(R)
- ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot')
Do'stlaringiz bilan baham: |