In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what are the commonly used Matplotlib diagrams in Python", the content is simple and clear, and I hope it can help you solve your doubts. Here, let the editor lead you to study and learn what Matplotlib diagrams are commonly used in Python.
Bar chart
The bar chart effectively conveys the ranking order of projects. However, by adding the value of the metric at the top of the chart, the user can obtain accurate information from the chart itself.
# Prepare Datadf_raw = pd.read_csv ("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")df = df_raw [['cty',' manufacturer']] .groupby ('manufacturer') .apply (lambda x: x.mean () df.sort_values (' cty', inplace=True) df.reset_index (inplace=True) # Draw plotimport matplotlib.patches as patchesfig, ax = plt.subplots (figsize= (16d10), facecolor='white', dpi= 80) ax.vlines (x=df.index, ymin=0) Ymax=df.cty, color='firebrick', alpha=0.7, linewidth=20) # Annotate Textfor i, cty in enumerate (df.cty): ax.text (I, cty+0.5, round (cty, 1), horizontalalignment='center') # Title, Label, Ticks and Ylimax.set_title ('Bar Chart for Highway Mileage', fontdict= {' size':22}) ax.set (ylabel='Miles Per Gallon', ylim= (0,30)) plt.xticks (df.index, df.manufacturer.str.upper (), rotation=60, horizontalalignment='right' Fontsize=12) # Add patches to color the X axis labelsp1 = patches.Rectangle ((.57,-0.005), width=.33, height=.13, alpha=.1, facecolor='green', transform=fig.transFigure) p2 = patches.Rectangle ((.124,-0.005), width=.446, height=.13, alpha=.1, facecolor='red', transform=fig.transFigure) fig.add_artist (p1) fig.add_artist (p2) plt.show ()
Lollipop chart
Lollipop charts provide purposes similar to ordered bar charts in a visually pleasing way.
# Prepare Datadf_raw = pd.read_csv ("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")df = df_raw [['cty',' manufacturer']] .groupby ('manufacturer') .apply (lambda x: x.mean () df.sort_values (' cty', inplace=True) df.reset_index (inplace=True) # Draw plotfig, ax= plt.subplots (figsize= (1610), dpi= 80) ax.vlines (x=df.index, ymin=0, ymax=df.cty, color='firebrick') Alpha=0.7, linewidth=2) ax.scatter (x=df.index, y=df.cty, Song75, color='firebrick', alpha=0.7) # Title, Label, Ticks and Ylimax.set_title ('Lollipop Chart for Highway Mileage', fontdict= {' size':22}) ax.set_ylabel ('Miles Per Gallon') ax.set_xticks (df.index) ax.set_xticklabels (df.manufacturer.str.upper (), rotation=60, fontdict= {' horizontalalignment': 'right',' size':12}) ax.set_ylim (0 30) # Annotatefor row in df.itertuples (): ax.text (row.Index, row.cty+.5, s=round (row.cty, 2), horizontalalignment= 'center', verticalalignment='bottom', fontsize=14) plt.show ()
Histogram of continuous variables
The histogram shows the frequency distribution of a given variable. The following representation groups frequency bars based on classified variables to better understand continuous and concatenated variables.
# Import Datadf = pd.read_csv ("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")# Prepare datax_var = 'displ'groupby_var =' class'df_agg = df.loc [:, [x_var, groupby_var]] .groupby (groupby_var) vals = [DF [x _ var] .values.tolist () for I, df in df_agg] # Drawplt.figure (figsize= (16Power9) Dpi= 80) colors = [plt.cm.Spectral (i/float (len (vals)-1) for i in range (len (vals))] n, bins, patches = plt.hist (vals, 30, stacked=True, density=False, color=colors [: len (vals)]) # Decorationplt.legend ({group:col for group, col in zip (np.unique (df [groupby _ var]). Tolist (), colors [: len (vals)]}) plt.title (f "Stacked Histogram of ${x_var} $colored by ${groupby_var} $" Fontsize=22) plt.xlabel (x_var) plt.ylabel ("Frequency") plt.ylim (0,25) plt.xticks (ticks=bins [:: 3], labels= [round (BL1) for bin bins [:: 3]]) plt.show ()
Histogram of classified variables
The histogram of the classified variable shows the frequency distribution of the variable. By shading the bar chart, you can associate the distribution with another classification variable that represents the color.
# Import Datadf = pd.read_csv ("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")# Prepare datax_var = 'manufacturer'groupby_var =' class'df_agg = df.loc [:, [x_var, groupby_var]] .groupby (groupby_var) vals = [DF [x _ var] .values.tolist () for I, df in df_agg] # Drawplt.figure (figsize= (16Power9) Dpi= 80) colors = [plt.cm.Spectral (i/float (len (vals)-1) for i in range (len (vals))] n, bins, patches = plt.hist (vals, DF [x _ var] .unique (). _ len__ (), stacked=True, density=False, color=colors [: len (vals)]) # Decorationplt.legend ({group:col for group, col in zip (np.unique (DF [groupby _ var]). Tolist () Colors [: len (vals)]) plt.title (f "Stacked Histogram of ${x_var} $colored by ${groupby_var} $", fontsize=22) plt.xlabel (x_var) plt.ylabel ("Frequency") plt.ylim (0,40) plt.xticks (ticks=bins, labels=np.unique (DF [x _ var]). Tolist (), rotation=90, horizontalalignment='left') plt.show ()
Scatter plot
Scatteplot is a classical and basic diagram used to study the relationship between two variables. If there are multiple groups in the data, you may need to visualize each group in a different color. In Matplotlib, you can use it easily.
# Import dataset midwest = pd.read_csv ("https://raw.githubusercontent.com/selva86/datasets/master/midwest_filter.csv")# Prepare Data # Create as many colors as there are unique midwest ['category'] categories = np.unique (midwest [' category']) colors = [plt.cm.tab10 (i/float (len (categories)-1) for i in range (len (categories))] # Draw Plot for Each Categoryplt.figure (figsize= (16,10), dpi= 80, facecolor='w', edgecolor='k') for I Category in enumerate (categories): plt.scatter ('area',' poptotal', data=midwest.loc [midwest.category==category,:], c=colors 20, c=colors [I], label=str (category)) # Decorationsplt.gca (). Set (xlim= (0.0,0.1), ylim= (0, 90000), xlabel='Area', ylabel='Population') plt.xticks (fontsize=12) Plt.yticks (fontsize=12) plt.title (Scatterplot of Midwest Area vs Population, fontsize=22) plt.legend (fontsize=12) plt.show ()
Tree view
The tree view combines similar points together according to a given distance measure, and organizes them into tree links according to the similarity of the points.
Import scipy.cluster.hierarchy as shc# Import Datadf = pd.read_csv ('https://raw.githubusercontent.com/selva86/datasets/master/USArrests.csv')# Plotplt.figure (figsize= (16,10), dpi= 80) plt.title ("USArrests Dendograms", fontsize=22) dend = shc.dendrogram (shc.linkage (df [[' Murder', 'Assault',' UrbanPop', 'Rape']], method='ward'), labels=df.State.values, color_threshold=100) plt.xticks (fontsize=12) plt.show ()
Population pyramid
The population pyramid can be used to display the distribution of groups sorted by volume. Alternatively, it can also be used to show the gradual filtering of the population, as it is used below to show how many people are passing through the marketing channel at each stage.
# Read datadf = pd.read_csv ("https://raw.githubusercontent.com/selva86/datasets/master/email_campaign_funnel.csv")# Draw Plotplt.figure (figsize= (13jing10), dpi= 80) group_col = 'Gender'order_of_bars = df.Stage.unique () [::-1] colors = [plt.cm.Spectral (i/float (len (group _ col]. Unique ()-1)) for i in range (len (DF [group _ col]. Unique ())] for c Group in zip (colors, DF [group _ col] .unique ()): sns.barplot (xylene user, yawned stagekeeper, data= df.loco [group _ col] = = group,:], order=order_of_bars, color=c, label=group) # Decorations plt.xlabel ("$Users$") plt.ylabel ("Stage of Purchase") plt.yticks (fontsize=12) plt.title ("Population Pyramid of the Marketing Funnel", fontsize=22) plt.legend () plt.show ()
Pie chart
Pie charts are a classic way to show the composition of a group. However, it is generally not recommended these days because the size of the pie can sometimes be misleading. Therefore, if you want to use a pie chart, it is strongly recommended that you explicitly write down the percentages or numbers of each part of the pie chart.
# Importdf_raw = pd.read_csv ("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")# Prepare Datadf = df_raw.groupby ('class'). Size () # Make the plot with pandasdf.plot (kind='pie', subplots=True, figsize= (8,8), dpi= 80) plt.title (" Pie Chart of Vehicle Class-Bad ") plt.ylabel (") plt.show ()
Time series diagram
Time series diagrams are used to visualize how a given indicator changes over time. Here, you can learn how the air passenger flow changed from 1949 to 1969.
# Import Datadf = pd.read_csv ('https://github.com/selva86/datasets/raw/master/AirPassengers.csv')# Draw Plotplt.figure (figsize= (16jing10), dpi= 80) plt.plot (' date', 'traffic', data=df, color='tab:red') # Decorationplt.ylim (50750) xtick_location = df.index.tolist () [:: 12] xtick_labels = [x [- 4:] for x in df.date.tolist () [: 12]] plt.xticks (ticks=xtick_location, labels=xtick_labels Rotation=0, fontsize=12, horizontalalignment='center', alpha=.7) plt.yticks (fontsize=12, alpha=.7) plt.title ("Air Passengers Traffic (1949-1969)", fontsize=22) plt.grid (axis='both' Alpha=.3) # Remove bordersplt.gca (). Spines ["top"]. Set_alpha (0.0) plt.gca (). Spines ["bottom"]. Set_alpha (0.3) plt.gca (). Spines ["right"]. Set_alpha (0.0) plt.gca (). Spines ["left"]. Set_alpha (0.3) plt.show ()
The area map is not stacked
Unstacked area maps are used to visualize the progress (ups and downs) of two or more series relative to each other. In the chart below, you can clearly see how the personal savings rate decreases as the median time of unemployment increases. The unstacked area map shows this phenomenon very well.
Display code
# Import Datadf = pd.read_csv ("https://github.com/selva86/datasets/raw/master/economics.csv")# Prepare Datax = df ['date'] .values.tolist () y1 = df [' psavert'] .values.tolist () y2 = df ['uempmed'] .values.tolist () mycolors = [' tab:red', 'tab:blue',' tab:green', 'tab:orange',' tab:brown', 'tab:grey',' tab:pink' 'tab:olive'] columns = [' psavert', 'uempmed'] # Draw Plot fig, ax = plt.subplots (1, 1, figsize= (16 dpi= 9), dpi= 80) ax.fill_between (x, y1=y1, y2300, label=columns [1], alpha=0.5, color=mycolors [1], linewidth=2) ax.fill_between (x, y1=y2, y2300, label=columns [0], alpha=0.5, color=mycolors [0], linewidth=2) # Decorationsax.set_title (' Personal Savings Rate vs Median Duration of Unemployment', fontsize=18) ax.set (ylim= [0) 30]) ax.legend (loc='best', fontsize=12) plt.xticks (x [:: 50], fontsize=10, horizontalalignment='center') plt.yticks (np.arange (2.5,30.0,2.5), fontsize=10) plt.xlim (- 10, x [- 1]) # Draw Tick lines for y in np.arange (2.5,30.0,2.5): plt.hlines (y, xmin=0, xmax=len (x), colors='black', alpha=0.3, linestyles= "-" Lw=0.5) # Lighten bordersplt.gca (). Spines ["top"]. Set_alpha (0) plt.gca (). Spines ["bottom"]. Set_alpha (.3) plt.gca (). Spines ["right"]. Set_alpha (0) plt.gca (). Spines ["left"]. Set_alpha (.3) plt.show () all the contents of the article "what Matplotlib diagrams are commonly used in Python" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.