����λ�ã���ҳ > �����̳� > �̳� > ʹ��Python��matplotlib�����ͼ��

ʹ��Python��matplotlib�����ͼ��

��Դ������������|��ʱ�䣺2024-04-25 11:59:11 |���Ķ���185��|�� ��ǩ�� T ��ͼ Python �� |����������

���Ľ��������ʹ�� Python �� matplotlib �⻭ͼ����¼һЩ���õĻ�ͼ demo ����

���Ľ��������ʹ��Python��matplotlib�����ͼ��������¼һЩ���õĻ�ͼʾ�����롣

��װmatplotlib�⣺

# �������л������⻷����
pip install matplotlib

������ʾ���ã�

�°��matplotlib�Ѿ�֧��������˹��ܣ���˿���ֱ����������ΪTimes New Roman��SimSun�����壩����ʵ��Ӣ����Times New Roman��ʾ��������������ʾ��

import matplotlib.pyplot as plt

plt.rcParams['font.family'] = ['Times New Roman','SimSun']

����ͼ�͵���ͼ��

ʹ��plot�������Ի�������ͼ�͵���ͼ��ͨ��marker�������õ����ʽ��markersize�������õ�Ĵ�С��

import matplotlib.pyplot as plt

# ������������ΪTimes New Roman������
plt.rcParams['font.family'] = ['Times New Roman','SimSun']

# ��������
x = range(0, 6)
y = [i**3 for i in x]

# ���Ƶ���ͼ
plt.plot(x, y, marker='o', markersize=6, label='y=x^3')

# �����������ǩ
plt.xlabel('x')
plt.ylabel('y')

# ���������᷶Χ
plt.xlim(0)
plt.ylim(0)

# ����ͼ��
plt.legend()

# ���ý��ղ���
plt.tight_layout(pad=0.1)

# ����ͼƬ
plt.savefig('plot.png')

��״ͼ�Ͷѻ���״ͼ��

ʹ��bar����������״ͼ��ͨ��bottom�������Ի��ƶѻ���״ͼ��

# ��������
x = range(1, 6)
y1 = [1 for i in x]
y2 = [i for i in x]

# ���ƶѻ���״ͼ
plt.bar(x, y1, color='tab:blue', edgecolor='black', label='y1=1', width=0.5)
plt.bar(x, y2, bottom=y1, color='tab:orange', edgecolor='black', label='y2=x', width=0.5)

������ϵ㣺

��ʱ��Ҫ����״ͼ������y��Ķϵ㣬����ͨ����������ͬ��ͼ�������ò�ͬ��y�᷶Χ��Ȼ��������ͼ֮�����ӽض��ߵķ�ʽ��ʵ�֡�

# ��������
x = range(1, 5)
y = [i**3 for i in x]

# �ֱ������������ͼ
b1 = ax1.bar(x, y, color='tab:blue', edgecolor='black', label='y1=1', width=0.5)
b2 = ax2.bar(x, y, color='tab:blue', edgecolor='black', label='y1=1', width=0.5)

# ��ʾ����
ax1.bar_label(b1, fmt='%d', label_type='edge')
ax2.bar_label(b2, fmt='%d', label_type='edge')

# ������ͼ�������᷶Χ
ax1.set_ylim(15)
ax1.set_yticks([20, 40, 60])
# ɾ����ͼ���±߿�
ax1.spines['bottom'].set_visible(False)
# ������ͼ��x��
ax1.xaxis.set_visible(False)

# ������ͼ�������᷶Χ
ax2.set_ylim(0, 9)
ax2.set_yticks([0, 2, 4, 6, 8])
# ɾ����ͼ���ϱ߿�
ax2.spines['top'].set_visible(False)

# ���ӽض��ߣ�����ͼ�߶ȱ���Ϊ1:2�����Խض��ߵ�y����Ҳ��Ҫ����������
d = .015
kwargs = dict(transform=ax1.transAxes, color='k', clip_on=False)
ax1.plot((-d, +d), (-2*d, +2*d), **kwargs) 
ax1.plot((1 - d, 1 + d), (-2*d, +2*d), **kwargs)

kwargs.update(transform=ax2.transAxes)
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)
ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)

�ο����ϣ�

  • ��GitHub��Implement Font-Fallback in Matplotlib
  • ��matplotlib��axes.Axes.bar_label
  • ��matplotlib��Broken Axis

�������ߣ� ywang_wnlo
�������ӣ� https://ywang-wnlo.github.io/posts/731b80f7/
��Ȩ������ �������������³��ر������⣬������BY-NC-SA����Э�顣ת����ע��

С���Ƽ��Ķ�

�������������Ľ�Ϊ������Ϣ����������������ͬ���޹۵��֤ʵ��������

�����Ƶ����

����

ͬ������

����

ɨ��ά�����������ֻ��汾��

ɨ��ά����������΢�Ź��ںţ�

��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]

��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������