首页 > 代码库 > Data Visualizations 4

Data Visualizations 4

So far, I have learn some types of plotting methods:

  • Matplotlib‘s high-level plotting methods - e.g. .scatter().plot()
  • Seaborn‘s high-level plotting methods - e.g. .distplot().boxplot()
  • Pandas DataFrame methods - e.g. .hist().boxplot()

High level plotting method like scatter(), only need to input the data and the function will take care the rest of the part. Low level plotting allows users do more customization.

1. Figure, Plot. Figsize(), add_subplot() function.

  import matplotlib.pyplot as plt # import the function

  %matplotlib inline # Make sure that all the plots are inline

  fig = plt.figure(figsize=(5,7)) # Create a figure which is 5 inch width, 7 inch high

  ax = fig.add_subplot(3,3,2) # Create 3 plots inside the figure, the width for each of them is 1/3 of the figure width. The height for each plot is 1/3 of figure height. And we display the second plot.
  
plt.show()

2. Axes:  A Subplot is an abstraction that creates an Axes object whenever you call .add_subplot(). An Axes object controls how the plotting actually happens. The Axes object describes what‘s actually inside the plot we‘re interested in (like the points in a scatter plot) and also describes the x and y axes, including the ticks, labels, etc. 

 While each Figure instance can contain multiple plots, and therefore multiple Axes objects, that specify how each plot looks, each unique Axes object can only belong to one figure.

3.   ax.set_xlim([1,12]) # set the range of ticks for x 

    ax.set_ylim([15,105])# set the range of ticks for y 

4.  Example:

  month_2013 = [1,2,3,4,5,6,7,8,9,10,11,12]
  temperature_2013 = [32,18,40,40,50,45,52,70,85,60,57,45]
  month_2014 = [1,2,3,4,5,6,7,8,9,10,11,12]
  temperature_2014 = [35,28,35,30,40,55,50,71,75,70,67,49]

  fig = plt.figure()
  ax1 = fig.add_subplot(1,2,1)
  ax2 = fig.add_subplot(1,2,2)

  ax1.set(xlim =(0,13),ylim = (10,110),xlabel= "month_2013",ylabel = "temperature_2013",title = "2013" )
  ax1.scatter(month,temperature,color = "darkblue",marker = "o")
  ax2.set(xlim =(0,13),ylim = (10,110),xlabel= "month_2014",ylabel = "temperature_2014",title = "2014" )
  ax2.scatter(month,temperature, color = "darkgreen",marker = "o")

  plt.show()

Data Visualizations 4