Bagaimana Anda memplot grafik yx dengan python?

Jika kita perlu memplot garis dari (1, 3) ke (8, 10), kita harus meneruskan dua larik [1, 8] dan [3, 10] ke fungsi plot

Contoh

Gambar garis dalam diagram dari posisi (1, 3) ke posisi (8, 10)

impor matplotlib. pyplot sebagai plt
impor numpy sebagai np

xpoint = np. larik([1, 8])
titik y = np. larik([3, 10])

plt. plot(titik x, titik y)
plt. menunjukkan()

Hasil

Bagaimana Anda memplot grafik yx dengan python?

Cobalah sendiri "

Sumbu x adalah sumbu mendatar

Sumbu y adalah sumbu vertikal



Merencanakan Tanpa Garis

Untuk memplot penanda saja, Anda dapat menggunakan parameter notasi string pintasan 'o', yang berarti 'dering'

Contoh

Gambarlah dua titik pada diagram, satu di posisi (1, 3) dan satu di posisi (8, 10)

impor matplotlib. pyplot sebagai plt
impor numpy sebagai np

xpoint = np. larik([1, 8])
titik y = np. larik([3, 10])

plt. plot(titik x, titik y, 'o')
plt. menunjukkan()

Hasil

Bagaimana Anda memplot grafik yx dengan python?

Cobalah sendiri "

Anda akan belajar lebih banyak tentang penanda di bab berikutnya


Beberapa Poin

Anda dapat memplot titik sebanyak yang Anda suka, pastikan Anda memiliki jumlah titik yang sama di kedua sumbu

Contoh

Buat garis dalam diagram dari posisi (1, 3) ke (2, 8) lalu ke (6, 1) dan terakhir ke posisi (8, 10)

impor matplotlib. pyplot sebagai plt
impor numpy sebagai np

xpoint = np. larik([1, 2, 6, 8])
titik y = np. larik([3, 8, 1, 10])

plt. plot(titik x, titik y)
plt. menunjukkan()

Hasil

Bagaimana Anda memplot grafik yx dengan python?

Cobalah sendiri "


Poin X bawaan

Jika kita tidak menentukan titik pada sumbu x, mereka akan mendapatkan nilai default 0, 1, 2, 3 (dll. , tergantung pada panjang titik-y

Jadi, jika kita mengambil contoh yang sama seperti di atas, dan menghilangkan titik-x, diagram akan terlihat seperti ini

Dalam contoh ini, kita akan memplot Y vs X di mana x dan y adalah daftar angka. Nomor masing-masing dari daftar x dan daftar y pada indeks i terjemahkan ke titik di peta

contoh. py

import matplotlib.pyplot as plt

# x axis and y axis data
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
y = [5, 7, 8, 1, 4, 9, 6, 3, 5, 2, 1, 8]

#plot y vs x
plt.plot(x, y)

#set title and x, y - axes labels
plt.title('Matplotlib Example')
plt.xlabel('x-axis label')
plt.ylabel('y-axis label')

#show plot to user
plt.show()

Keluaran

Bagaimana Anda memplot grafik yx dengan python?
Plot Y vs X

Kesimpulan

Sebagai penutup dari Tutorial Matplotlib ini, kita belajar bagaimana memplot Y vs X menggunakan Axes. plot() fungsi di perpustakaan Matplotlib Python


Untuk memplot y=1/x sebagai grafik tunggal dengan Python, kita dapat mengambil langkah-langkah berikut −

  • Atur ukuran gambar dan sesuaikan padding antara dan di sekitar subplot
  • Buat poin data menggunakan numpy
  • Plot x dan 1/x titik data menggunakan metode plot()
  • Tempatkan legenda pada gambar
  • Untuk menampilkan gambar, gunakan metode show()

Contoh

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

x = np.linspace(-10, 10, 101)
plt.plot(x, 1/x, label='$f(x)=\frac{1}{x}$')
plt.legend(loc='upper left')

plt.show()
_

Keluaran

Bagaimana Anda memplot grafik yx dengan python?

Bagaimana Anda memplot grafik yx dengan python?


Bagaimana Anda memplot grafik yx dengan python?

Dalam tutorial kami sebelumnya, kami belajar cara menggambar garis lurus, atau persamaan linier bertipe $y=mx+c$

Di sini, kita akan mempelajari cara memplot fungsi yang ditentukan $y=f(x)$ dengan Python, selama interval yang ditentukan

Kita mulai dengan memplot persamaan kuadrat paling sederhana $y=x^{2}$

Persamaan kuadrat

Persamaan kuadrat adalah persamaan polinomial orde dua dengan tipe $ax^{2} + bx + c = 0$, di mana $x$ adalah variabel dan $a \ne 0$. Memplot fungsi kuadrat hampir sama dengan memplot garis lurus pada tutorial sebelumnya

Di bawah ini adalah kode Matplotlib untuk memplot fungsi $y=x^{2}$. Ini adalah kode langsung yang sederhana; . Karena eksponen dari $x$ adalah $2$, hanya akan ada nilai positif dari $y$, sehingga kita dapat memposisikan di bawah

					
						import matplotlib.pyplot as plt
						import numpy as np

						# 100 linearly spaced numbers
						x = np.linspace(-5,5,100) 

						# the function, which is y = x^2 here
						y = x**2

						# setting the axes at the centre
						fig = plt.figure()
						ax = fig.add_subplot(1, 1, 1)
						ax.spines['left'].set_position('center')
						ax.spines['bottom'].set_position('zero')
						ax.spines['right'].set_color('none')
						ax.spines['top'].set_color('none')
						ax.xaxis.set_ticks_position('bottom')
						ax.yaxis.set_ticks_position('left')

						# plot the function
						plt.plot(x,y, 'r') 

						# show the plot
						plt.show()
					
				
Bagaimana Anda memplot grafik yx dengan python?

Persamaan Kubik

Selanjutnya, kita akan memplot fungsi kubik paling sederhana $y=x^{3}$

Karena eksponen dalam $y=x^{3}$ adalah $3$, pangkat pasti memiliki nilai negatif untuk nilai negatif $x$. Oleh karena itu, untuk visibilitas nilai negatif dalam sumbu $y$, kita perlu memindahkan sumbu $x$ ke pusat grafik. demikian diposisikan ke tengah

					
						import matplotlib.pyplot as plt
						import numpy as np

						# 100 linearly spaced numbers
						x = np.linspace(-5,5,100) 

						# the function, which is y = x^3 here
						y = x**3

						# setting the axes at the centre
						fig = plt.figure()
						ax = fig.add_subplot(1, 1, 1)
						ax.spines['left'].set_position('center')
						ax.spines['bottom'].set_position('center')
						ax.spines['right'].set_color('none')
						ax.spines['top'].set_color('none')
						ax.xaxis.set_ticks_position('bottom')
						ax.yaxis.set_ticks_position('left')

						# plot the function
						plt.plot(x,y, 'g') 

						# show the plot
						plt.show()
					
				
Bagaimana Anda memplot grafik yx dengan python?

Fungsi trigonometri

Di sini kita memplot fungsi trigonometri $y=\text{sin}(x)$ untuk nilai $x$ antara $-\pi$ dan $\pi$. Metode linspace()_ intervalnya ditetapkan dari $-\pi$ hingga $\pi$

					
					import matplotlib.pyplot as plt
					import numpy as np

					# 100 linearly spaced numbers
					x = np.linspace(-np.pi,np.pi,100)

					# the function, which is y = sin(x) here
					y = np.sin(x)

					# setting the axes at the centre
					fig = plt.figure()
					ax = fig.add_subplot(1, 1, 1)
					ax.spines['left'].set_position('center')
					ax.spines['bottom'].set_position('center')
					ax.spines['right'].set_color('none')
					ax.spines['top'].set_color('none')
					ax.xaxis.set_ticks_position('bottom')
					ax.yaxis.set_ticks_position('left')

					# plot the function
					plt.plot(x,y, 'b')

					# show the plot
					plt.show()
					
				
_
Bagaimana Anda memplot grafik yx dengan python?

Mari kita plot bersama dengan dua fungsi lainnya, $y=2\text{sin}(x)$ dan $y=3\text{sin}(x)$. Kali ini, kami memberi label fungsi

					
					import matplotlib.pyplot as plt
					import numpy as np

					# 100 linearly spaced numbers
					x = np.linspace(-np.pi,np.pi,100)

					# the function, which is y = sin(x) here
					y = np.sin(x)

					# setting the axes at the centre
					fig = plt.figure()
					ax = fig.add_subplot(1, 1, 1)
					ax.spines['left'].set_position('center')
					ax.spines['bottom'].set_position('center')
					ax.spines['right'].set_color('none')
					ax.spines['top'].set_color('none')
					ax.xaxis.set_ticks_position('bottom')
					ax.yaxis.set_ticks_position('left')

					# plot the functions
					plt.plot(x,y, 'b', label='y=sin(x)')
					plt.plot(x,2*y, 'c', label='y=2sin(x)')
					plt.plot(x,3*y, 'r', label='y=3sin(x)')

					plt.legend(loc='upper left')

					# show the plot
					plt.show()
					
				
Bagaimana Anda memplot grafik yx dengan python?

Dan di sini kita memplot bersama $y=\text{sin}(x)$ dan $y=\text{cos}(x)$ pada interval yang sama $-\pi$ ke $\pi$

					
					import matplotlib.pyplot as plt
					import numpy as np

					# 100 linearly spaced numbers
					x = np.linspace(-np.pi,np.pi,100)

					# the functions, which are y = sin(x) and z = cos(x) here
					y = np.sin(x)
					z = np.cos(x)

					# setting the axes at the centre
					fig = plt.figure()
					ax = fig.add_subplot(1, 1, 1)
					ax.spines['left'].set_position('center')
					ax.spines['bottom'].set_position('center')
					ax.spines['right'].set_color('none')
					ax.spines['top'].set_color('none')
					ax.xaxis.set_ticks_position('bottom')
					ax.yaxis.set_ticks_position('left')

					# plot the functions
					plt.plot(x,y, 'c', label='y=sin(x)')
					plt.plot(x,z, 'm', label='y=cos(x)')

					plt.legend(loc='upper left')

					# show the plot
					plt.show()
					
				
_
Bagaimana Anda memplot grafik yx dengan python?

Fungsi eksponensial

Fungsi eksponensial $y=e^{x}$ tidak akan pernah memiliki nilai negatif untuk nilai $x$. Jadi kami memindahkan sumbu $x$ ke bawah lagi dengan menyetel ax.spines['bottom'] ke

					
						import matplotlib.pyplot as plt
						import numpy as np

						# 100 linearly spaced numbers
						x = np.linspace(-5,5,100) 

						# the function, which is y = x^3 here
						y = x**3

						# setting the axes at the centre
						fig = plt.figure()
						ax = fig.add_subplot(1, 1, 1)
						ax.spines['left'].set_position('center')
						ax.spines['bottom'].set_position('center')
						ax.spines['right'].set_color('none')
						ax.spines['top'].set_color('none')
						ax.xaxis.set_ticks_position('bottom')
						ax.yaxis.set_ticks_position('left')

						# plot the function
						plt.plot(x,y, 'g') 

						# show the plot
						plt.show()
					
				
0. Kami memplotnya selama interval $-2$ hingga $2$