可视化代码练习:matplotlib

    xiaoxiao2022-07-01  107

    import numpy as np import pandas as pd import matplotlib.pyplot as plt plt.close('all') ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/2/2000', periods=1000)) ts = ts.cumsum() ts.plot() df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD')) df = df.cumsum() plt.figure() df.plot() df3 = pd.DataFrame(np.random.randn(1000, 2), columns=list('BC')).cumsum() df3['A'] = pd.Series(list(range(len(df)))) df3.plot(x='A', y='B') plt.figure() df.iloc[5].plot(kind='bar') plt.figure() df.iloc[5].plot.bar() df.iloc[5].plot.area() df.iloc[5].plot.barh() df.iloc[5].plot.box() df.iloc[5].plot.density() df.iloc[5].plot.hist() df.iloc[5].plot.kde() df.iloc[5].plot.line() df.iloc[5].plot.scatter() df.iloc[5].plot.bar() plt.axhline(0, color='k') df2 = pd.DataFrame(np.random.rand(10, 4), columns=list('abcd')) df2.plot.bar() df2.plot.bar(stacked=True) df2.plot.barh() df2.plot.barh(stacked=True) df4 = pd.DataFrame({'a':np.random.randn(1000) + 1, 'b':np.random.randn(1000), 'c':np.random.randn(1000) },columns=list('abcd')) plt.figure() df4.plot.hist() df4.plot.hist(alpha=0.5) df4.plot.hist(stacked=True, bins=20) df4.plot.hist(stacked=True) df4.plot.hist(stacked=True, bins=20) df4['a'].plot.hist(orientation='horizontal', cumulative=True) df4['a'].plot.hist() df4['a'].plot.hist(orientation='horizontal') df['A'].diff().hist() df['A'] df['A'].diff() df.diff().hist(color='k', alpha=0.5, bins=50) data = pd.Series(np.random.randn(1000)) data.hist(by=np.random.randint(0, 4, 1000), figsize=(6, 4)) data.hist(by=np.random.randint(0, 4, 1000), figsize=(6, 4), alpha=0.5) df = pd.DataFrame(np.random.rand(10, 5), columns=list('ABCDE')) df.plot.box() color = {'boxes':'DarkGreen', 'whiskers':'DarkOrange', 'medians': 'DarkBlue', 'caps':'Gray'} df.plot.box(color=color, sym='r+') df.plot.box() df.plot.box(vert=False) df.plot.box(vert=False, positions=[1, 4, 5, 6, 8]) df = pd.DataFrame(np.random.rand(10, 5)) plt.figure() bp = df.boxplot() df = pd.DataFrame(np.random.rand(10, 2), columns=['Col1', 'Col2']) df['X'] = pd.Series(list('AAAAABBBBB')) plt.figure() bp = df.boxplot() bp = df.boxplot(by='X') df = pd.DataFrame(np.random.rand(10, 3), columns=['Col1', 'Col2', 'Col3']) df['X'] = pd.Series(list('AAAAABBBBB')) df['Y'] = pd.Series(list('ABABABABAB')) plt.figure() bp = df.boxplot(column=['Col1', 'Col2'], by=list('XY')) np.random.seed(1234) df_box = pd.DataFrame(np.random.randn(50, 2)) df_box['g'] = np.random.choice(list('AB'), size=50) df_box.loc[df_box['g'] == 'B', 1] += 3 bp = df_box.boxplot(by='g') bp = df_box.groupby('g').boxplot() df = pd.DataFrame(np.random.rand(10, 4), columns=list('abcd')) df.plot.area() df.plot.area(stacked=False) df.plot.area(stacked=False, alpha=0.5) df = pd.DataFrame(np.random.rand(50, 4), columns=list('abcd')) df.plot.scatter(x='a', y='b') ax = df.plot.scatter(x='a', y='b', color='DarkBlue', label='Group 1') df.plot.scatter(x='c', y='d', color='DarkGreen', label='Group 2', ax=ax) df.plot.scatter(x='a', y='b', c='c', s=50) df.plot.scatter(x='a', y='b', s=df['c']*2000) df = pd.DataFrame(np.random.randn(1000, 2), columns=list('ab')) df['b'] = df['b'] + np.arange(1000) df.plot.hexbin(x='a', y='b', gridsize=25) df = pd.DataFrame(np.random.randn(1000, 2), columns=list('ab')) df['b'] = df['b'] + np.arange(1000) df['z'] = np.random.uniform(0, 3, 1000) df.plot.hexbin(x='a', y='b', C='z', reduce_C_function=np.max, gridsize=25) series = pd.Series(3 * np.random.rand(4), index=list('abcd'), name='series') series.plot.pie(figsize=(6, 6)) df = pd.DataFrame(3 * np.random.rand(4, 2), index=list('abcd'), columns=list('xy')) df.plot.pie(subplots=True, figsize=(8, 4))
    最新回复(0)