python画⽓泡图_⽤Python把图做的好看点:⽤Matplotlib做个
⽐较⽓泡图
本篇⽂章的⽬的通过汇制⽓泡图,熟悉Matplotlib中的patch元素
保存图⽚时连同背景颜⾊⼀起保存
我们成品长成这个样⼦
我们先来看⼀些⽓泡图的案
⽓泡图本质就是把柱状图⽤其他形状来表⽰,其主要⽬的还是为了形成数据间的对⽐
你可以⽤圆形,⽅向甚⾄三⾓形或者其他形状,关键的问题是⽤确定你⽤什么来表⽰数据⼤⼩
例如,⽓泡图⼀般都是⽤⾯积来表⽰数据⼤⼩,如果数据是100和10,对应图形的⾯积就是100和10,当然你也可以⽤其他特征,但是要保证对⽐的⼝径统⼀。
⼆、Matplotlib的patch元素
这些patch(块)其实就是各种图形元素的基本构成,⽐如三⾓形啊,圆形,多边形啊,等等
我们可以从matplotlib.pyplot中调⽤,也可以从matplotlib.patch中调⽤
已画圆形为例
from matplotlib import pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
circ=plt.Circle((0.7,0.2),0.15,color='b')
ax.add_patch(circ)
我们得到了这样⼀个图形,!为什么不圆
circ=plt.Circle((0.7,0.2),0.15,color='b')
我继续来研究这个代码,plt.Circle建⽴了圆形,这个圆形的圆⼼位于(0.7,0.2) ⽽半径为0.15
但是为什么是个椭圆?
仔细观察坐标轴的刻度,原因就在这,坐标轴的刻度不⼀样宽,画布也不是⼀个正⽅形,导致圆被挤压了
我们固定画布的形状再试试
fig=plt.figure(figsize=[10,10])
ax=fig.add_subplot(1,1,1)
circ=plt.Circle((0.7,0.2),0.15,color='b')
ax.add_patch(circ)
成功画了个圆形!! 撒花
我们再来圆个正⽅形
fig=plt.figure(figsize=[10,10])
ax=fig.add_subplot(1,1,1)
pgon=plt.Polygon([[0.5,0.5],[1,0.5],[1,1],[0.5,1]],color='k')
ax.add_patch(pgon)
pgon=plt.Polygon([[0.5,0.5],[1,0.5],[1,1],[0.5,1]],color='k')
Polygon(多边形)的绘图逻辑就是传⼊⼀个坐标列表[[0.5,0.5],[1,0.5],[1,1],[0.5,1]],⽽绘图的时候会根据坐标的先后,顺时针绘制绘制图形
那如何画⼀个半圆呢?⽼师
蠢不蠢,你画⼀个圆,上⾯再画⼀个和背景颜⾊⼀样的矩形盖住不就好了!!
基本知识讲完了
我们来看看例⼦的代码
import numpy as np
from matplotlib import pyplot as plt
year=[2012,2013,2014,2015,2016,2017,2018]
data1=[1200,1600,2400,3300,3300,3800,4000]
data2=[0,0,100,275,400,480,580]
radius1=[np.sqrt(i) for i in data1]
radius2=[np.sqrt(i) for i in data2]
x=[radius1[0]]
for i in range(len(radius1)):
try:
x.append(radius1[i]+radius1[i+1]+x[-1])
except:
continue
y=[100 for i in range(0,7)]
xy=[i for i in zip(x,y)]
fig=plt.figure(figsize=[20,20],facecolor=(0,47/255,99/255))
ax=fig.add_subplot(1,1,1,facecolor=(0,47/255,99/255))
ax.axis('off')
for i in range(len(x)):
circ=plt.Circle(xy=xy[i],radius=radius1[i],color='w')
ax.add_patch(circ)
for i in range(len(x)):
circ=plt.Circle(xy=xy[i],radius=radius2[i],color=(58/255,94/255,148/255))
ax.add_patch(circ)
pgon1=plt.Polygon([[-100,100],[800,100],[800,300],[-100,300]],color=(0,47/255,99/255))
pgon3=plt.Polygon([[0,140],[880,140]],color='w')#⽩⾊的线
ax.add_patch(pgon1)
ax.add_patch(pgon3)
for i in range(len(x)):
<(x=x[i],y=y[i]+15,s='%s'%year[i],color='w',ha='center',va='bottom',fontsize=14.5)
for i in range(len(x)):matplotlib中subplot
<(x=x[i],y=y[i]-0.8*radius1[i],s='%s'%data1[i],color=(0,47/255,99/255),ha='center',va='bottom',fontsize=14.5) for i in range(len(x)):
<(x=x[i],y=y[i]-1.1*radius2[i],s='%s'%data2[i],color=(0,47/255,99/255),ha='center',va='top',fontsize=11.5) plt.savefig('D:\\test2.png',_facecolor(),dpi=500)
这样的⽓泡图,有些⼈喜欢做成⽓泡挨着⽓泡的,⽐如例⼦这样,还也有些⼈喜欢做等距的⽐较⽓泡⽐如下图
定距离的很简单,确定好坐标就好
相互挨着的就要计算,圆⼼与圆⼼之间的距离也就是两个圆半径的和
x=[radius1[0]]#第⼀个圆⼼要⾃⼰确定放在哪⾥
for i in range(len(radius1)):
try:
x.append(radius1[i]+radius1[i+1]+x[-1])
except:
continue
y=[100 for i in range(0,7)]
xy=[i for i in zip(x,y)] #把圆⼼坐标打包
画好圆之后需要覆盖,在上⾯再画⼀层和背景⼀个颜⾊的矩形,就可以实现半圆了
plt.savefig('D:\\test2.png',_facecolor(),dpi=500)
最后如果需要保存的话 _facecolor()可以保存背景颜⾊,不然背景是⽩⾊的

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。