一、图表样式概括
1.1、默认图表样式
可以通过rc_params()函数查看全部配置
import matplotlib
matplotlib.rc_params()
运行结果如下;
matplotlib常用配置
matplotlib载入时会主动调用rc_params()函数获取包含全部配置项的字典
2.1、图表样式修改
使用matplotlib_fname()可以查看文件所在的位置
import matplotlib
matplotlib.matplotlib_fname()
运行结果如下:
二、使用颜色
使用colors.cnames可以查看全部的颜色
for name,hex in matplotlib.colors.cnames.items():
print(name,hex)
运行结果如下:
等Deng…
有两种不同颜色的矩形条堆积而成的直方图 ,让人在视觉上更加直观
#01_book_purchase
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams["axes.unicode_minus"]=False
x=np.arange(5)
y1=[1200,2400,1800,2200,1600]
y2=[1050,2100,1300,1600,1340]
bar_width=0.6
tick_label=['家庭',"小说","心理","科技","儿童"]
fig=plt.figure()
ax=fig.add_subplot(111)
ax.bar(x,y1,bar_width,color="#87CEFA",align="center",label="地区1")
ax.bar(x,y2,bar_width,bottom=y1,color="#B0C4DE",align="center",label="地区2")
ax.set_ylabel("采购数量(本)")
ax.set_xlabel("图书种类")
ax.set_title("地区1和地区2对各类图书的采集情况")
ax.grid(True,axis='y',color="gray",alpha=0.2)
ax.set_xticks(x)
ax.set_xticklabels(tick_label)
ax.legend()
plt.title("2020080603039")
plt.show()
结果如下:
三、选择线型
当pyplot绘制折线图、显示网格或添加参考线时,可以将线型的取值传递给linestyle或ls参数
示例:
import numpy as np
import matplotlib.pyplot as