Article From:https://www.cnblogs.com/HuZihu/p/9511993.html

Learn to draw pictures using data from Nathan Yau’s book Live Data: A Guide to Data Visualization.

 

Data address: http://datasets.flowingdata.com/us-postage.csv

 

Preparations: First import Matplotlib and pandas, read the CSV file with pandas, and then create an image and a coordinate axis

import pandas as pd
from matplotlib import pyplot as plt
postage=pd.read_csv(r"http://datasets.flowingdata.com/us-postage.csv")
fig,ax=plt.subplots()

 

Let’s look at this data file first.

   Year  Price
0  1991   0.29
1  1995   0.32
2  1999   0.33
3  2001   0.34
4  2002   0.37
5  2006   0.39
6  2007   0.41
7  2008   0.42
8  2009   0.44
9  2010   0.44

This data is very simple, showing the change of US postage in 1991 -2010.

 

Let’s draw a ladder diagram to show the change of postage.

 

Ladder diagram:ax.step(x,y)

 

The code is as follows:

import pandas as pd
from matplotlib import pyplot as plt
postage=pd.read_csv(r"http://datasets.flowingdata.com/us-postage.csv")
fig,ax=plt.subplots(figsize=(10,4))

ax.step(postage["Year"],postage["Price"],where='post')
ax.set_title("US Postage Fee") #Setting up the title
ax.set_xticks([i for i in postage["Year"]]) #Set X axis scale
ax.set_yticks([]) #Removal of Y axis scale
#Remove the border
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.spines["right"].set_visible(False)
#Add text annotation
for i,j in zip(postage["Year"],postage["Price"]):
    ax.text(x=i,y=j+0.003,s=j)
fig.tight_layout()

plt.show()

 

The image is as follows:

 

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *