python畫(huà)圖常規(guī)設(shè)置方式
python繪圖的包大家應(yīng)該不會(huì)陌生,但是,對(duì)圖的常規(guī)設(shè)置不一定會(huì)知道(其實(shí)自己也是才知道的),比如:坐標(biāo)軸的字體大小、顏色設(shè)置;標(biāo)題的字體顏色大小設(shè)置;線的粗細(xì)、顏色;圖片風(fēng)格的設(shè)置等。了解這些常規(guī)設(shè)置必定會(huì)讓圖片更加美觀。
下面就具體來(lái)說(shuō)說(shuō)matplotlib中有哪些常規(guī)設(shè)置。
我主要總結(jié)了這幾個(gè)函數(shù):
plt.style.use()函數(shù);可以對(duì)圖片的整體風(fēng)格進(jìn)行設(shè)置。可以通過(guò)plt.style.availabel知道一共有多少種主題。
import matplotlib.pyplot as pltimport numpy as npimport pandas as pdimport matplotlib as mplprint plt.style.availabel
我們?cè)囉闷渲袃蓚€(gè)主題。
plt.style.use('fivethirtyeight')data = np.random.randn(50)plt.scatter(range(50), data)
with plt.style.context((’dark_background’)): plt.plot(np.sin(np.linspace(0, 2 * np.pi)), ’r-o’) # 'r-o'表示紅色的點(diǎn)用線連接起來(lái)。plt.show()
mpl.rcParams()函數(shù);這個(gè)函數(shù)可以設(shè)置圖片的坐標(biāo)軸以及標(biāo)題的字體大小、顏色、寬度等。同時(shí),也可以用mpl.rcParams.keys()進(jìn)行查看有哪些設(shè)置。
mpl.rcParams[’xtick.labelsize’] = 16mpl.rcParams['ytick.color'] = ’b’plt.plot(range(50), data, ’g^’)plt.show()
這張圖就通過(guò)rcParams()函數(shù)設(shè)置了y軸的字體顏色,x軸的字體大小。同時(shí),將點(diǎn)的marker變成了三角形、顏色變?yōu)榱司G色。
mpl.rc()函數(shù);它可以用來(lái)設(shè)置線的粗細(xì)、風(fēng)格、顏色等。
mpl.rc(’lines’, linewidth=4, color=’r’, linestyle=’-.’)plt.plot(data)
fontdict()函數(shù);也可以來(lái)辦同樣的事情。
font = {’family’ : ’monospace’, ’weight’ : ’bold’, ’size’ : ’larger’, ’color’ : 'r' }plt.scatter(range(50), data)plt.xlabel('number', fontdict=font)
font()字典中主要存在這么幾類(lèi)鍵:
font.family ;一共有5種設(shè)置: serif sans-serif cursive antasy monospace
font.style ;一種有3種設(shè)置:normal italic oblique
font.variant ;一共有2種設(shè)置:normal or small-caps
font.weight ;一共有4種設(shè)置:normal, bold, bolder, lighter
font.stretch ;一共有13種設(shè)置:
ultra-condensed, extra-condensed, condensed, semi-condensed, normal, semi-expanded, expanded, extra-expanded, ultra-expanded, wider, and narrower. font.size ;默認(rèn)值是10pt
plt.setp()函數(shù);也是可以設(shè)置線的粗細(xì)以及顏色,還可以設(shè)置坐標(biāo)軸的方向,位置。
例如:
setp(lines, ’linewidth’, 2, ’color’, ’r’)
借用幫助文檔上的一個(gè)例子:
import numpy as npimport matplotlib.pyplot as pltdata = {’Barton LLC’: 109438.50, ’Frami, Hills and Schmidt’: 103569.59, ’Fritsch, Russel and Anderson’: 112214.71, ’Jerde-Hilpert’: 112591.43, ’Keeling LLC’: 100934.30, ’Koepp Ltd’: 103660.54, ’Kulas Inc’: 137351.96, ’Trantow-Barrows’: 123381.38, ’White-Trantow’: 135841.99, ’Will LLC’: 104437.60}group_data = list(data.values())group_names = list(data.keys())group_mean = np.mean(group_data)fig, ax = plt.subplots()ax.barh(group_names, group_data)labels = ax.get_xticklabels()plt.setp(labels, rotation=45, horizontalalignment=’right’)ax.set(xlim=[-10000, 140000], xlabel=’Total Revenue’, ylabel=’Company’, title=’Company Revenue’)
可以看到x軸坐標(biāo)斜向45°旋轉(zhuǎn)了,整個(gè)圖片變得更加美觀了。為了對(duì)數(shù)據(jù)更加一步分析,做下面操作:
def currency(x, pos): '''The two args are the value and tick position''' if x >= 1e6: s = ’${:1.1f}M’.format(x*1e-6) else: s = ’${:1.0f}K’.format(x*1e-3) return sformatter = FuncFormatter(currency)fig, ax = plt.subplots(figsize=(6, 8))ax.barh(group_names, group_data)labels = ax.get_xticklabels()plt.setp(labels, rotation=45, horizontalalignment=’right’) ax.set(xlim=[-10000, 140000], xlabel=’Total Revenue’, ylabel=’Company’, title=’Company Revenue’)ax.xaxis.set_major_formatter(formatter)fig, ax = plt.subplots(figsize=(8, 8))ax.barh(group_names, group_data)labels = ax.get_xticklabels()plt.setp(labels, rotation=45, horizontalalignment=’right’) # 以所有收益的平均值畫(huà)一條垂直線,看哪些公司是超越平均收益的ax.axvline(group_mean, ls=’--’, color=’r’) # 標(biāo)注新成立的公司for group in [3, 5, 8]: ax.text(145000, group, 'New Company', fontsize=10, verticalalignment='center') # 將標(biāo)題移動(dòng)一點(diǎn),與圖片保持一點(diǎn)距離。ax.title.set(y=1.05)ax.set(xlim=[-10000, 140000], xlabel=’Total Revenue’, ylabel=’Company’, title=’Company Revenue’)ax.xaxis.set_major_formatter(formatter)ax.set_xticks([0, 25e3, 50e3, 75e3, 100e3, 125e3])plt.show()
現(xiàn)在好了,可以直觀的看出哪些公司是新成立得,同時(shí)哪些公司的收益是超越平均水平的。對(duì)之后的數(shù)據(jù)分析和統(tǒng)計(jì)都是有非常大的幫助的。
以上這篇python畫(huà)圖常規(guī)設(shè)置方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. HTML5 Canvas繪制圖形從入門(mén)到精通2. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說(shuō)明(學(xué)習(xí))3. 不要在HTML中濫用div4. XML入門(mén)的常見(jiàn)問(wèn)題(三)5. HTML DOM setInterval和clearInterval方法案例詳解6. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)7. CSS清除浮動(dòng)方法匯總8. 本站用的rss輸出9. Vue如何使用ElementUI對(duì)表單元素進(jìn)行自定義校驗(yàn)及踩坑10. XML在語(yǔ)音合成中的應(yīng)用
