最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python畫圖常規(guī)設(shè)置方式

 更新時(shí)間:2020年03月05日 08:33:06   作者:llh_1178  
這篇文章主要介紹了python畫圖常規(guī)設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

python繪圖的包大家應(yīng)該不會(huì)陌生,但是,對(duì)圖的常規(guī)設(shè)置不一定會(huì)知道(其實(shí)自己也是才知道的),比如:坐標(biāo)軸的字體大小、顏色設(shè)置;標(biāo)題的字體顏色大小設(shè)置;線的粗細(xì)、顏色;圖片風(fēng)格的設(shè)置等。了解這些常規(guī)設(shè)置必定會(huì)讓圖片更加美觀。

下面就具體來說說matplotlib中有哪些常規(guī)設(shè)置。

我主要總結(jié)了這幾個(gè)函數(shù):

plt.style.use()函數(shù);可以對(duì)圖片的整體風(fēng)格進(jìn)行設(shè)置??梢酝ㄟ^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)用線連接起來。
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()

這張圖就通過rcParams()函數(shù)設(shè)置了y軸的字體顏色,x軸的字體大小。同時(shí),將點(diǎn)的marker變成了三角形、顏色變?yōu)榱司G色。

mpl.rc()函數(shù);它可以用來設(shè)置線的粗細(xì)、風(fēng)格、顏色等。

mpl.rc('lines', linewidth=4, color='r', linestyle='-.')
plt.plot(data)

fontdict()函數(shù);也可以來辦同樣的事情。

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')
 
# 以所有收益的平均值畫一條垂直線,看哪些公司是超越平均收益的
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畫圖常規(guī)設(shè)置方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

峨眉山市| 岑巩县| 屏东市| 东兰县| 卢氏县| 凤阳县| 城固县| 扬州市| 长宁县| 洛川县| 淮安市| 商水县| 延长县| 裕民县| 房产| 江源县| 星子县| 泰兴市| 额尔古纳市| 招远市| 名山县| 明光市| 黎川县| 汤阴县| 三门县| 乌兰浩特市| 正阳县| 民丰县| 隆子县| 通城县| 涞水县| 永福县| 龙海市| 嘉善县| 崇文区| 贵州省| 四子王旗| 阿克苏市| 祁阳县| 凤山市| 长治市|