plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)bar_width = 0.45 hatch_par = ['/', 'x', 'O']for i in range(3):plt.bar(i, height=data[i], color='white', width=bar_width, edgecolor="k", hatch=hatch_par[i]*3)plt.bar(i+bar_width, height=...
python matplotlib绘制条形图填充效果
Python matplotlib库是绘制图表的强大工具,其中plt.bar专门用于条形图绘制。基本条形图代码如下:
# 代码段落1
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
data = np.random.uniform(low=0.5, high=1.0, size=10)
bar_width = 0.45
plt.bar(range(len(data)), height=data, color='blue', width=bar_width, edgecolor="k")
plt.xticks(fontsize=32)
plt.yticks(fontsize=32)
plt.show()
简单的条形图可能难以满足美观要求,尤其是在黑白打印版中,需要利用颜色与图案填充提升视觉效果。代码如下:
# 代码段落2
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
data = np.random.uniform(low=0.5, high=1.0, size=10)
plt.plot(1)
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
bar_width = 0.45
hatch_par = ['/', '', '|', '-', '+', 'x', 'o', 'O', '.', '*']
for i in range(10):
plt.bar(i, height=data[i], color='white', width=bar_width, edgecolor="k", hatch=hatch_par[i])
# 坐标轴名称设置
plt.xticks(range(len(hatch_par)), hatch_par, fontsize=32)
plt.yticks(fontsize=32)
plt.show()
在plt.bar方法中,hatch参数提供图案填充选项,可选值为:{'/', '', '|', '-', '+', 'x', 'o', 'O', '.', '*'}。选择图案与数量调整填充密度。示例代码如下:
# 代码段落3
data = np.random.uniform(low=0.5, high=1.0, size=3)
plt.plot(1)
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
bar_width = 0.45
hatch_par = ['/', 'x', 'O']
for i in range(3):
plt.bar(i, height=data[i], color='white', width=bar_width, edgecolor="k", hatch=hatch_par[i]*3)
plt.bar(i+bar_width, height=data[i], color='white', width=bar_width, edgecolor="k", hatch=hatch_par[i])
# 坐标轴名称设置
plt.xticks(fontsize=32)
plt.yticks(fontsize=32)
plt.show()2024-08-17