实例 1:
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, drange
import numpy as np
date1 = datetime.datetime(2020, 4, 2)
date2 = datetime.datetime(2020, 4, 6)
delta = datetime.timedelta(hours = 6)
dates = drange(date1, date2, delta)
y = np.arange(len(dates))
fig, ax = plt.subplots()
ax.plot_date(dates, y ** 2, 'g')
ax.set_xlim(dates[0], dates[-1])
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_minor_locator(HourLocator(range(0, 25, 6)))
ax.xaxis.set_major_formatter(DateFormatter('% Y-% m-% d'))
ax.fmt_xdata = DateFormatter('% Y-% m-% d % H:% M:% S')
fig.autofmt_xdate()
fig.suptitle('matplotlib.figure.Figure.autofmt_xdate()
function Examplenn', fontweight ="bold")
plt.show()
对于横坐标是日期的,可以使用fig.autofmt_xdate(),达到横坐标倾斜的效果,与plt.xticks(rotation=45)效果一致。
输出图:
代码来源:https://www.geeksforgeeks.org/matplotlib-figure-figure-autofmt_xdate-in-python/
实例2:
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.cbook as cbook
years = mdates.YearLocator()
months = mdates.MonthLocator()
years_fmt = mdates.DateFormatter('% Y')
with cbook.get_sample_data('goog.npz') as datafile:
data = np.load(datafile)['price_data'].view(np.recarray)
fig, ax = plt.subplots()
ax.plot('date', 'adj_close',
data = data[:300],
color ="green")
ax.xaxis.set_major_locator(years)
ax.format_ydata = lambda x: '$% 1.2f' % x
ax.grid(True)
fig.autofmt_xdate()
fig.suptitle('matplotlib.figure.Figure.autofmt_xdate()
function Examplenn', fontweight ="bold")
plt.show()
实例3:
绘制散点图+折线图:sns.relplot(x="timepoint", y="signal", hue="region", , dashes=False, markers=True, kind="line", data=fmri)
bash seaborn.lineplot 官网参数:https://seaborn.pydata.org/generated/seaborn.lineplot.html
实例4:
代码来源:https://zhuanlan.zhihu.com/p/494651966
代码:
sns.relplot(x='time', y='signal', data=df) plt.plot(time, data_mean, color='deeppink', label='mean') plt.plot(time, sin_waves, 'b-', label='ideal') plt.fill_between(time, data_min, data_max, color='violet', alpha=0.2) plt.legend() plt.show() plt.plot(time, data_mean, color='deeppink', label='mean') plt.plot(time, sin_waves, 'b-', label='ideal') plt.fill_between(time, data_mean - data_std, data_mean + data_std, color='violet', alpha=0.2) plt.legend() plt.show()
关于绘图颜色搭配,可以参考这个网站:https://mycolor.space/?hex=%23472FE7&sub=1
参考资料:
用seaborn/matplot绘制误差带阴影图:https://colainlibrary.com/archives/7cd1c910.html
Manage markers for lineplots with Python seaborn:https://vicastel.net/post-its/python_seaborn_lineplot_markers



