生活随笔
收集整理的這篇文章主要介紹了
可视化篇(四)——— python绘制双y轴、箱线图、概率分布三种图形及案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
可視化篇(四)——— python繪制雙y軸、箱線圖、概率分布三種圖形及案例
摘要
本文演示了如何通過python繪制雙y軸、箱線圖、概率分布三種圖形,并給出了具體案例供讀者參考。
繪制雙y軸圖形
效果圖如下
代碼如下
import numpy
as np
import matplotlib
.pyplot
as plt
from matplotlib
.font_manager
import FontProperties
import datetime# 此設置為使圖形能顯示帶特殊格式的字符
plt
.rcParams
['axes.unicode_minus'] = False# 構建虛擬數據
"""
x軸為日期
左y軸為y1,y2
右y軸為 y1
-y2 及其均值
"""
date
= [(datetime
.datetime
.strptime("2019-12-08 00:00:00","%Y-%m-%d %H:%M:%S") + datetime
.timedelta(days
=i
)).strftime("%Y-%m-%d%H:%M:%S")[:10] for i
in range(300)]
x
= np
.arange(300)
x_ticks
= [int(i
) for i
in np
.linspace(0, (x
.shape
[0] - 1), 5)]
y1
= np
.random
.randn(300) *2 + 75
y2
= np
.random
.randn(300) *2 + 70# 設置字體
font
= FontProperties(fname
="C:\Windows\Fonts\simsun.ttc", size
=30)figure
= plt
.figure(figsize
=[30, 16], dpi
=72)ax1
= figure
.add_subplot(111)fig1
= ax1
.plot(x
, y1
, color
="c", label
="y1")
fig2
= ax1
.plot(x
, y2
, color
="b", label
="y2")
plt
.yticks(fontproperties
=font
)
plt
.xlabel("date", fontproperties
=FontProperties(fname
="C:\Windows\Fonts\simsun.ttc", size
=45))
plt
.xticks(x_ticks
, date
, fontproperties
=FontProperties(fname
="C:\Windows\Fonts\simsun.ttc", size
=30), rotation
=15)
ax1
.set_ylim(40, 83)
ax1
.set_ylabel("y", fontproperties
=FontProperties(fname
="C:\Windows\Fonts\simsun.ttc", size
=45))ax2
= ax1
.twinx()fig3
= ax2
.plot(x
, y1
-y2
, color
="m", label
="y1-y2")
fig4
= ax2
.plot(x
, [(y1
-y2
).mean()] * (x
.shape
[0]), color
="g", linestyle
="--", linewidth
=5, label
="mean of y1-y2")
ax2
.set_ylim(-5, 40)
ax2
.set_ylabel("y1-y2", fontproperties
=FontProperties(fname
="C:\Windows\Fonts\simsun.ttc", size
=45))
plt
.yticks(fontproperties
=font
)# 顯示均值
ax2
.text(x
.shape
[0], (y1
-y2
).mean() - 0.1, str((y1
-y2
).mean())[:4], fontdict
={"size": 30, "color": "g"})# 設置圖列
legends
= fig1
+ fig2
+ fig3
+ fig4
labels
= [l
.get_label() for l
in legends
]
plt
.legend(legends
, labels
, prop
=font
, loc
=(0.8, 0.4))plt
.show()
繪制箱線圖
效果圖如下
代碼如下
import numpy
as np
import matplotlib
.pyplot
as plt
from matplotlib
.font_manager
import FontProperties
import seaborn
as sns
import pandas
as pd# 此設置為使圖形能顯示帶特殊格式的字符
plt
.rcParams
['axes.unicode_minus'] = False# 構建虛擬數據
"""
繪制x1
,x2
,x3
,x4四組數據的箱線圖
"""
x1
= np
.random
.randn(200)*2+5
x2
= np
.random
.randn(200)*2+10
x3
= np
.random
.randn(200)*2+15
x4
= np
.random
.randn(200)*2+20
data
= np
.array([x1
,x2
,x3
,x4
]).T
df
= pd
.DataFrame(data
,columns
=["x1","x2","x3","x4"])font
= FontProperties(fname
= "C:\Windows\Fonts\simsun.ttc",size
=30)
figure
= plt
.figure(figsize
=[16,12],dpi
=72)# whis為異常值在上下四分位點差值的多少倍以外
sns
.boxplot(data
= df
, whis
=3, orient
="v", fliersize
=15)
plt
.ylabel("Kpa", fontproperties
=FontProperties(fname
="C:\Windows\Fonts\simhei.ttf", size
=45))
plt
.yticks(fontproperties
=font
)
plt
.xticks(fontproperties
= FontProperties(fname
= "C:\Windows\Fonts\simsun.ttc",size
=45))
plt
.grid(linestyle
="--", linewidth
=1, alpha
=0.5, axis
="y")
plt
.show()
繪制概率分布圖
效果圖如下
代碼如下
import numpy
as np
import matplotlib
.pyplot
as plt
from matplotlib
.font_manager
import FontProperties
import seaborn
as sns# 此設置為使圖形能顯示帶特殊格式的字符
plt
.rcParams
['axes.unicode_minus'] = False# 構建虛擬數據
"""
繪制x的概率分布圖
"""
x
= np
.random
.randn(1000)*2+100
figure
= plt
.figure(figsize
=[30,26],dpi
=72)
sns
.distplot(x
,bins
=30,kde_kws
={"color":"red"},color
="c")
plt
.xticks(fontproperties
=FontProperties(fname
="C:\Windows\Fonts\simsun.ttc",size
=30))
plt
.yticks(fontproperties
=FontProperties( fname
="C:\Windows\Fonts\simsun.ttc",size
=30))
plt
.xlabel(xlabel
="活化能",fontproperties
= FontProperties( fname
="C:\Windows\Fonts\simsun.ttc",size
=45))
plt
.ylabel("概率密度",fontproperties
= FontProperties( fname
="C:\Windows\Fonts\simsun.ttc",size
=45))plt
.show()
by CyrusMay 2021 01 25
脫下長日的假面
奔向夢幻的疆界
南瓜馬車的午夜
換上童話的玻璃鞋
——————五月天(擁抱)——————
總結
以上是生活随笔為你收集整理的可视化篇(四)——— python绘制双y轴、箱线图、概率分布三种图形及案例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。