[PYTHON] Bar graph display in pandas (basic edition)

Thing you want to do

Premise

Official documentation

Official pandas documentation. Specifications of plot.bar.

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.bar.html

CSV dataset

sample.csv

Fictitious data created by myself. Shows the sales of companies that sell alcoholic beverages. Monthly sales amount of beer and sake (100 million yen). For example, in February, beer (beer) sold 43.1 billion yen and sake (sake) sold 26.7 billion yen.

month,beer,sake
1,1024,667
2,431,267
3,341,166
4,530,461
5,482,367
6,339,331
7,1203,227
8,1376,312
9,896,211
10,754,361
11,561,325
12,938,452

Below, implementation in python.

Import the library

import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt

Read CSV

df = pd.read_csv('csv/sample.csv')

The contents of df are as follows.

image.png

View monthly beer sales

df.plot.bar('month', 'beer')

The horizontal axis is "month" and the vertical axis is "beer sales amount (100 million yen)".

image.png

View monthly sake sales

df.plot.bar('month', 'sake')

The horizontal axis is "month" and the vertical axis is "sake sales amount (100 million yen)".

image.png

Display monthly "Beer and Sake Sales"

df.plot.bar('month',{'beer','sake'} )

The horizontal axis is "month" and the vertical axis is "sales amount of beer (blue) and sake (orange) (100 million yen)".

image.png

You can see the following.

Display monthly "sales of beer and sake" (stacked)

Add stacked = True to make a stacked graph.

df.plot.bar('month',{'beer','sake'}, stacked=True)

The horizontal axis is "month" and the vertical axis is "sales amount (100 million yen) of beer (blue) and sake (orange)" (accumulated).

image.png

You can see the following.

Change the color of the stick

df.plot.bar(
  'month',{'beer','sake'},
   color={"beer": "red", "sake": "green"}
)

image.png

Stack

df.plot.bar(
  'month',{'beer','sake'},
   color={"beer": "red", "sake": "green"},
   stacked=True
)

image.png

Scale the size of the graph display area

Specify with figsize.

figsize=(5,5)

df.plot.bar(
  'month',{'beer','sake'},
  figsize=(5,5)
)

image.png

figsize=(8,5)

image.png

figsize=(5,10)

image.png

Recommended Posts

Bar graph display in pandas (basic edition)
Display Matplotlib xy graph in PySimple GUI.
Learn Pandas in 10 minutes
Basic sorting in Python
Basic operation of pandas
Graph drawing in python
UnicodeDecodeError in pandas read_csv
Basic operation of Pandas
[In 3 lines] Plot the population pyramid (bar graph of age group / gender) with Pandas alone
Draw graph in python
Python Basic --Pandas, Numpy-
Graph time series data in Python using pandas and matplotlib
VScode environment construction (on Mac) & graph display in Python (@browser)