大家好,我是 👉【Python当打之年(点击跳转)】
本期 手把手教你绘制官网神图 , Matplotlib系列和Pyecharts系列都会不间断更新,希望对大家有所帮助,如有疑问或者需要改进的地方可以联系小编。
Matplotlib高阶绘图系列:
Matplotlib | 高阶绘图案例【3】
Matplotlib | 高阶绘图案例【2】
Matplotlib | 高阶绘图案例【1】
示例图(部分调整):
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.patheffects import withStroke
from matplotlib.ticker import AutoMinorLocator, MultipleLocator
# 绘图
fig = plt.figure(figsize=(8,8),facecolor='#ECEFF1')
ax = fig.add_axes([0.2, 0.17, 0.68, 0.7])
# 设置x、y轴范围
ax.set_xlim(0, 4)
ax.set_ylim(0, 4)
# 设置坐标轴主刻度
ax.xaxis.set_major_locator(MultipleLocator(1))
ax.yaxis.set_major_locator(MultipleLocator(1))
# 设置坐标轴次刻度
ax.xaxis.set_minor_locator(AutoMinorLocator(4))
ax.yaxis.set_minor_locator(AutoMinorLocator(4))
# 设置坐标轴次刻度文本格式
ax.xaxis.set_minor_formatter("{x:.2f}")
ax.yaxis.set_minor_formatter("{x:.2f}%")
# 坐标轴标签替换
ax.set_yticks(np.linspace(0, 4, 5))
ax.set_yticklabels(list('ABCDE'))
# 设置刻度线样式
ax.tick_params(which='major', width=1, length=10, labelsize=14)
ax.tick_params(which='minor', width=1.0, length=5, labelsize=10, labelcolor='0.25')
# 数据
X = np.linspace(0.5, 3.5, 100)
Y1 = 3+np.cos(X)
Y2 = 1+np.cos(1+X/0.75)/2
Y3 = np.random.uniform(Y1, Y2, len(X))
ax.plot(X, Y1, c='C0', lw=2.5, label="Blue signal", zorder=10)
ax.plot(X, Y2, c='C1', lw=2.5, label="Orange signal")
# 设置栅格
ax.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10)
ax.scatter(X[::3], Y3[::3],marker = 'o',color="w",edgecolors='black')
# 设置标题
ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment='bottom')
# 设置标签
ax.set_xlabel("x Axis label", fontsize=14)
ax.set_ylabel("y Axis label", fontsize=14)
# 设置图例
ax.legend(loc="upper right", fontsize=14)
royal_blue = [0, 20/256, 112/256]
bbox=dict(boxstyle="round", fc="w", ec="w")
# 绘制标注
def annotate(x, y, text):
c = Circle((x, y), radius=0.1, clip_on=False, zorder=10, linewidth=2.5,
edgecolor=royal_blue + [0.6], facecolor='none',
path_effects=[withStroke(linewidth=7, foreground='white')])
ax.add_artist(c)
ax.text(x, y-0.25, text, style='italic', fontfamily='Courier New', ha='center', va='top', weight='bold',color=royal_blue,bbox=bbox)
annotate(3.8, -0.13, "Minor tick label\nax.xaxis.set_minor_formatter")
annotate(-0.03, 1.0, "Major tick\nax.yaxis.set_major_locator")
annotate(0.00, 3.75, "Minor tick\nax.yaxis.set_minor_locator")
annotate(-0.15, 3.00, "Major tick label\nax.yaxis.set_major_formatter")
annotate(1.68, -0.39, "xlabel\nax.set_xlabel")
annotate(-0.49, 1.67, "ylabel\nax.set_ylabel")
annotate(1.62, 4.15, "Title\nax.set_title")
annotate(1.75, 2.80, "Line\nax.plot")
annotate(2.25, 1.54, "Markers\nax.scatter")
annotate(3.00, 3.00, "Grid\nax.grid")
annotate(3.60, 3.58, "Legend\nax.legend")
annotate(2.5, 0.55, "Axes\nfig.subplots")
annotate(4.6, 4.5, "Figure\nplt.figure")
annotate(0.65, 0.01, "x Axis\nax.xaxis")
annotate(0, 0.36, "y Axis\nax.yaxis")
annotate(4.0, 0.7, "Spine\nax.spines")
# 设置边框
fig.patch.set(linewidth=4, edgecolor='0.5')
以上就是本期为大家整理的全部内容了,赶快练习起来吧,原创不易,喜欢的朋友可以点赞、收藏也可以分享(注明出处)让更多人知道。