How to use Matplotlib to draw a variety of common graphics in python
Today, I would like to share with you how python uses Matplotlib to draw a variety of common graphics, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.
Import numpy as np import matplotlib.pyplot as plt%matplotlib inline # can write this without writing plt.show () plt.rcParams ['font.sans-serif'] = [' SimHei'] # to display the Chinese label plt.rcParams ['axes.unicode_minus'] = False # to display the negative sign X = np.linspace (0, 2*np.pi) normally 100) # evenly divided data Y = np.sin (X) Y1 = np.cos (X) plt.title ("Hello Worldwide!") plt.plot (XMagi Y) plt.plot (XMagi Y1)
X = np.linspace, Y = np.sin (X) Y1 = np.cos (X) plt.subplot (211) # is equivalent to subplot (2) 1) # one printmaking two pictures plt.plot (XMague Y) plt.subplot (212) plt.plot (XMagne Y1)
Histogram data = [5, 25, 50, 20] plt.bar (range (len (data)), data)
Draw a bar chart horizontally data = [5 range (len (data)), data)
Multiple bar graphs data = [5LECHEROGRAPHY 25LECHERING 50], [4LECHERE 23WEI 51LECHERY 17], [6WEI 22Eng 52JE19]] X = np.arange (4) plt.bar (X + 0.00, data [0], color = 'baked, width = 0.25pint label = "A") plt.bar (X + 0.25, data [1], color =' glitter, width = 0.25) plt.bar (X + 0.50, data [2], color = 'ritual, width = 0.25) Label = "C") # display the lableplt.legend () set above
Superimposed bar chart data = [5LECHERE 25LECHERY 50], [4LECHERE 23 pint 51JEI 17], [6LJ 22nd 52jue 19]] X = np.arange (4) plt.bar (X, data [0], color = 'baked, width = 0.25) plt.bar (X, data [1], color =' gagged, width = 0.25 bottom = data [0]) plt.bar (X, data [2], color = 'rpm, width = 0.25) Bottom = np.array (data [0]) + np.array (data [1]) plt.show ()
Scatter plot N = 50x = np.random.rand (N) y = np.random.rand (N) plt.scatter (x, y)
Bubble chart N = 50x = np.random.rand (N) y = np.random.rand (N) colors = np.random.randn (N) # Color can be expressed numerically as area = np.pi * (15 * np.random.rand (N)) * * 2 # resize plt.scatter (x, y, c=colors, alpha=0.5, s = area)
N = 50x = np.random.rand (N) y = np.random.rand (N) colors = np.random.randint (0meme 2) plt.scatter (x, y, c=colors, alpha=0.5,s = area)
Histogram a = np.random.rand (100,100) plt.hist (a dint bins = 20) plt.ylim (0mem15)
A = np.random.randn (10000) plt.hist (a dint binsals 50) plt.title ("standard normal distribution")
Box diagram x = np.random.randint (20Power100 plt.ylim size = (30mage3)) plt.boxplot (x) plt.ylim (0120) # where to fill in a label on the x axis. Here we make it in the position of 1meme, 2meme and 3, and write "A np.median" Cplt.xticks ([1mme2) xticks], ['Achieve2]]) plt.hlines (y = np.median (xQuery axis = 0) [0], xmin = 0mXmax])
Add a text description # set the canvas color to bluefig, ax = plt.subplots (facecolor='blue') # y-axis data data = [[5Ling 25pje 50pje 20], [4Ling 23 Ling 51J 17], [6LJ 22Ling 52J 19] X = np.arange (4) plt.bar (x = 0.00, data [0], color=' darkorange', width = 0.25herb label ='A') plt.bar (x = 0.25, data [1], color=' steelblue', width = 0.25) Label= "B") plt.bar (Xray 0.50, data [2], color = 'violet', width = 0.25 ax.set_title =' C') ax.set_title ("Figure 2") plt.legend () # add text description method-W = [0.000f 0.25pr 0.50] for i in range (3): for an in zip b in zip (Xray W [I], data [I]): plt.text (afield b, "% .0f"% BTH = "center") Va= "bottom") plt.xlabel ("Group") plt.ylabel ("Num") plt.text (0.0 48, "TEXT")
Add text description method 2: X = np.linspace (0,2cm np.pippag100) # uniform partition data Y = np.sin (X) Y1 = np.cos (X) plt.plot (XPowerY) plt.plot (XPowerY1) plt.annotate ('Points', xy= (1, np.sin (1)), xytext= (2,0.5), fontsize=16, arrowprops=dict (arrow)) plt.title ("this is a test chart!")
Multiple graphics subplots%pylab inlinepylab.rcParams ['figure.figsize'] = (10,6) # resize the picture # np.random.seed (19680801) n_bins = 10x = np.random.randn (1000, 3) fig, axes = plt.subplots (nrows=2, ncols=2) ax0, ax1, ax2, ax3 = axes.flatten () colors = [' red', 'tan',' lime'] ax0.hist (x, n_bins, normed=1, histtype='bar', color=colors Label=colors) ax0.legend (prop= {'size': 10}) ax0.set_title (' bars with legend') ax1.hist (x, n_bins, normed=1, histtype='bar', stacked=True) ax1.set_title ('stacked bar') ax2.hist (x, n_bins, histtype='step', stacked=True, fill=False) ax2.set_title (' stack step (unfilled)') # Make a multiple-histogram of data-sets with different length.x_multi = [np.random.randn (n) for n in [10000, 5000] 2000]] ax3.hist (x_multi, n_bins, histtype='bar') ax3.set_title ('different sample sizes')
Use Pandas to draw import pandas as pddf = pd.DataFrame (np.random.rand (50,2), columns= ['axiom,' b']) # scatter plot df.plot.scatter (xerographic, yawb')
Df = pd.DataFrame (np.random.rand (10jue 4), columns= ['axiajiaoyuzhuo']) # draw histogram df.plot.bar ()
# stacked bar chart df.plot.bar (stacked=True)
# horizontal bar chart df.plot.barh (stacked=True)
Df = pd.DataFrame ({'a':np.random.randn (1000) + 1). Randn (1000),' c':np.random.randn (1000)-1}, columns= ['await,' baked,'c']) # histogram df.plot.hist (bins=20)
# Box diagram df = pd.DataFrame (np.random.rand (10,5), columns= ['A','B','C','D','E']) df.plot.box ()
These are all the contents of the article "how python uses Matplotlib to draw a variety of common graphics". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.