共用y轴的双图形绘制 2022-05-23 13:48 103阅读 0赞 我数据分析的时候主要是stacked bar、bar和line形式的放在一张图上。[stacked bar若用matplotlib实现的话会比较复杂(多组)][stacked bar_matplotlib] 先上图吧 ![技术分享][20180110173619870220.png] 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)) 另一种形式的图形: ![技术分享][20180110173619874126.png] 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_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_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_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_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][stacked bar_matplotlib] [stacked bar_matplotlib]: http://stackoverflow.com/questions/19060144/more-efficient-matplotlib-stacked-bar-chart-how-to-calculate-bottom-values [20180110173619870220.png]: /images/20220523/00d7004f88ad4eafb05dca957df34402.png [20180110173619874126.png]: /images/20220523/e294062ec6db4ae5a64c7d8f79b759b8.png [http_stackoverflow.com_questions_19952290_how-to-align-the-bar-and-line-in-matplotlib-two-y-axes-chart_newreg_4f241248a9f44c34a467216c0a6cf099]: 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/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/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/22623324/plot-bar-graph-and-timeseries-plot-on-different-axis-using-pandas
相关 双Y轴echarts [https://www.makeapie.com/editor.html?c=xE1oQek3jr][https_www.makeapie.com_editor.html_c 待我称王封你为后i/ 2022年11月10日 14:17/ 0 赞/ 157 阅读
相关 【python科研绘图】双y轴并列柱状图+折线图+数据表结合,并封装图形绘制函数 双y轴并列柱状图+折线图+数据表结合 1. 论文原图 2 数据准备 3 代码实现步骤拆解 3.1 导入第三方库 3.2 数据赋 - 日理万妓/ 2022年11月07日 05:47/ 0 赞/ 148 阅读
相关 Matlab实用程序--图形应用-双y轴图形的绘制 function shili04h0=figure('toolbar','none',... 'position',[200 150 450 250],... 阳光穿透心脏的1/2处/ 2022年09月12日 11:53/ 0 赞/ 105 阅读
相关 Matlab绘制包含双Y轴的图 绘制数据对左侧 y 轴的图 创建左右两侧都有 y 轴的坐标区。yyaxis left 命令用于创建坐标区并激活左侧。后续图形函数(例如 plot)的目标为活动侧。绘制数据 ╰半橙微兮°/ 2022年09月10日 06:13/ 0 赞/ 188 阅读
相关 echarts双y轴对齐 import as echarts from 'echarts'; var chartDom = document.getElementById(' 「爱情、让人受尽委屈。」/ 2022年09月03日 00:10/ 0 赞/ 248 阅读
相关 共用y轴的双图形绘制 我数据分析的时候主要是stacked bar、bar和line形式的放在一张图上。[stacked bar若用matplotlib实现的话会比较复杂(多组)][stacked 一时失言乱红尘/ 2022年05月23日 13:48/ 0 赞/ 104 阅读
相关 Echarts实现双y轴 转载自 :[https://blog.csdn.net/njueyupeng/article/details/77839897?fps=1&locationNum=1][htt 浅浅的花香味﹌/ 2022年05月13日 05:46/ 0 赞/ 842 阅读
相关 matlab绘制垂线(x轴或y轴) 使用line函数就可以绘制垂线 1、绘制垂直于x轴的垂线 line(\[xvalue xvalue\],\[y1 y2\]); 比如绘制x=5 y取值为\[0,10\]; 骑猪看日落/ 2022年05月12日 08:42/ 0 赞/ 1378 阅读
相关 Echarts3实例 双Y轴折线图 实现效果 ![这里写图片描述][70] 知识点 双Y轴:yAxisIndex Y轴垂直标题 调整左侧Y轴刻度 代码实现 opti 电玩女神/ 2021年09月16日 07:06/ 0 赞/ 522 阅读
还没有评论,来说两句吧...