Get the App
SLTechnology News&Howtos  ›  Development  › 

How to draw stacked bar chart by python

Shulou Source: shulou.com Published: 2022-06-02 06:02:49 10月03日 Update

Python how to draw stack bar chart, for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

At present, there is mostly a single bar chart stack on the network, and we do not see a group of bar chart stack.

The code is as follows:

Import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport matplotlib.ticker as ticker

Import a set of self-made data

Data = pd.read_excel ('data.xlsx') In [4]: dataOut [4]:

If you use a few more plt.bar () functions, you can draw them.

Tick_label = list (data.columns) tick_label.remove ('Category') x = np.arange (len (tick_label)) y1 = data.iloc [2Magne1:] .values.tolist () # income (excluding self-transfer) y2 = data.iloc [3Mague 1:] .values.tolist () # Expenditure (excluding self-transfer) y3 = data.iloc [4jue 1:] .values.tolist () # income (self-transfer) y4 = data.iloc [51st :] .values.tolist () # Expenditure (transfer by yourself) bar_with = 0.25 # cylinder width plt.figure (figsize = (1213)) # canvas size plt.bar (x Y1, width = bar_with, # cylinder width align = 'center', # x axis coordinates and cylinder pair position color =' orangered', alpha = 0.6, # cylinder transparency label = 'income (excluding self-transfer)') plt.bar (x _ center', color y3 ~ width = bar_with, bottom = y1, # y-axis coordinates of the cylinder baseline align = 'center', color =' lightsalmon', alpha = 0.6 Label = 'income (self-transfer)') plt.bar (x + bar_with, y2, width = bar_with,align = 'center', color =' deepskyblue', alpha = 0.6, label = 'expenditure (excluding self-transfer)') plt.bar (x + bar_with, y4, width = bar_with, bottom = y2) plt.title ('monthly income and expenditure statement') Fontsize = 10) # set x-axis title plt.xticks (x + bar_with/2, tick_label, rotation= 70) # set x-axis coordinates plt.xlabel ('time', fontsize = 8, verticalalignment = 'top', horizontalalignment='right',rotation='horizontal') plt.xlabel (' time', fontsize = 8, verticalalignment = 'bottom', horizontalalignment='center') # legend is set outside the drawing Control coordinate parameters plt.legend (loc = 'center', bbox_to_anchor = (0.77,1.1), ncol=2) plt.savefig (' draw_bar.png', dpi=200, bbox_inches = 'tight') plt.close ()

Draw as shown in the figure:

Is it true that plt drawing is not so eye-catching?

This is the answer to the question about how to draw the stacked bar chart in python. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Tags: Cylinder bar coordinate income question width time more help solution easy easy code location content function single parameter legend graph Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL OPPO Reno Redmi NVidia vpn