📜  使用 pandas 和 matplotlib 进行不同的绘图

📅  最后修改于: 2022-05-13 01:55:06.259000             🧑  作者: Mango

使用 pandas 和 matplotlib 进行不同的绘图

我们在 matplotlib 库中有不同类型的图,可以帮助我们根据需要制作合适的图。根据给定的数据,我们可以制作很多图表,并且在 pandas 的帮助下,我们可以在绘制数据之前创建一个数据框。让我们使用 Pandas 讨论 matplotlib 中不同类型的绘图。

使用这些命令安装 matplotlib、pandas 和 numpy:

pip install matplotlib
pip install pandas
pip install numpy 

地块类型:

  • 基本绘图:在这个基本绘图中,我们可以使用随机生成的数据使用系列和 matplotlib 绘制图形。
Python3
# import libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
ts = pd.Series(np.random.randn(1000), index = pd.date_range(
                                '1/1/2000', periods = 1000))
ts = ts.cumsum()
ts.plot()
 
plt.show()


Python3
# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
ts = pd.Series(np.random.randn(1000), index = pd.date_range(
                                '1/1/2000', periods = 1000))
 
df = pd.DataFrame(np.random.randn(1000, 4),
   index = ts.index, columns = list('ABCD'))
 
df = df.cumsum()
plt.figure()
df.plot()
plt.show()


Python3
# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
ts = pd.Series(np.random.randn(1000), index = pd.date_range(
                                '1/1/2000', periods = 1000))
 
df = pd.DataFrame(np.random.randn(1000, 4), index = ts.index,
                                      columns = list('ABCD'))
 
df3 = pd.DataFrame(np.random.randn(1000, 2),
               columns =['B', 'C']).cumsum()
 
df3['A'] = pd.Series(list(range(len(df))))
df3.plot(x ='A', y ='B')
plt.show()


Python3
# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
ts = pd.Series(np.random.randn(1000), index = pd.date_range(
                                '1/1/2000', periods = 1000))
 
df = pd.DataFrame(np.random.randn(1000, 4), index = ts.index,
                                      columns = list('ABCD'))
 
df3 = pd.DataFrame(np.random.randn(1000, 2),
               columns =['B', 'C']).cumsum()
 
df3['A'] = pd.Series(list(range(len(df))))
df3.iloc[5].plot.bar()
plt.axhline(0, color ='k')
 
plt.show()


Python3
# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
df4 = pd.DataFrame({'a': np.random.randn(1000) + 1,
                    'b': np.random.randn(1000),
                    'c': np.random.randn(1000) - 1},
                           columns =['a', 'b', 'c'])
plt.figure()
 
df4.plot.hist(alpha = 0.5)
plt.show()


Python3
# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.rand(10, 5),
      columns =['A', 'B', 'C', 'D', 'E'])
 
df.plot.box()
plt.show()


Python3
# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.rand(10, 5),
      columns =['A', 'B', 'C', 'D', 'E'])
 
ser = pd.Series(np.random.randn(1000))
ser.plot.kde()
 
plt.show()


Python3
# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.rand(10, 5),
       columns =['A', 'B', 'C', 'D', 'E'])
 
df.plot.area()
plt.show()


Python3
# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.rand(500, 4),
           columns =['a', 'b', 'c', 'd'])
 
df.plot.scatter(x ='a', y ='b')
plt.show()


Python3
# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.randn(1000, 2), columns =['a', 'b'])
 
df['a'] = df['a'] + np.arrange(1000)
df.plot.hexbin(x ='a', y ='b', gridsize = 25)
plt.show()


Python3
# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
series = pd.Series(3 * np.random.rand(4),
  index =['a', 'b', 'c', 'd'], name ='series')
 
series.plot.pie(figsize =(4, 4))
plt.show()


输出:

  • 不同数据的绘图:在绘图中使用多个数据列表。

Python3

# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
ts = pd.Series(np.random.randn(1000), index = pd.date_range(
                                '1/1/2000', periods = 1000))
 
df = pd.DataFrame(np.random.randn(1000, 4),
   index = ts.index, columns = list('ABCD'))
 
df = df.cumsum()
plt.figure()
df.plot()
plt.show()

输出:

  • 在给定轴上绘图:我们可以明确定义轴的名称并在此轴的基础上绘制数据。

Python3

# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
ts = pd.Series(np.random.randn(1000), index = pd.date_range(
                                '1/1/2000', periods = 1000))
 
df = pd.DataFrame(np.random.randn(1000, 4), index = ts.index,
                                      columns = list('ABCD'))
 
df3 = pd.DataFrame(np.random.randn(1000, 2),
               columns =['B', 'C']).cumsum()
 
df3['A'] = pd.Series(list(range(len(df))))
df3.plot(x ='A', y ='B')
plt.show()

输出:

  • 使用 matplotlib 的条形图:查找不同类型的条形图以清楚地了解给定数据的行为。

Python3

# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
ts = pd.Series(np.random.randn(1000), index = pd.date_range(
                                '1/1/2000', periods = 1000))
 
df = pd.DataFrame(np.random.randn(1000, 4), index = ts.index,
                                      columns = list('ABCD'))
 
df3 = pd.DataFrame(np.random.randn(1000, 2),
               columns =['B', 'C']).cumsum()
 
df3['A'] = pd.Series(list(range(len(df))))
df3.iloc[5].plot.bar()
plt.axhline(0, color ='k')
 
plt.show()

输出:

  • 直方图:

Python3

# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
df4 = pd.DataFrame({'a': np.random.randn(1000) + 1,
                    'b': np.random.randn(1000),
                    'c': np.random.randn(1000) - 1},
                           columns =['a', 'b', 'c'])
plt.figure()
 
df4.plot.hist(alpha = 0.5)
plt.show()

输出:

  • 使用 Series 和 matplotlib 的箱线图:使用箱线绘制数据框的数据。

Python3

# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.rand(10, 5),
      columns =['A', 'B', 'C', 'D', 'E'])
 
df.plot.box()
plt.show()

输出:

  • 密度图:

Python3

# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.rand(10, 5),
      columns =['A', 'B', 'C', 'D', 'E'])
 
ser = pd.Series(np.random.randn(1000))
ser.plot.kde()
 
plt.show()

输出:

  • 使用 matplotlib 绘制面积图:

Python3

# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.rand(10, 5),
       columns =['A', 'B', 'C', 'D', 'E'])
 
df.plot.area()
plt.show()

输出:

  • 散点图:

Python3

# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.rand(500, 4),
           columns =['a', 'b', 'c', 'd'])
 
df.plot.scatter(x ='a', y ='b')
plt.show()

输出:

  • 六角箱图:

Python3

# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.randn(1000, 2), columns =['a', 'b'])
 
df['a'] = df['a'] + np.arrange(1000)
df.plot.hexbin(x ='a', y ='b', gridsize = 25)
plt.show()

输出:

  • 饼图:

Python3

# importing libraries
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
 
series = pd.Series(3 * np.random.rand(4),
  index =['a', 'b', 'c', 'd'], name ='series')
 
series.plot.pie(figsize =(4, 4))
plt.show()

输出: