我在 pandas 資料框中有一些時間序列資料,我可以將其可視化如下:
import pandas as pd
d = {'end_time': [datetime.datetime(2020, 3, 17, 0, 0), datetime.datetime(2020, 3, 17, 0, 5), datetime.datetime(2020, 3, 17, 0, 10), datetime.datetime(2020, 3, 17, 0, 15), datetime.datetime(2020, 3, 17, 0, 20), datetime.datetime(2020, 3, 17, 0, 25), datetime.datetime(2020, 3, 17, 0, 30), datetime.datetime(2020, 3, 17, 0, 35), datetime.datetime(2020, 3, 17, 0, 40), datetime.datetime(2020, 3, 17, 0, 45), datetime.datetime(2020, 3, 17, 0, 50), datetime.datetime(2020, 3, 17, 0, 55)], "measurement": [2000, 1500, 800, 900, 400, 4000, 300, 900, 1000, 1250, 1100, 1300], "reliability": [99, 81, 84, 85, 99, 86, 96, 97, 98, 99, 98, 97]}
# select some relevant columns
subset_df = pd.DataFrame.from_dict(d)
# plot measurements over time
subset_df.plot('end_time', 'measurement')
現在該reliability列是 和 之間的0數字100。我想要做的是突出這個可靠性得分低于 95 的區域。所以我可以在這些區域周圍覆寫一個透明框,以直觀地突出測量可能不太可靠的地方。
uj5u.com熱心網友回復:
考慮到這個隨機資料幀,其中
end_time: 日期從2020-03-17 00:00:00到2020-03-17 00:55:00以5分鐘為間隔measurement300:和之間的隨機整數4000reliability0:和之間的隨機整數100import pandas as pd import numpy as np df = pd.DataFrame({'end_time': pd.date_range(start='2020-03-17 00:00:00', end='2020-03-17 00:55:00', freq='5min'), 'measurement': np.random.randint(300, 4000, size=12), 'reliability': np.random.randint(0, 100, size=12)}) [Out]: end_time measurement reliability 0 2020-03-17 00:00:00 3905 7 1 2020-03-17 00:05:00 1143 93 2 2020-03-17 00:10:00 2672 55 3 2020-03-17 00:15:00 416 29 4 2020-03-17 00:20:00 1246 21 5 2020-03-17 00:25:00 2743 32 6 2020-03-17 00:30:00 2798 49 7 2020-03-17 00:35:00 1012 21 8 2020-03-17 00:40:00 3894 64 9 2020-03-17 00:45:00 1218 18 10 2020-03-17 00:50:00 1600 97 11 2020-03-17 00:55:00 729 76
如果目標是繪制所有reliability低于95紅色的度量,其余的繪制為藍色,讓我們首先創建一些有用的變數:
measurementreliability低于95:_measures = df[df.reliability < 95].measurementend_time的低于:measurement_reliability95dates = df[df.reliability < 95].end_timemeasurementreliability高于95 :measures2 = df[df.reliability >= 95].measurementend_time的高于:measurement_reliability95dates2 = df[df.reliability >= 95].end_time
現在讓我們創建情節
import matplotlib.pyplot as plt
# Create the plot:
plt.plot(dates, measures, 'ro', dates2, measures2, 'bo')
# Set the title:
plt.title('Measures over time')
# Set the x label:
plt.xlabel('Date')
# Set the y label:
plt.ylabel('Measure')
# Set the x ticks:
plt.xticks(rotation=45)
# Show the plot:
plt.show()

現在,根據要求(use fill_between so that I can paint a transparent box from the x-axes to the top of y-axes),在plt.show()可以使用以下內容之前
plt.fill_between(dates, 0, measures, color='red', alpha=0.2)

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/519483.html
上一篇:在Python中從DataFrame中洗掉None值
下一篇:在apandas資料框中連接字串
