用Pandas和Matplotlib创建棒棒糖图表

用Pandas和Matplotlib创建棒棒糖图表

在这篇文章中,我们将创建棒棒糖图。它们只不过是条形图的一个变体,其中粗大的条形图被替换成了一条线和一个点状的 “o”(o型)。当有大量的数据需要表示时,棒状图是首选,当以条形图的形式表示时,这些数据会形成一个群组。

链接到CSV文件: 点击这里

让我们用不同的方法来讨论一些例子,用pandas来实现棒棒糖图的可视化。

示例 1:

使用plt.stem函数,它在图形下覆盖的每个x位置创建垂直线,从基线到y,并在那里放置一个标记。但它不灵活,所以你可能也想试试其他方法。

在这个例子中,我们将通过使用pandas从CSV文件中收集数据来绘制棒棒糖图。

首先,我们将通过plt.subplots()创建一个空图表,并使用干线图,它接受(x,y)形式的参数,两者都可以是类似数组的值,我们将把这些值绘制到图表中。use_line_collection=True提高了干线图的性能,并将干线绘制成LineCollection,而不是单个线条。basefmt=’ ‘从干线中删除基线。

CSV文件中有 “月数 “和 “总利润 “这两列,已经从CSV文件中读出,并作为(x,y)传入干线图,一些格式化和细节也已被添加。

set_ylim()被设置为零,这样Y轴的数值就从零开始。

步骤:

  • Import modules (matplotlib and pandas)
  • 打开CSV文件并读取数据
  • 使用plt.stem绘制它
# importing modules
from pandas import *
import matplotlib.pyplot as plt
  
# reading CSV file
d = read_csv("company_sales_data.csv")
  
# creating an empty chart
fig, axes = plt.subplots()
  
# plotting using plt.stem
axes.stem(d['month_number'], d['total_profit'],
          use_line_collection=True, basefmt=' ')
  
# starting value of y-axis
axes.set_ylim(0)
  
# details and formatting of chart
plt.title('Total Profit')
plt.xlabel('Month')
plt.ylabel('Profit')
plt.xticks(d['month_number'])

输出:

用Pandas和Matplotlib创建棒棒糖图表

示例 2:

在这个例子中,我们将使用plt.subplots()创建一个空的图形/图表,然后从CSV文件的列中读取数据,我们将使用vlines()函数绘制垂直线,该函数的参数为(x,ymin,ymax),其中ymin是最小值,ymax是要绘制的最大值。

在画完垂直线后,我们将使用plot()函数来画标记,这些标记将是圆形的,最后形成棒棒糖图。

# import modules
from pandas import *
from matplotlib import pyplot as plt
  
# read csv file
d = read_csv("company_sales_data.csv")
  
# using subplots() to draw vertical lines
fig, axes = plt.subplots()
axes.vlines(d['month_number'], ymin=0, ymax=d['total_profit'])
  
# drawing the markers (circle)
axes.plot(d['month_number'], d['total_profit'], "o")
axes.set_ylim(0)
  
# formatting and details
plt.xlabel('Month')
plt.ylabel('Profit')
plt.title('Total Profit')
plt.xticks(d['month_number'])

输出:

用Pandas和Matplotlib创建棒棒糖图表

示例 3:

本例与第二种方法相同,唯一不同的是线条的绘制,这里我们将以水平方式绘制线条。因此,我们将使用plt.subplots()创建一个空的图形/图表,然后从CSV文件的列中读取数据,我们将使用hlines()函数绘制垂直线,该函数的参数为(y,xmin,xmax),其中xmin是最小值,xmax是要绘制的最大值。

在再次绘制垂直线后,我们将使用plot()函数绘制标记,这些标记将是圆形的,最后形成棒棒糖图。在这里,我们将为每个月添加不同的颜色,使其看起来更有吸引力,hlines()函数作为选项颜色可以为不同的线设置颜色。

# import modules
from pandas import *
from matplotlib import pyplot as plt
  
# read csv file
d = read_csv("company_sales_data.csv")
  
# using subplots() to draw vertical lines
fig, axes = plt.subplots()
  
# providing list of colors
line_colors = ['blue', 'cyan', 'green', 'red',
               'skyblue', 'brown', 'yellow',
               'black', 'grey', 'orange', 'maroon',
               'lightgreen']
  
axes.hlines(d['month_number'], xmin=0,
            xmax=d['total_profit'], colors=line_colors)
  
# drawing the markers (circle)
axes.plot(d['total_profit'], d['month_number'], "o")
axes.set_xlim(0)
  
# formatting and details
plt.xlabel('Profit')
plt.ylabel('Month')
plt.title('Total Profit')
plt.yticks(d['month_number'])

输出:

用Pandas和Matplotlib创建棒棒糖图表

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程