Matplotlib memo

As a learning note, I have compiled the code that can be used for visualization around Matplotlib for my own reference instead of a cheat sheet.

Basic code summary

#Import matplotlib as plt
import matplotlib.pyplot as plt

#Create a line graph with list x on the horizontal axis and y on the vertical axis
plt.plot(x, y)

#Create a scatter plot with list x on the horizontal axis and y on the vertical axis(Plot size is proportional to size, color is col, transparency is 0.8)
plt.scatter(x, y, s = size, c = col, alpha = 0.8))

#Create a histogram of the data in the list values in n bins
plt.hist(values, bins = n)

#Title on the graph(TITLE)Put on
plt.title('TITLE')

#Label XXX on the horizontal axis and yyy on the vertical axis
plt.xlabel('xxx')
plt.ylabel('yyy')

#Specify the vertical axis(Example.From 0 to 10 in 2 increments)
plt.yticks([0,2,4,6,8,10])
#Specify the vertical axis(Example.Customize the notation in 2 increments from 0 to 10)
plt.yticks([0,2,4,6,8,10],['0','20,000','40,000','60,000','80,000','Hundred thousand'])

#Add text to a particular plot(Example.For plots with 10 on the horizontal axis and 52 on the vertical axis'text'Add text)
plt.text(10, 52, 'text')

#Show grid lines
plt.grid(True)

#Draw the created figure
plt.show()

#Set the horizontal axis to logarithmic display
plt.xscale('log')

Actual plot example

Graph of GDP and population for each prefecture image.png

#Data reading
with open('data.csv','r',encoding='shift_jis') as f:
    dataReader = csv.reader(f)
    list1 = [row for row in dataReader]  
    district = list1[0]
    population = list1[1]
    GDP = list1[2]
    district = district[1:]
    population = population[1:]
    GDP = GDP[1:]
    population = [int(s) for s in population]
    GDP = [int(s) for s in GDP]
#Graph drawing
import matplotlib.pyplot as plt
plt.scatter(GDP, population)
plt.title('Relationship between GDP and population')
plt.xlabel('GDP')
plt.ylabel('Population')
plt.xticks([0,30000000,60000000,90000000,120000000])
plt.yticks([0,3000000,6000000,9000000,12000000,15000000])
#Name some plots
plt.text(104470026,13623937, 'Tokyo')
plt.text(1864072,569554, 'Tottori')
plt.text(39409405,7506900, 'Aichi')
plt.text(38994994,8832512, 'Osaka')
plt.text(11944686,2837348, 'Hiroshima')
plt.text(9475481,2330120, 'Miyagi')
plt.text(19018098,5351828, 'Hokkaido')
plt.text(34609343,9144504, 'Kanagawa')
plt.text(22689675,7289429, 'Saitama')
plt.text(20391622,6235725, 'Chiba')
plt.grid(True)
plt.show()

Exhibitor: https://www.esri.cao.go.jp/jp/sna/data/data_list/kenmin/files/contents/main_h28.html (Cabinet Office / Prefectural Accounts)

Recommended Posts

Matplotlib memo
Matplotlib memorandum
gzip memo
Pandas memo
HackerRank memo
Python memo
python memo
graphene memo
Flask memo
Matplotlib gallery
pyenv memo
pytest memo
sed memo
Python memo
Install Memo
BeautifulSoup4 memo
networkx memo
python memo
tomcat memo
command memo
Generator memo.
psycopg2 memo
Python memo
SSH memo
matplotlib summary
Command memo
Memo: rtl8812
pandas memo
Shell memo
Python memo
Pycharm memo
Python memo
[Jupyter Notebook memo] Display kanji with matplotlib
AtCoder devotion memo (11/12)
[Python] Memo dictionary
PyPI push memo
tensorflow-gpu introduction memo
LPIC201 learning memo
Jupyter Notebook memo
LPIC304 virtualization memo
python beginner memo (9.2-10)
youtube download memo
Linux x memo
Django Learning Memo
ARC # 016 Participation memo
Beautiful Soup memo
LPIC101 study memo
linux (ubuntu) memo
scp command memo
#Python basics (#matplotlib)
Flask Primer Memo
celery / kombu memo
who command memo
I wrote matplotlib
django tutorial memo
Flask basic memo
Linux # Command Memo 1
[Python] Memo to translate Matplotlib into Japanese [Windows]
★ Memo ★ Python Iroha
Gender recognition memo
Image reading memo