文章原創(chuàng),最近更新:2018-05-9
1.原數(shù)據的展示
2.折線圖的細節(jié)設置
課程來源: python數(shù)據分析與機器學習實戰(zhàn)-唐宇迪
為了方便學習,將練習所涉及的練習percent-bachelors-degrees-women-usa.csv文件以百度網盤共享的方式分享出來.
鏈接: https://pan.baidu.com/s/1igpIRU3g7rBkJm2nnkY6tg 密碼: 8tsw
1.原數(shù)據的展示
percent-bachelors-degrees-women-usa.csv文件本數(shù)據集匯總了從1970年到2011年之間美國大學各專業(yè)中女生數(shù)占總學生數(shù)的百分比例數(shù)值谬返,男女生共100人,如下圖所示:
2.折線圖的細節(jié)設置
利用Pandas庫讀入CSV文件,并快速繪制生物學專業(yè)女生比例隨著年份變化的曲線圖.
曲線圖完整的代碼如下;
import pandas as pd
import matplotlib.pyplot as plt
women_degrees=pd.read_csv('percent-bachelors-degrees-women-usa.csv')
plt.plot(women_degrees["Year"],women_degrees["Biology"])
plt.show()
輸出的結果如下:
如何在同一子圖中,繪制兩條不同顏色的線性圖,并且圖例在左上方,有標題等相關元素?
完整的代碼如下:
import pandas as pd
import matplotlib.pyplot as plt
women_degrees=pd.read_csv('percent-bachelors-degrees-women-usa.csv')
plt.plot(women_degrees['Year'], women_degrees['Biology'], c='blue', label='Women')
plt.plot(women_degrees['Year'], 100-women_degrees['Biology'], c='green', label='Men')
plt.legend(loc='upper right')
plt.title('Percentage of Biology Degrees Awarded By Gender')
plt.show()
輸出的結果如下:
如何設置忽略x軸和y軸的刻度?
這里涉及到的知識點是.tick_params()函數(shù)
matplotlib.pyplot.tick_params(axis ='both'昔驱,** kwargs )
作用是更改刻度線,刻度線標簽和網格線的外觀糕再。
- axis : {'x'淹真,'y'检激,'both'},可選將參數(shù)應用于哪個軸领跛。
運行的軸; 默認axis=both表示同時影響x乏德、y軸的刻度. - bottom, top, left, right : 布爾型.
是否繪制相應的刻度。
參考資料:matplotlib官方資料
可以利用子圖ax對象的tick_params屬性,忽略x軸和y軸的刻度
完整的代碼如下:
import pandas as pd
import matplotlib.pyplot as plt
women_degrees=pd.read_csv('percent-bachelors-degrees-women-usa.csv')
fig,ax=plt.subplots()
ax.plot(women_degrees['Year'], women_degrees['Biology'], c='blue', label='Women')
ax.plot(women_degrees['Year'], 100-women_degrees['Biology'], c='green', label='Men')
ax.tick_params(bottom="off", top="off", left="off", right="off")
ax.legend(loc='upper right')
ax.set_title('Percentage of Biology Degrees Awarded By Gender')
ax.legend(loc="upper right")
plt.show()
輸出的結果是:
如何設置忽略繪圖顯示的邊框?
這里涉及到的函數(shù)是如下:
for key,spine in ax.spines.items():
spine.set_visible(False)
作用是隱藏坐標系的外圍框線
注意:關于.spines.items()網上可以查的內容很少,這里也不是很理解.
利用子圖中spine對象中的items屬性喊括,可以忽略繪圖顯示的邊框,具體代碼如下:
import pandas as pd
import matplotlib.pyplot as plt
women_degrees=pd.read_csv('percent-bachelors-degrees-women-usa.csv')
fig,ax=plt.subplots()
ax.plot(women_degrees['Year'], women_degrees['Biology'], c='blue', label='Women')
ax.plot(women_degrees['Year'], 100-women_degrees['Biology'], c='green', label='Men')
ax.tick_params(bottom="off", top="off", left="off", right="off")
ax.legend(loc='upper right')
ax.set_title('Percentage of Biology Degrees Awarded By Gender')
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.legend(loc="upper right")
plt.show()
輸出的結果如下:
如何在同一畫布中繪制4個子圖,并且分別顯示'Biology', 'Computer Science', 'Engineering', 'Math and Statistics'這四個專業(yè)的男女生比例隨年份變化的趨勢?
代碼如下:
import pandas as pd
import matplotlib.pyplot as plt
women_degrees=pd.read_csv('percent-bachelors-degrees-women-usa.csv')
major_cats=['Biology', 'Computer Science', 'Engineering', 'Math and Statistics']
fig=plt.figure(figsize=(12,12))
for sp in range(0,4):
ax=fig.add_subplot(2,2,sp+1)
ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c='blue', label='Women')
ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c='green', label='Men')
plt.legend(loc="upper right")
plt.show()
輸出的結果如下:
從以上輸出的結果來看,不是很滿意,想對繪圖曲線取消邊框/刻度,設置標題,并且重新設置xy軸的刻度標簽,那又應該怎么操作呢?
代碼如下:
import pandas as pd
import matplotlib.pyplot as plt
women_degrees=pd.read_csv('percent-bachelors-degrees-women-usa.csv')
major_cats=['Biology', 'Computer Science', 'Engineering', 'Math and Statistics']
fig=plt.figure(figsize=(12,12))
for sp in range(0,4):
ax=fig.add_subplot(2,2,sp+1)
ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c='blue', label='Women')
ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c='green', label='Men')
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968,2011)
ax.set_ylim(0,100)
ax.set_title(major_cats[sp])
ax.tick_params(bottom="off", top="off", left="off", right="off")
plt.legend(loc="upper right")
plt.show()
輸出的結果如下:
怎么用RGB更改折線的顏色
以下是比較通用的RGB顏色
其他的顏色的RGB顏色,可以在其他相關的網站尋找.
代碼如下:
import pandas as pd
import matplotlib.pyplot as plt
women_degrees = pd.read_csv('percent-bachelors-degrees-women-usa.csv')
major_cats = ['Biology', 'Computer Science', 'Engineering', 'Math and Statistics']
cb_dark_blue = (0/255, 107/255, 164/255)
cb_orange = (255/255, 128/255, 14/255)
fig = plt.figure(figsize=(12, 12))
for sp in range(0,4):
ax = fig.add_subplot(2,2,sp+1)
# The color for each line is assigned here.
ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c=cb_dark_blue, label='Women')
ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c=cb_orange, label='Men')
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(major_cats[sp])
ax.tick_params(bottom="off", top="off", left="off", right="off")
plt.legend(loc='upper right')
plt.show()
輸出的結果如下:
從以上結果可以看到每個子圖折線的寬度太窄,看起來有點不爽,有沒有辦法可以讓子圖看起來更清晰一些呢?
在ax.plot參數(shù)增加折線寬度的設置,設置linewidth=10
修改前:
ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c=cb_dark_blue, label='Women')
ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c=cb_orange, label='Men')
修改后:
ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c=cb_dark_blue, label='Women',linewidth=10)
ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c=cb_orange, label='Men',linewidth=10)
修改后完整的代碼如下:
import pandas as pd
import matplotlib.pyplot as plt
women_degrees = pd.read_csv('percent-bachelors-degrees-women-usa.csv')
major_cats = ['Biology', 'Computer Science', 'Engineering', 'Math and Statistics']
cb_dark_blue = (0/255, 107/255, 164/255)
cb_orange = (255/255, 128/255, 14/255)
fig = plt.figure(figsize=(12, 12))
for sp in range(0,4):
ax = fig.add_subplot(2,2,sp+1)
ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c=cb_dark_blue, label='Women', linewidth=10)
ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c=cb_orange, label='Men', linewidth=10)
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(major_cats[sp])
ax.tick_params(bottom="off", top="off", left="off", right="off")
plt.legend(loc='upper right')
plt.show()
輸出的結果如下:
從以上結果看,效果還不是很完善,能否增加2個專業(yè),并且將繪制的子圖并排排列?
對畫布的大小進行重新設置
修改前:
major_cats = ['Biology', 'Computer Science', 'Engineering', 'Math and Statistics']
fig = plt.figure(figsize=(12, 12))
ax = fig.add_subplot(2,2,sp+1)
for sp in range(0,4):
ax.plot(women_degrees['Year'], women_degrees[major_cats[sp]], c=cb_dark_blue, label='Women', linewidth=10)
ax.plot(women_degrees['Year'], 100-women_degrees[major_cats[sp]], c=cb_orange, label='Men', linewidth=10)
ax = fig.add_subplot(2,2,sp+1)
ax.set_title(major_cats[sp])
修改后:
stem_cats = ['Engineering', 'Computer Science', 'Psychology', 'Biology', 'Physical Sciences', 'Math and Statistics']
fig = plt.figure(figsize=(18, 3))
for sp in range(0,6)
ax = fig.add_subplot(1,6,sp+1)
ax.plot(women_degrees['Year'], women_degrees[stem_cats[sp]], c=cb_dark_blue, label='Women', linewidth=10)
ax.plot(women_degrees['Year'], 100-women_degrees[stem_cats[sp]], c=cb_orange, label='Men', linewidth=10)
ax = fig.add_subplot(1,6,sp+1)
ax.set_title(stem_cats[sp])
修改后的代碼如下:
import pandas as pd
import matplotlib.pyplot as plt
women_degrees = pd.read_csv('percent-bachelors-degrees-women-usa.csv')
stem_cats = ['Engineering', 'Computer Science', 'Psychology', 'Biology', 'Physical Sciences', 'Math and Statistics']
cb_dark_blue = (0/255, 107/255, 164/255)
cb_orange = (255/255, 128/255, 14/255)
fig = plt.figure(figsize=(18, 3))
for sp in range(0,6):
ax = fig.add_subplot(1,6,sp+1)
ax.plot(women_degrees['Year'], women_degrees[stem_cats[sp]], c=cb_dark_blue, label='Women', linewidth=10)
ax.plot(women_degrees['Year'], 100-women_degrees[stem_cats[sp]], c=cb_orange, label='Men', linewidth=10)
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(stem_cats[sp])
ax.tick_params(bottom="off", top="off", left="off", right="off")
plt.legend(loc='upper right')
plt.show()
輸出的結果如下:
怎么在第1張以及第6張子圖的兩條折線上添加文字?
.text()函數(shù)相關的知識:
例如ax.text(1, 2, "I'm a text")
前兩個參數(shù)表示文本坐標, 第三個參數(shù)為要添加的文本
通過ax.text()進行設置,就可以在兩條折線上添加文字.
完整的代碼如下:
import pandas as pd
import matplotlib.pyplot as plt
women_degrees = pd.read_csv('percent-bachelors-degrees-women-usa.csv')
stem_cats = ['Engineering', 'Computer Science', 'Psychology', 'Biology', 'Physical Sciences', 'Math and Statistics']
cb_dark_blue = (0/255, 107/255, 164/255)
cb_orange = (255/255, 128/255, 14/255)
fig = plt.figure(figsize=(18, 3))
for sp in range(0,6):
ax = fig.add_subplot(1,6,sp+1)
ax.plot(women_degrees['Year'], women_degrees[stem_cats[sp]], c=cb_dark_blue, label='Women', linewidth=10)
ax.plot(women_degrees['Year'], 100-women_degrees[stem_cats[sp]], c=cb_orange, label='Men', linewidth=10)
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(stem_cats[sp])
ax.tick_params(bottom="off", top="off", left="off", right="off")
if sp ==0:
ax.text(2005,87,'Men')
ax.text(2002, 8, 'Women')
elif sp==5:
ax.text(2005, 62, 'Men')
ax.text(2001, 35, 'Women')
plt.show()
輸出的結果如下: