自学气象人 发表于 2024-3-14 09:28:45

50个常用统计图表代码总结(7)

Composition
31. Waffle Chart
The waffle chart can be created using the pywaffle package and is used to show the compositions of groups in a larger population.
#! pip install pywaffle
# Reference: <a href="https://stackoverflow.com/questions/41400136/how-to-do-waffle-charts-in-python-square-piechart" target="_blank">https://stackoverflow.com/questions/41400136/how-to-do-waffle-charts-in-python-square-piechart</a>
from pywaffle importWaffle

# Import
df_raw = pd.read_csv("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")

# Prepare Data
df = df_raw.groupby('class').size().reset_index(name='counts')
n_categories = df.shape
colors =

# Draw Plot and Decorate
fig = plt.figure(
    FigureClass=Waffle,
    plots={
      '111':{
            'values': df['counts'],
            'labels':["{0} ({1})".format(n, n)for n in df[['class','counts']].itertuples()],
            'legend':{'loc':'upper left','bbox_to_anchor':(1.05,1),'fontsize':12},
            'title':{'label':'# Vehicles by Class','loc':'center','fontsize':18}
      },
    },
    rows=7,
    colors=colors,
    figsize=(16,9)
)



#! pip install pywaffle
from pywaffle importWaffle

# Import
# df_raw = pd.read_csv("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")

# Prepare Data
# By Class Data
df_class = df_raw.groupby('class').size().reset_index(name='counts_class')
n_categories = df_class.shape
colors_class =

# By Cylinders Data
df_cyl = df_raw.groupby('cyl').size().reset_index(name='counts_cyl')
n_categories = df_cyl.shape
colors_cyl =

# By Make Data
df_make = df_raw.groupby('manufacturer').size().reset_index(name='counts_make')
n_categories = df_make.shape
colors_make =


# Draw Plot and Decorate
fig = plt.figure(
    FigureClass=Waffle,
    plots={
      '311':{
            'values': df_class['counts_class'],
            'labels':["{1}".format(n, n)for n in df_class[['class','counts_class']].itertuples()],
            'legend':{'loc':'upper left','bbox_to_anchor':(1.05,1),'fontsize':12,'title':'Class'},
            'title':{'label':'# Vehicles by Class','loc':'center','fontsize':18},
            'colors': colors_class
      },
      '312':{
            'values': df_cyl['counts_cyl'],
            'labels':["{1}".format(n, n)for n in df_cyl[['cyl','counts_cyl']].itertuples()],
            'legend':{'loc':'upper left','bbox_to_anchor':(1.05,1),'fontsize':12,'title':'Cyl'},
            'title':{'label':'# Vehicles by Cyl','loc':'center','fontsize':18},
            'colors': colors_cyl
      },
      '313':{
            'values': df_make['counts_make'],
            'labels':["{1}".format(n, n)for n in df_make[['manufacturer','counts_make']].itertuples()],
            'legend':{'loc':'upper left','bbox_to_anchor':(1.05,1),'fontsize':12,'title':'Manufacturer'},
            'title':{'label':'# Vehicles by Make','loc':'center','fontsize':18},
            'colors': colors_make
      }
    },
    rows=9,
    figsize=(16,14)
)



32. Pie Chart
Pie chart is a classic way to show the composition of groups. However, its not generally advisable to use nowadays because the area of the pie portions can sometimes become misleading. So, if you are to use pie chart, its highly recommended to explicitly write down the percentage or numbers for each portion of the pie.
# Import
df_raw = pd.read_csv("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")

# Prepare Data
df = df_raw.groupby('class').size()

# Make the plot with pandas
df.plot(kind='pie', subplots=True, figsize=(8,8), dpi=80)
plt.title("Pie Chart of Vehicle Class - Bad")
plt.ylabel("")
plt.show()



# Import
df_raw = pd.read_csv("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")

# Prepare Data
df = df_raw.groupby('class').size().reset_index(name='counts')

# Draw Plot
fig, ax = plt.subplots(figsize=(12,7), subplot_kw=dict(aspect="equal"), dpi=80)

data = df['counts']
categories = df['class']
explode =

def func(pct, allvals):
    absolute = int(pct/100.*np.sum(allvals))
    return"{:.1f}% ({:d} )".format(pct, absolute)

wedges, texts, autotexts = ax.pie(data,
                                  autopct=lambda pct: func(pct, data),
                                  textprops=dict(color="w"),
                                  colors=plt.cm.Dark2.colors,
                                 startangle=140,
                                 explode=explode)

# Decoration
ax.legend(wedges, categories, title="Vehicle Class", loc="center left", bbox_to_anchor=(1,0,0.5,1))
plt.setp(autotexts, size=10, weight=700)
ax.set_title("Class of Vehicles: Pie Chart")
plt.show()




33. Treemap
Tree map is similar to a pie chart and it does a better work without misleading the contributions by each group.
# pip install squarify
import squarify

# Import Data
df_raw = pd.read_csv("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")

# Prepare Data
df = df_raw.groupby('class').size().reset_index(name='counts')
labels = df.apply(lambda x: str(x)+"\n ("+ str(x)+")", axis=1)
sizes = df['counts'].values.tolist()
colors =

# Draw Plot
plt.figure(figsize=(12,8), dpi=80)
squarify.plot(sizes=sizes, label=labels, color=colors, alpha=.8)

# Decorate
plt.title('Treemap of Vechile Class')
plt.axis('off')
plt.show()



34. Bar Chart
Bar chart is a classic way of visualizing items based on counts or any given metric. In below chart, I have used a different color for each item, but you might typically want to pick one color for all items unless you to color them by groups. The color names get stored inside all_colors in the code below. You can change the color of the bars by setting the color parameter in plt.plot().
import random

# Import Data
df_raw = pd.read_csv("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")

# Prepare Data
df = df_raw.groupby('manufacturer').size().reset_index(name='counts')
n = df['manufacturer'].unique().__len__()+1
all_colors = list(plt.cm.colors.cnames.keys())
random.seed(100)
c = random.choices(all_colors, k=n)

# Plot Bars
plt.figure(figsize=(16,10), dpi=80)
plt.bar(df['manufacturer'], df['counts'], color=c, width=.5)
for i, val in enumerate(df['counts'].values):
    plt.text(i, val, float(val), horizontalalignment='center', verticalalignment='bottom', fontdict={'fontweight':500,'size':12})

# Decoration
plt.gca().set_xticklabels(df['manufacturer'], rotation=60, horizontalalignment='right')
plt.title("Number of Vehicles by Manaufacturers", fontsize=22)
plt.ylabel('# Vehicles')
plt.ylim(0,45)
plt.show()



Change
35. Time Series Plot
Time series plot is used to visualise how a given metric changes over time. Here you can see how the Air Passenger traffic changed between 1949 and 1969. Check this free video tutorial on how to implement line plots for analyzing time series.
# Import Data
df = pd.read_csv('https://github.com/selva86/datasets/raw/master/AirPassengers.csv')

# Draw Plot
plt.figure(figsize=(16,10), dpi=80)
plt.plot('date','traffic', data=df, color='tab:red')

# Decoration
plt.ylim(50,750)
xtick_location = df.index.tolist()[::12]
xtick_labels =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 borders
plt.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()




文章来源于微信公众号:自学气象人

FrankJScott 发表于 2024-9-4 01:40:59

Updated Tajir4D Login Tips

In response to the guy talking about toto judi 4d login, 4d online login, t0gel online, cara main 4d singapore, 4d singapore login, bandar indotogel, bandar judi togel 4d, 4d malaysia online, toto 4ds, toto 4d 4d,I highly suggest this recommended TAJIR4D info or deposit dewatogel, bandar judi terpercaya, agen slot4d, toto 4d 4d, 5505 4d, bandar 4d slot, 4d singapore slot, judi 4d slot, judi hongkong online, dewatogel singapore, as well as this this guy about TAJIR4D link on top of cara toto, judi toto, bandar indotogel, bandar judi togel 4d, judi toto slot, all toto 4d, on 4d toto, 5505 4d, bandar togel bonus deposit 100, bandar macau, and don't forget this additional reading about TAJIR4D url which is also great. Also, have a look at this find out more about Tajir4D Login info not to mention singapore 4d slot, toto judi slot, bandar judi terpercaya di indonesia, judi 4d slot, bandar judi hongkong, judi toto, 2d toto, 4d 4d toto, deposit dewatogel, slottogel login, alongside all this recommended TAJIR4D advice with http dewatogel net, 100 4d, toto 4d, bandar judi terpercaya, master wap, bandar slot 4d, 4d online, toto 2d, bandar macau, www 4d net m,for good measure. Check more @ Best Tajir4D Login Tips 9c2cdec
页: [1]
查看完整版本: 50个常用统计图表代码总结(7)