共用y轴的双图形绘制
实现这种形式的图形,可通过matplotlib和pandas的实现,相比下pandas实现方便的多。
我数据分析的时候主要是stacked bar、bar和line形式的放在一张图上。stacked bar若用matplotlib实现的话会比较复杂(多组)
先上图吧

def plot_stacked_bar(left_data, right_data):
width = .3
axe = plt.subplot(111)
axe = left_data.plot(kind='bar', stacked=True, ax=axe, width=width, use_index=True, legend=False)
axe.set_xticklabels(left_data.index, rotation=0) #add patches to the stacked bar
patterns = ('-', '+', 'x', '\\', '*', 'o', 'O', '.', '/')
bars = axe.patches
hatches = ''.join(h*len(left_data) for h in patterns)
for bar, hatch in zip(bars, hatches):
bar.set_hatch(hatch) #plottint the line sharing the same x-axis on the secondary y-axis
axf = axe.twinx()
axf.plot(axe.get_xticks(), right_data, linestyle='-', marker='o', linewidth=2.0)
axf.set_ylim((0, 90))
另一种形式的图形:

from matplotlib import pyplot as plt
import pandas as pd
from pandas import Series
import numpy as np
n = 50
x = pd.period_range('2001-01-01', periods=n, freq='M')
y1 = (Series(np.random.randn(n)) + 5).tolist()
y2 = (Series(np.random.randn(n))).tolist()
df = pd.DataFrame({'bar':y2, 'line':y1}, index=x) # let's plot
plt.figure(figsize=(20, 4))
ax1 = df['bar'].plot(kind='bar', label='bar')
ax2 = ax1.twiny()
df['line'].plot(kind='line', label='line', ax=ax2)
ax2.grid(color="red", axis="x") def align_xaxis(ax2, ax1, x1, x2):
"maps xlim of ax2 to x1 and x2 in ax1"
(x1, _), (x2, _) = ax2.transData.inverted().transform(ax1.transData.transform([[x1, 0], [x2, 0]]))
xs, xe = ax2.get_xlim()
k, b = np.polyfit([x1, x2], [xs, xe], 1)
ax2.set_xlim(xs*k+b, xe*k+b) align_xaxis(ax2, ax1, 0, n-1)
#参考#
- http://stackoverflow.com/questions/19952290/how-to-align-the-bar-and-line-in-matplotlib-two-y-axes-chart?newreg=4f241248a9f44c34a467216c0a6cf099
- http://stackoverflow.com/questions/22833404/how-do-i-plot-hatched-bars-using-pandas
- http://stackoverflow.com/questions/24227650/multiple-plots-on-same-figure-with-dataframe-plot/24237358#24237358
- http://stackoverflow.com/questions/22623324/plot-bar-graph-and-timeseries-plot-on-different-axis-using-pandas
- http://stackoverflow.com/questions/19060144/more-efficient-matplotlib-stacked-bar-chart-how-to-calculate-bottom-values
共用y轴的双图形绘制的更多相关文章
- highchart 设置双Y轴坐标 双x轴坐标方法
我们的图表一旦引入了两种不同单位或者数量级相差很大的数据以后,这时候需要两种坐标对其进行计量. 下面以设置双Y轴为例, y轴坐标的参数设置成: yAxis: [{ title: { text: '坐标 ...
- QT 基本图形绘制
QT 基本图形绘制 1.告诉绘制引擎一些东西 QPainter::Antialiasing 在可能的情况下,反锯齿 QPainter::TextAntialiasing 在可能的情况下,文 ...
- Android自定义控件之基本图形绘制
入门示例代码 在Android中Paint类就是画笔,Canvas 就是画布 对于画笔的大小,粗细,颜色,透明度都在paint类中设置,对于画出的成品,比如圆,方形,等在canvas类中的函数生成 入 ...
- 绘制复数图形和双y轴图形
clearclct=0:0.1:2*pi;x=sin(t);y=cos(t);z=x+i*y;subplot(1,3,1)plot(t,z,'r') %注:这种方式下,不论参数t,z哪个是复数,都将忽 ...
- Origin绘制双Y轴图的方法
1.已有数据绘图如下,其中网络流量的单位是M Bytes/s,与另外两组数据的单位(时间)不同,现在要为其添加右侧Y轴. 2.首先选中该图像,找到工具条,点击第三个按钮“Add Right-Y Lay ...
- Python科学计算技巧积累四——双y轴图像绘制
双y轴图像具有单y轴图像没有的对比效果,在MATLAB中有plotyy函数可以实现,Python的实现方式没有MATLAB那样方便,不过实现效果却也不见得差. 以往我常用的绘图命令是import ma ...
- 【Python】matplotlib 双y轴绘制及合并图例
1.双y轴绘制 关键函数:twinx() 问题在于此时图例会有两个. # -*- coding: utf-8 -*- import numpy as np import matplotlib.pypl ...
- Python教程:matplotlib 绘制双Y轴曲线图
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:数据皮皮侠 双X轴的可以理解为共享y轴 ax1=ax.twiny() ...
- Python实现双X轴双Y轴绘图
诈尸人口回归.这一年忙着灌水忙到头都掉了,最近在女朋友的提醒下终于想起来博客的账号密码,正好今天灌水的时候需要画一个双X轴双Y轴的图,研究了两小时终于用Py实现了.找资料的过程中没有发现有系统的文章, ...
随机推荐
- zen-cart 一页支付实现方法
1.下载插件CSS JS Loader 和 Fast and Easy Checkout for Zen Cart,插件请下载附件 2.先把CSS JS Loader覆盖,后台选项点击,点击后,程序会 ...
- 支持向量机SVM、优化问题、核函数
1.介绍 它是一种二类分类模型,其基本模型定义为特征空间上的间隔最大的线性分类器,即支持向量机的学习策略便是间隔最大化,最终可转化为一个凸二次规划问题的求解. 2.求解过程 1.数据分类—SVM引入 ...
- MyBatis For .NET学习-问题总结
1. MyBatis在进行sqlserver与c# 类型转换时需要注意,sqlserver中dbtype为float时,c#需要使用double与之对应,而不能使用float或decimal 2. M ...
- POJ - 1511 - 两次SPFA
这道题也算是一道模板题,但是第一次用优先队列迪杰斯特拉就T了.1e6的数据量,给了8s,网上其他题解中说要用SPFA. 题意:N个点的带权有向图.每次都从1出发,要到达其余没有被访问过的一个点(发传单 ...
- ServletConfig获取Servlet的公共参数方法
web.xml配置: <servlet> <servlet-name>AServlet</servlet-name> <servlet-class>AS ...
- Flash Builder4注册机
我的Eclipse下的Flash Builder 4正式版已经过期,之前在网上找到的注册码,都不能用了, 花了很久时间,才找到这个注册机. Flash Builder 4 注册机 Serial Cra ...
- ERROR 1093 (HY000): You can't specify target table 'test' for update in FROM clause
MySQL执行更新语句报错: 更新语句:UPDATE test SET state=50 WHERE num IN(SELECT num FROM test WHERE state=60): 报错:E ...
- STL学习笔记(不定期更新)
algorithm *1.sort() 用法:sort(数组名,名+长度(,cmp)); int cmp(T a,T b)//T是要排序的a,b的类型,也可以是结构体中任意成员变量 { return ...
- HAL编译问题
1 make:进入目录'/opt/FriendlyARM/tiny4412/android/android-4.1.2'make: *** 没有规则可以创建“out/target/product/ge ...
- Swift日常开发随笔
1.修改UISearchBar的搜索框底色 使用以下代码: setSearchFieldBackgroundImage(CommonUseClass._sharedManager.imageFromC ...