精华内容
下载资源
问答
  • matplotlib堆叠柱状图

    2020-01-30 21:38:56
    import numpy as np import pandas as pd import matplotlib.pyplot as plt df1 = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6], 'C':[7,8,9]}) fig,axes = plt.subplots(1,2,f...
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    df1 = pd.DataFrame({'A':[1,2,3],
                       'B':[4,5,6],
                       'C':[7,8,9]})
    
    fig,axes = plt.subplots(1,2,figsize = (10,6))
    df1.plot(kind = 'bar',colormap = 'Blues_r',stacked = True,grid = True,ax = axes[0])
    df1.plot(kind = 'bar',colormap = 'Reds_r',stacked = False,grid = True,ax = axes[1])
    

    在这里插入图片描述

    展开全文
  • Matplotlib绘制堆叠柱状图

    千次阅读 2021-01-06 17:17:06
    import matplotlib.pyplot as plt labels = ['G1', 'G2', 'G3', 'G4', 'G5','G6'] # 级别 men_means = np.random.randint(20,35,size = 6) women_means = np.random.randint(20,35,size = 6) men_std = np.random....
    import numpy as np
    import matplotlib.pyplot as plt
    
    labels = ['G1', 'G2', 'G3', 'G4', 'G5','G6'] # 级别
    men_means = np.random.randint(20,35,size = 6)
    women_means = np.random.randint(20,35,size = 6)
    men_std = np.random.randint(1,7,size = 6)
    women_std = np.random.randint(1,7,size = 6)
    width = 0.35
    
    plt.bar(labels, # 横坐标
            men_means, # 柱⾼
            width, # 线宽
            yerr=4, # 误差条
            label='Men')#标签
    
    plt.bar(labels, women_means, width, yerr=2, bottom=men_means,label='Women')
    plt.ylabel('Scores')
    plt.title('Scores by group and gender')
    plt.legend()
    

    image-20210106164833997

    展开全文
  • @MaxNoe已经用zip和enumerate很优雅地回答了这个问题。但是,对于不熟悉zip和enumerate的用户,以下代码可以... plt.rcdefaults()import numpy as npimport matplotlib.pyplot as pltimport matplotlib.patches as...

    @MaxNoe已经用zip和enumerate很优雅地回答了这个问题。但是,对于不熟悉zip和enumerate的用户,以下代码可以达到预期的结果:import matplotlib.pyplot as plt; plt.rcdefaults()

    import numpy as np

    import matplotlib.pyplot as plt

    import matplotlib.patches as mpatches

    status_day1 = [[0.2,0.3,0.5], [0.1,0.3,0.6], [0.4,0.4,0.2], [0.6,0.1,0.3]]

    status_day2 = [[0.1,0.2,0.7], [0.3,0.2,0.5], [0.1,0.5,0.4], [0.2,0.5,0.3]]

    xval = [0.,1.,2.] #The places where the ticks are going to be on the x-axis

    bottom_append = 0 #Counter to keep track of the bar (this is quite important)

    fig = plt.figure(figsize=(5,5))

    ax = fig.add_subplot(111)

    for x in range(0,4): #Looping through every hour

    for y in range(0,3): #Looping through every parameter

    if y==0:

    if x==0:

    print(status_day1[x][y], bottom_append)

    ax.bar(0, status_day1[x][y], width = 0.3, color='blue',align='center')

    bottom_append = bottom_append+status_day1[x][y]

    else:

    print(status_day1[x][y], bottom_append)

    ax.bar(0, status_day1[x][y], width = 0.3, color='blue',align='center',bottom=bottom_append)

    bottom_append = bottom_append+status_day1[x][y]

    elif y==1:

    print(status_day1[x][y], bottom_append)

    ax.bar(0, status_day1[x][y], width = 0.3, color='red',align='center', bottom=bottom_append)

    bottom_append = bottom_append+status_day1[x][y]

    else:

    print(status_day1[x][y], bottom_append)

    ax.bar(0, status_day1[x][y], width = 0.3, color='green',align='center', bottom=bottom_append)

    bottom_append = bottom_append+status_day1[x][y]

    bottom_append = 0

    # Code is exactly same as the above, only takes into account day2

    for x in range(0,4): #Looping through every hour

    for y in range(0,3): #Looping through every parameter

    if y==0:

    if x==0:

    print(status_day2[x][y], bottom_append)

    ax.bar(1, status_day2[x][y], width = 0.3, color='blue',align='center')

    bottom_append = bottom_append+status_day2[x][y]

    else:

    print(status_day2[x][y], bottom_append)

    ax.bar(1, status_day2[x][y], width = 0.3, color='blue',align='center',bottom=bottom_append)

    bottom_append = bottom_append+status_day2[x][y]

    elif y==1:

    print(status_day2[x][y], bottom_append)

    ax.bar(1, status_day2[x][y], width = 0.3, color='red',align='center', bottom=bottom_append)

    bottom_append = bottom_append+status_day2[x][y]

    else:

    print(status_day2[x][y], bottom_append)

    ax.bar(1, status_day2[x][y], width = 0.3, color='green',align='center', bottom=bottom_append)

    bottom_append = bottom_append+status_day2[x][y]

    # Setting the properties of the subplot in an attempt to beautify it

    Label1 = mpatches.Patch(color='blue', label='Label1')

    Label2 = mpatches.Patch(color='green', label='Label2')

    Label3 = mpatches.Patch(color='red', label='Label3')

    ax.legend(handles=[Label1, Label2, Label3], loc=1)

    ax.set_xticks(xval)

    ax.set_xticklabels(["Day1","Day2","Day3"])

    ax.set_xlabel('Day')

    ax.set_ylabel('Hours')

    plt.show()

    展开全文
  • matplotlib 堆积柱状图-正负叠加问题

    千次阅读 2019-11-08 16:19:46
    没看到很好的解决方案,希望有好的方案的在下方提供链接。...import matplotlib.pyplot as plt # Juwairia's data: a = [4,-6,9] b = [2,7,1] c = [3,3,1] d = [4,0,-3] data = np.array([a, b, c, d]) d...

    提供一个方案供参考

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Juwairia's data:     
    a = [4,-6,9]
    b = [2,7,1]
    c = [3,3,1]
    d = [4,0,-3]
    data = np.array([a, b, c, d])
    
    data_shape = np.shape(data)
    
    # Take negative and positive data apart and cumulate
    def get_cumulated_array(data, **kwargs):
        cum = data.clip(**kwargs)
        cum = np.cumsum(cum, axis=0)
        d = np.zeros(np.shape(data))
        d[1:] = cum[:-1]
        return d  
    
    cumulated_data = get_cumulated_array(data, min=0)
    cumulated_data_neg = get_cumulated_array(data, max=0)
    
    # Re-merge negative and positive data.
    row_mask = (data<0)
    cumulated_data[row_mask] = cumulated_data_neg[row_mask]
    data_stack = cumulated_data
    
    cols = ["g", "y", "b", "c"]
    
    fig = plt.figure()
    ax = plt.subplot(111)
    
    for i in np.arange(0, data_shape[0]):
        ax.bar(np.arange(data_shape[1]), data[i], bottom=data_stack[i], color=cols[i],)
    
    plt.show()

     

     

     

     

    展开全文
  • matplotlib堆叠条形图/柱状图

    万次阅读 2019-06-24 15:58:49
    import matplotlib.pyplot as plt y1 = np.array([68.03, 68.83, 93.53, 70.67, 87.70, 91.04, 68.42, 69.92, 96.49, 88.64, 82.91, 88.22, 93.06, 95.42, 81.32, 82.38, 64.71, 61.16, 73.8...
  • 有个朋友要求帮忙绘制堆叠柱状图,查阅了一些文档之后也算是完成了,只是一个小demo,下面我就记录一下。 1.什么是堆叠柱状图 与并排显示分类的分组柱状图不同,堆叠柱状图将每个柱子进行分割以显示相同类型下各个...
  • 本文为Matplotlib柱状图系列基础柱状图绘制的第二篇文章,主要将带来三幅堆叠柱状图的制作基础柱状图-004下面我们就来讲解如何绘制第一个堆叠柱状图,注意,代码在以下环境全部通过测试:Python == 3.7.1matplotlib =...
  • 参考官网:matplotlib.pyplot.bar matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align=‘center’, data=None, **kwargs) ...使用bar绘制堆叠柱状图 代码如下: # TODO: 画出device_conn...
  • 柱形图(又称柱状图、直方图、条形图)用于各组数据的数量比较,可以简单的分为水平柱形图、垂直柱形图及堆积柱形图。本文详细介绍matpltlib中垂直、水平和堆积三种柱形图的绘制。我的公众号:“pythonic生物人“,...
  • 也可以是一个list width 柱体的宽度, 其他同上 bottom 直译是底座, 这里表示柱状图下面是谁, 在实例中表现为, 黄色柱体的底下是蓝色的 align 决定柱状图的位置, 两个参数可选, ‘center’(居中),‘edge’(左边缘与...
  • import matplotlib.pyplot as plt import matplotlib as mpl import numpy as np mpl.rcParams['font.sans-serif'] = ['SimHei'] 以下主输入对象均为数据框 饼图 def pie_plot(object_obj,col_name=...
  • Matplotlib 2 |折线图| 柱状图| 堆叠图| 面积图| 填图| 饼图| 直方图| 散点图| 极坐标| 图箱型图 1.基本图表绘制 plt.plot() ts.plot() 由Series直接生成图表 Dataframe直接生成图表 df.plot( ) 2.柱状图堆叠...
  • 一、折线 1、常规折线 import matplotlib.pyplot as plt import matplotlib.font_manager as fm #这样设置以后 可以在中显示中文 #Fontproperties 字体性质 my_font = fm.FontProperties(fname='C:\Windows\...
  • matplot-堆叠柱状图

    千次阅读 2019-06-12 09:03:54
    import matplotlib.pyplot as plt import matplotlib p = matplotlib.rcParams p["figure.subplot.left"] = 0.2 plt.figure(figsize=(9.8, 6)) base= [0, 80.6, 69.7, 81.8] add= [82.2, 83....
  • 1、实现excel表中柱状图的功能;2、并按照三个月的合计排序。import pandas as pdimport matplotlib.pyplot as pltusers = pd.read_excel('F:/practice/py/practise_09.xlsx')#print(users)users.plot.bar(x='NAME',...
  • import matplotlib.pyplot as plt import numpy as np import matplotlib as mpl mpl.rcParams["font.sans-serif"] = ["SimHei"] mpl.rcParams["axes.unicode_minus"] = False pos_num = [108, 36, 121, 109, 147,...

空空如也

空空如也

1 2 3 4 5
收藏数 98
精华内容 39
关键字:

matplotlib堆叠柱状图