Sumber: Babi Terbang Gunung Salju
Artikel ini adalah tentang 2800 kata , Dianjurkan untuk membaca 10 menit
Artikel ini akan berbagi dengan Anda 16 tip praktis untuk plotting matplotlib.
Label: Analisis Data
1. Tambahkan judul-judul
import numpy as npimport matplotlib.pyplot as plt # Display Chinese plt.rcParams = plt.rcParams = False% matplotlib inlinex = np.arange (0,10) plt.title ('This is an example title') plt.plot (x , x * x) plt.show ()Keluaran:
2. Tambahkan teks-teks
Cukup atur koordinat dan teksnya
impor numpy sebagai npimport matplotlib.pyplot sebagai plt # Tampilkan Cina plt.rcParams = plt.rcParams = Salah% matplotlib inlinex = np.arange (-10,11,1) y = x * xplt.plot (x, y) plt. title ('Ini adalah contoh judul') # Tambahkan teks plt.text (-2.5,30, 'function y = x * x') plt.show ()Keluaran:
3. Tambahkan anotasi-anotasi
- xy: Titik koordinat untuk komentar
- xytext: Koordinat teks komentar (defaultnya adalah posisi xy)
- panah: Gambarlah panah antara xy dan xytext
Keluaran:
4. Set nama sumbu-xlabel / ylabel
impor numpy sebagai npimport matplotlib.pyplot sebagai plt # Display Chinese plt.rcParams = plt.rcParams = Salah% matplotlib inlinex = np.arange (1,20) plt.xlabel ('example x axis') plt.ylabel ('example y Axis ') plt.plot (x, x * x) plt.show ()Keluaran:
5. Tambahkan legenda-legenda
impor numpy sebagai npimport matplotlib.pyplot sebagai plt # Tampilkan Cina plt.rcParams = plt.rcParams = Salah% matplotlib inlineplt.plot (x, x) plt.plot (x, x * 2) plt.plot (x, x * 3 ) plt.plot (x, x * 4) # Langsung berikan legendplt.legend () plt.show ()Keluaran:
6. Sesuaikan warna-warna
Lulus parameter warna, dukung metode berikut
import numpy as np import matplotlib.pyplot as plt% matplotlib inlinex = np.arange (1,5) #Beberapa cara color plt.plot (x, color = 'g') plt.plot (x + 1, color = ' 0,5 ') plt.plot (x + 2, color =' # FF00FF ') plt.plot (x + 3, color = (0.1,0.2,0.3)) plt.show ()Keluaran:
7. Ganti penanda gaya garis
impor numpy sebagai np import matplotlib.pyplot sebagai plt% matplotlib inlinex = np.arange (1,5) plt.plot (x, marker = 'o') plt.plot (x + 1, marker = ' > ') plt.plot (x + 2, marker =' s ') plt.show ()Keluaran:
8. Menampilkan rumus-rumus matematika
Formatnya adalah sebagai berikut: \ omega $, simbol dalam rumus akan diurai di tengah
impor numpy sebagai np impor matplotlib.pyplot sebagai plt% matplotlib inlineplt.title ('chenqionghe') plt.xlim () plt.ylim () plt.text (2,4, r '$ \ alpha \ beta \ pi \ lambda \ omega $ ', size = 25) plt.text (4,4, r' $ \ sin (0) = \ cos (\ frac {\ pi) {2}) $ ', size = 25) plt.text (2, 2, r '$ \ lim_ {x \ rightarrow y} \ frac {1} {x ^ 3} $', size = 25) plt.text (4,2, r '$ \ sqrt {x} = \ sqrt { y) $ ', size = 25) plt.show ()Keluaran:
9. Tampilkan grid-grid
import numpy as np import matplotlib.pyplot as plt% matplotlib inlinex = 'a', 'b', 'c', 'd'y = plt.grid () # Anda juga bisa menyetel warna, lebar garis, dan gaya garis # plt.grid (color = 'g', linewidth = '1', linestyle = '-.') plt.plot (x, y) plt.show ()Keluaran:
10. Sesuaikan axis scale-locator_params
- Sesuaikan sumbu x dan sumbu y secara bersamaan: plt.locator_params (nbins = 20);
- Sesuaikan hanya sumbu x: plt.locator_params ('x', nbins = 20);
- Sesuaikan hanya sumbu y: plt.locator_params ('y', nbins = 20).
Keluaran:
11. Sesuaikan rentang sumbu-sumbu / xlim / ylim
- sumbu: , X dari 0 sampai 5, y dari 0 sampai 10
- xlim: Parameter yang sesuai adalah xmin dan xmax, yang masing-masing dapat menyesuaikan nilai maksimum dan minimum
- ylim: Sama seperti penggunaan xlim
Keluaran:
12. Sesuaikan tanggal adaptive-autofmt_xdate
Terkadang tanggal yang ditampilkan akan tumpang tindih, yang sangat tidak bersahabat. Memanggil plt.gcf (). Autofmt_xdate () akan secara otomatis menyesuaikan sudut
impor numpy sebagai npimport pandas sebagai pdimport matplotlib.pyplot sebagai plt% matplotlib inlinex = pd.date_range ('2020/01/01', periode = 30) y = np.arange (0,30,1) plt.plot (x, y) plt.gcf (). autofmt_xdate () plt.show ()Keluaran:
13. Tambahkan sumbu ganda-twinx
impor numpy sebagai npimport matplotlib.pyplot sebagai plt% matplotlib inlinex = np.arange (1,20) y1 = x * xy2 = np.log (x) plt.plot (x, y1) # Tambahkan sumbu, default 0 ke 1plt.twinx () plt.plot (x, y2, 'r') plt.show ()Keluaran:
14. Isi area-fill / fill_beween
mengisi area fungsi isian
import numpy as np import matplotlib.pyplot as plt # Display Chinese plt.rcParams = plt.rcParams = False% matplotlib inlinex = np.linspace (0,5 * np.pi, 1000) y1 = np.sin (x) y2 = np .sin (2 * x) plt.plot (x, y1) plt.plot (x, y2) # Isi plt.fill (x, y1, 'g') plt.fill (x, y2, 'r') plt .title ('Ini adalah contoh judul') plt.show ()Keluaran:
fill_beween mengisi area persimpangan fungsi
import numpy as npimport matplotlib.pyplot as plt # Display Chinese plt.rcParams = plt.rcParams = False% matplotlib inlineplt.title ('Ini adalah contoh judul') x = np.linspace (0,5 * np.pi, 1000 ) y1 = np.sin (x) y2 = np.sin (2 * x) plt.plot (x, y1) plt.plot (x, y2) # Isi plt.fill_between (x, y1, y2, dimana = y1 > y2, interpolate = True) plt.show ()Keluaran:
15. Gambarlah bentuk-matplotlib.patche yang terisi
impor numpy sebagai npimport matplotlib.pyplot sebagai pltimport matplotlib.patches sebagai mptaches% matplotlib inlinexy1 = np.array () xy2 = np.array () xy3 = np.array () xy4 = np.array () ara, ax = plt. subplots () # circle, tentukan koordinat dan radius circle = mptaches.Circle (xy1,0.15) ax.add_patch (circle) #rectangle rect = mptaches.Rectangle (xy2,0.2,0.1, color = 'r') ax.add_patch (rect) #polygon polygon = mptaches.RegularPolygon (xy3,6,0.1, color = 'g') ax.add_patch (polygon) # ellipse = mptaches.Ellipse (xy4,0.4,0.2, color = 'c') ax .add_patch (elips) ax.axis ('equal') plt.show ()Keluaran:
16. Ganti style-plt.style.use
Matplotlib mendukung berbagai gaya, Anda dapat beralih gaya melalui plt.style.use, misalnya: plt.style.use ('ggplot') masukan plt.style.available untuk melihat semua gaya:
impor matplotlib.pyplot sebagai pltplt.style.availableKeluaran:
Kode contoh, gaya ggplot:
impor numpy sebagai npimport matplotlib.pyplot sebagai pltimport matplotlib.patches sebagai mptaches% matplotlib inlineplt.style.use ('ggplot') # Buat 4 subplot baru fig, axes = plt.subplots (2,2) ax1, ax2, ax3, ax4 = axes.ravel () # Grafik pertama x, y = np.random.normal (size = (2,100)) ax1.plot (x, y, 'o') # Grafik kedua x = np.arange ( 0,10) y = np.arange (0,10) colors = plt.rcParamslength = np.linspace (0,10, len (colors)) untuk panjang s: ax2.plot (x, y + s, '- ') # Grafik ketiga x = np.arange (5) y1, y2, y3 = np.random.randint (1,25, size = (3,5)) width = 0.25ax3.bar (x, y1, lebar) ax3.bar (x + lebar, y2, lebar) ax3.bar (x + 2 * lebar, y3, lebar) # Gambar keempat untuk i, warna dalam enumerate (warna): xy = np.random.normal (size = 2) ax4.add_patch (plt.Circle (xy, radius = 0.3, color = color)) ax4.axis ('equal') plt.show ()Keluaran:
Alamat asli: https://www.cnblogs.com/chenqionghe/
Editor: Wang Jing
Proofreading: Lin Yilin
-Selesai-
Ikuti platform publik WeChat resmi dari Institut Ilmu Data Tsinghua-Qingdao " Pai Data AI "Dan nomor saudara perempuan" Data Pie THU "Dapatkan lebih banyak manfaat kuliah dan konten berkualitas.
- Tonggak sejarah yang "menyeramkan": AI ilmuwan China menerjemahkan gelombang otak dengan akurasi 97%
- Di mana menemukan data penelitian mahkota baru? Tempat yang harus dilihat untuk pekerja penelitian ilmiah (dengan tautan)
- Menggunakan gerbang logika protein untuk mengubah sel menjadi komputer, cendekiawan muda Tiongkok belajar tentang Sains
- Mesin terjemahan pertama di dunia telah kembali dari evolusi, dan "detail gila" menangani dialek China klasik
- Tim AI Tsinghua meluncurkan platform keamanan AI untuk menipu algoritme bagian atas dan kemudian memperbaiki kerentanan dengan kuat
- Robot kecil membantu mengambil! Taman Sains dan Teknologi Zhongguancun Dongsheng, "senjata pencegahan" ini membantu melanjutkan pekerjaan dan produksi