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 plt import numpy as np import pandas as pd import matplotlib as mpl print 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'] = 16 mpl.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()字典中主要存在這么幾類鍵:
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 np
import matplotlib.pyplot as plt
data = {'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 s
formatter = 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è)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)的簡(jiǎn)單排列組合算法示例
這篇文章主要介紹了Python實(shí)現(xiàn)的簡(jiǎn)單排列組合算法,涉及Python使用itertools庫(kù)進(jìn)行排列組合運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
解讀keras中的正則化(regularization)問(wèn)題
這篇文章主要介紹了解讀keras中的正則化(regularization)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
python進(jìn)程結(jié)束后端口占用問(wèn)題解析
這篇文章主要為大家介紹了python中在進(jìn)程結(jié)束后端口依然被占用的問(wèn)題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-01-01
YOLOv5中SPP/SPPF結(jié)構(gòu)源碼詳析(內(nèi)含注釋分析)
其實(shí)關(guān)于YOLOv5的網(wǎng)絡(luò)結(jié)構(gòu)其實(shí)網(wǎng)上相關(guān)的講解已經(jīng)有很多了,但是覺(jué)著還是有必要再給大家介紹下,下面這篇文章主要給大家介紹了關(guān)于YOLOv5中SPP/SPPF結(jié)構(gòu)源碼的相關(guān)資料,需要的朋友可以參考下2022-05-05
python3+PyQt5實(shí)現(xiàn)自定義分?jǐn)?shù)滑塊部件
這篇文章主要為大家詳細(xì)介紹了python3+PyQt5實(shí)現(xiàn)自定義分?jǐn)?shù)滑塊部件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04

