[PYTHON] Implement "Data Visualization Design # 2" with matplotlib

What is a data visualization design?

This is a summary of data visualization points published in note by Go Ando of THE GUILD, who is famous for services that focus on UX and UI.


# 1?



plt.rcParams['font.family'] = 'Hiragino Sans'  

It is the part of.

8. Do not use the 2nd axis


#Library import
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.ticker import MultipleLocator

mau = [450,480,500,470,520,550,530,580,620,700,690,720]
g_rate = [0,8,0,-4,10,9,-6,12,4,12,-1,8]
g_rate = [one/100 for one in g_rate]
timeindex = pd.date_range('2019/1', periods=12, freq='MS')
mau = pd.Series(mau, index=timeindex, name='MAU(M)')
g_rate = pd.Series(g_rate,index=timeindex,name='Last month growth rate')

#For error avoidance in pandas
from pandas.plotting import register_matplotlib_converters

#Font settings
plt.rcParams['font.family'] = 'Hiragino Sans'  
plt.rcParams['font.weight'] = 'heavy'

#Split up and down
fig = plt.figure(figsize=(8,5))
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)

# 1.Change bar color and width
ax1.plot(g_rate.index, g_rate,linewidth=5.,color="saddlebrown")
ax2.bar(mau.index, mau, width=20., color='darkorange', zorder=2, align='center')

# 2.Display y-axis label, specify color and font size
ax2.set_ylabel(mau.name, color='gray',fontsize=14,fontweight='bold')

# 3.Change y-axis tick position

# 4.Erase x-axis y-axis tick,For the graph above, erase all x-axis
ax1.tick_params(labelbottom="off",bottom=False, left=False) 
ax2.tick_params(bottom=False, left=False)

# 5.Change the color of the x-axis and y-axis tick labels
ax1.tick_params(axis='y', colors='dimgray')
ax2.tick_params(axis='x', colors='dimgray')
ax2.tick_params(axis='y', colors='dimgray')

# 6.Show y-axis grid, color black for graph above

# 7.Erase the right frame

# 8.Change the color of the frame

# 9.Set x-axis tick label to month only
# '%-m'Is strftime leading zero no month notation

# 10.Set the x-axis tick position every month

#The major tick position is not updated unless the fig is drawn once

# 11.Specifying the range of the vertical axis
# 12.Change of vertical axis tick
vals_1 = [-0.2,0,0.2]
ax1.set_yticklabels(['{:,.0%}'.format(x) for x in vals_1],color="saddlebrown",fontsize=12,fontweight='bold')
vals_2 = [0,400,800]
ax2.set_yticklabels(['{}'.format(x) for x in vals_2] ,fontsize=12,fontweight='bold')

# 13.Align ylabel
axs = [ax1,ax2]


For how to set the vertical axis to% in # 12., I referred to the answer from stackoverflow [^ 1].

13. is based on the official doc of matplotlib [^ 2].

You can also write the y label vertically by starting a new line character by character, but I didn't because the code was ugly. (Please tell me if there is a smart way)

9. Reduce elements to a minimum


shipment_n = pd.DataFrame([52217, 9113, 4078],
                           index=['iPhone', 'iPad', 'Mac'],

fig, ax = plt.subplots(figsize=(6, 3))

# 1.Erase the frame other than bottom
sides = ['left','right', 'top']
[ax.spines[side].set_visible(False) for side in sides] 

# 2.Change the color of the bottom frame to gray

# 3.Erase the y-axis tick
ax.tick_params(left=False, labelleft=False)

# 4.Larger x-axis label size, gray color
ax.tick_params(axis='x', labelsize='xx-large',color="dimgray",labelcolor="dimgray")

# 5.Show actual value above the bar
vmax = shipment_n['amount'].max()
for i, value in enumerate(shipment_n['amount']):
    ax.text(i, value+vmax*0.06, f'{value:,}', fontsize='xx-large', va='center', ha='center', color='C0')


4 is achieved by offsetting using vmax.

10. Write the largest key message


ori_blue = "#3674b3"
ori_lightblue = "#b3daff"

# 1.Paint the whole
plt.rcParams["axes.facecolor"] = (1,1,1,0)

# 2.Blue background
fig, ax = plt.subplots(figsize=(6, 4),facecolor=ori_blue)

# 3.title
fig.suptitle("iPhone accounts for 80% of shipments",color="white",size=24,fontweight='bold',ha='center')

# 4.Plot in light blue

# 5.Add a description
ax.text(-0.4,shipment_n["amount"].max()*1.2,"Chart: 2017 Apple Shipment Devices",color=ori_lightblue,fontsize=12)

#6.Delete frames other than bottom
sides = ['left','right', 'top']
[ax.spines[side].set_visible(False) for side in sides] 

# 7.bottom frame color setting

# 8.y-axis range setting

# 9.Erase the y-axis tick label
ax.tick_params(left=False, labelleft=False)
# 10.x-axis label setting
ax.tick_params(axis='x', labelsize='xx-large',color=ori_lightblue,labelcolor=ori_lightblue)

# 11.Show actual value above the bar
vmax = shipment_n['amount'].max()
for i, value in enumerate(shipment_n['amount']):
    ax.text( i,value+vmax*0.06, f'{value:,}', fontsize='xx-large', va='center', ha='center', color=ori_lightblue)

#When saving
plt.savefig("savefig_with_facecolor.png ",facecolor=fig.get_facecolor())


About the color, I set it somehow. In the head family, the corners of the background were rounded, but this could not be reproduced. (I did not come out even after checking it quite a bit ...) Also, when saving with plt.savefig (), it is necessary to specify the background color with facecolor = [^ 3].

Recommended Posts

Implement "Data Visualization Design # 2" with matplotlib
Implement "Data Visualization Design # 3" with pandas and matplotlib
Data visualization with pandas
Graph Excel data with matplotlib (1)
Graph Excel data with matplotlib (2)
Data visualization method using matplotlib (1)
Data visualization method using matplotlib (2)
Data visualization method using matplotlib (+ pandas) (5)
Versatile data plotting with pandas + matplotlib
Data visualization method using matplotlib (+ pandas) (3)
Easy data visualization with Python seaborn.
Data visualization method using matplotlib (+ pandas) (4)
Data analysis starting with python (data visualization 2)
Separation of design and data in matplotlib
Recommendation of Altair! Data visualization with Python
Animation with matplotlib
Japanese with matplotlib
Animation with matplotlib
Histogram with matplotlib
Animate with matplotlib
Visualize corona infection data in Tokyo with matplotlib
Read Python csv data with Pandas ⇒ Graph with Matplotlib
Data analysis with python 2
2-axis plot with Matplotlib
Implement FReLU with tf.keras
Visualize data with Streamlit
Reading data with TensorFlow
Overview and tips of seaborn with statistical data visualization
Heatmap with Python + matplotlib
Learn with Cheminformatics Matplotlib
Data manipulation with Pandas!
Shuffle data with pandas
Various colorbars with Matplotlib
Normarize data with Scipy
Data analysis with Python
Logistics visualization with Python
LOAD DATA with PyMysql
Adjust axes with matplotlib
Getting Started with Drawing with matplotlib: Creating Diagrams from Data Files
Implement normalization of Python training data preprocessing with scikit-learn [fit_transform]
[Causal search / causal inference] Implement a Bayesian network with Titanic data
Sample data created with python
Implement login function with django-allauth
Embed audio data with Jupyter
Artificial data generation with numpy
Implement subcommands with Python's argparse
Extract Twitter data with CSV
Try using matplotlib with PyCharm
Get Youtube data with python
Graph drawing method with matplotlib
Clustering ID-POS data with LDA
Learn new data with PaintsChainer
Visualization of data by prefecture
Binarize photo data with OpenCV
Implement PyTorch + GPU with Docker
Save tweet data with Django
Stackable bar plot with matplotlib
[QtDesigner] Implement WebView with PyQt5
Gradient color selection with matplotlib
Animate multiple graphs with matplotlib
Data processing tips with Pandas