twin系列函数 和seconday_xaxis类似,该系列也是包含了twinx和twiny两个函数, 其作用是拷贝生成一个新的axes对象,twinx生成的新axes与原来的axes共享x轴,而twiny 以twinx函数为例,基本用法如下 >>> fig, ax = plt.subplots() >>> ax.plot([1, 2, 3, 4]) >>> ax1 = ax.twinx() >>> ax1 第二个坐标轴的位置可以通过axes的spines属性进行调节,用法如下 >>> fig, ax = plt.subplots() >>> ax.plot([1, 2, 3, 4]) >>> ax1 = ax.twinx
这里我就直接给出Python-matplotlib绘制方法和R-ggplot2的绘制方法,主要的知识点如下: Matplotlib-Axes.twinx()方法添加副轴 ggplot2-sec.axis ()绘制双轴 Matplotlib-Axes.twinx()方法添加副轴 这里我们直接就给出数据预览和可视化设计的代码,图中部分代码我们再做详细解释,数据预览如下: ? ()方法绘制 second_plot = ax.twinx() second_plot.set_ylim(bottom=-3,top=43) second_plot.set_yticks(np.arange 添加双y轴:使用Axes.twinx()方法绘制:重点 #添加双y轴:使用Axes.twinx()方法绘制 second_plot = ax.twinx() second_plot.set_ylim(bottom 总结:Python-matplotlib 绘制双Y轴的关键就是使用Axes.twinx()方法再次添加一个绘图对象,再把要绘制的对象在此绘图对象上绘制即可,其他和正常的matplotlib语法一样。
,label = 'HS300') HS.set_xticks(xticks) HS.set_xticklabels(HS300.date[xticks],rotation=60) ax2 = HS.twinx ) HS = plt.subplot(612) HS.plot(HS300.close) HS.set_xticks(xticks) HS.set_xticklabels([]) ax2 = HS.twinx ) HS = plt.subplot(613) HS.plot(HS300.close) HS.set_xticks(xticks) HS.set_xticklabels([]) ax2 = HS.twinx ) HS = plt.subplot(614) HS.plot(HS300.close) HS.set_xticks(xticks) HS.set_xticklabels([]) ax2 = HS.twinx () ax2.plot(EMA_02, 'r') ax3 = ax2.twinx() ax3.plot(EMA_04, 'y') plt.title('Moment-5') plt.show()
这里我就直接给出Python-matplotlib绘制方法和R-ggplot2的绘制方法,主要的知识点如下: Matplotlib-Axes.twinx()方法添加副轴 ggplot2-sec.axis ()绘制双轴 Matplotlib-Axes.twinx()方法添加副轴 这里我们直接就给出数据预览和可视化设计的代码,图中部分代码我们再做详细解释,数据预览如下: ? ()方法绘制 second_plot = ax.twinx() second_plot.set_ylim(bottom=-3,top=43) second_plot.set_yticks(np.arange 添加双y轴:使用Axes.twinx()方法绘制:重点 #添加双y轴:使用Axes.twinx()方法绘制 second_plot = ax.twinx() second_plot.set_ylim(bottom 总结:Python-matplotlib 绘制双Y轴的关键就是使用Axes.twinx()方法再次添加一个绘图对象,再把要绘制的对象在此绘图对象上绘制即可,其他和正常的matplotlib语法一样。
= plt.subplots( 1, 1, figsize=(9,3) ) ax1.plot( x, np.sin(x), color='red', linewidth=10) ax2 = ax1.twinx fig, ax1 = plt.subplots( 1, 1, figsize=(9,3) ) ax1.plot( x, x, color='blue', linewidth=10) ax2 = ax1.twinx figsize=(9,3) ) ax1.plot( x, np.sin(x), color='red', linewidth=10) ax1.set_facecolor('none') ax2 = ax1.twinx fig, ax1 = plt.subplots( 1, 1, figsize=(9,3) ) ax1.plot( x, x, color='blue', linewidth=10) ax2 = ax1.twinx
index.to_list(): ax.axvspan(i-.35, i+.35, facecolor='gray',alpha=.1,zorder=0) #添加双y轴:使用Axes.twinx ()方法绘制 second_plot = ax.twinx() second_plot.set_ylim(bottom=-3,top=43) second_plot.set_yticks(np.arange ='center',fontsize = 20,color='k',fontweight="bold") ax.text(.02,1.04,'Use the Matplotlib axes.Axes.twinx 添加双y轴:使用Axes.twinx()方法绘制:重点 #添加双y轴:使用Axes.twinx()方法绘制 second_plot = ax.twinx() second_plot.set_ylim(bottom second_plot.scatter(x,0,s=15,c='white',zorder=5) 最终的可视化结果如下: 总结:Python-matplotlib 绘制双Y轴的关键就是使用Axes.twinx
数值范围差了不少,因此我还需要一个Y轴,来代表“Volume”这一列数据的走势,代码如下 fig, ax1 = plt.subplots(figsize=(12,6)) # 第二个Y轴的标记 ax2 = ax1.twinx ax2.set_ylabel("Volume", fontsize=15) plt.title("Apple Stock Price", fontsize=18) output 上面的代码我们通过twinx ) 添加图例 接下来给绘制好的图表添加图例,不同的折线代表的是不同的数据,代码如下 fig, ax1 = plt.subplots(figsize=(12,6)) # 第二个Y轴 ax2 = ax1.twinx 将网格线去除掉 有时候我们感觉图表当中的网格线有点碍眼,就可以将其去掉,代码如下 fig, ax1 = plt.subplots(figsize=(12,6)) # 第二个Y轴 ax2 = ax1.twinx plt.rcParams['font.sans-serif'] = ['SimHei'] fig, ax1 = plt.subplots(figsize=(12,6)) # 第二个Y轴 ax2 = ax1.twinx
111) ax1.plot(x, y1) ax1.set_ylabel('Y values for exp(-x)') ax1.set_title("Double Y axis") ax2 = ax1.twinx .set_ylim(-1.0, 1.0) ax_1.set_yticks(np.linspace(-1, 1, 9, endpoint=True)) # 画图的另外一种方式 ax_2 = ax_1.twinx
纵轴名 plt.xticks(rotation=90, color='green') # 设置横坐标变量名旋转度数和颜色 # *******播放量折线图 ax2 = ax1.twinx tick_params(labelsize=6) plt.xticks(rotation=90, color='green') # *******收藏数折线图 ax4 = ax3.twinx tick_params(labelsize=6) plt.xticks(rotation=90, color='green') # *******收藏折线图 ax6 = ax5.twinx tick_params(labelsize=6) plt.xticks(rotation=90, color='green') # *******评论数折线图 ax8 = ax7.twinx 纵轴名 plt.xticks(rotation=90, color='green') # 设置横坐标变量名旋转度数和颜色 # *******播放量折线图 ax2 = ax1.twinx
Data.Date[58], color='red',linestyle = "-.") plt.title("2015.10.26",loc = "center") ax3 = figure_1.twinx Microsoft YaHei'] # plt.title("污染后",x = 0.75,y = 2.05) figure_2.set_ylim(bottom=0,top=70) ax3 = figure_2.twinx bbox_to_anchor=(0.92, 1), frameon=False) plt.ylabel("NO$_2$(${ug/m^3}$)") # Y轴标签 plt.xticks() ax3 = figure_3.twinx rotation=45, # position=(0,0), #调整年份的位置,让远离了x轴 # fontsize=9) ax4 = figure_4.twinx
weather_energy.temperatureMin, color = 'tab:pink') ax1.set_ylabel('Temperature') ax1.legend() ax2 = ax1.twinx weather_energy.humidity, color = 'tab:orange') ax1.set_ylabel('Humidity',color = 'tab:orange') ax2 = ax1.twinx weather_energy.windSpeed, color = 'tab:orange') ax1.set_ylabel('Wind Speed',color = 'tab:orange') ax2 = ax1.twinx weather_energy.uvIndex, color='tab:orange') ax1.set_ylabel('UV Index', color='tab:orange') ax2 = ax1.twinx weather_energy.dewPoint, color = 'tab:orange') ax1.set_ylabel('Dew Point',color = 'tab:orange') ax2 = ax1.twinx
ax.plot(x,y1,c='tab:blue',ls=':',marker='o') ax.plot(x,y3,c='tab:green',ls='--',marker='^') ax2=ax.twinx () ax2.plot(x,y2,c='tab:red',ls='--',marker='*') twinx顾名思义,就是使两个子图共享x轴,但是y轴各用各的: 这时,三根线都能在图上比较正常的显示出来了 ax.plot(x,y1,c='tab:blue',ls=':',marker='o') line3,=ax.plot(x,y3,c='tab:green',ls='--',marker='^') ax2=ax.twinx ,dpi=500) host = host_subplot(111, axes_class=AA.Axes) plt.subplots_adjust(right=0.75) par1 = host.twinx () par2 = host.twinx() offset = 60 new_fixed_axis = par2.get_grid_helper().new_fixed_axis par2.axis[
.5,11.5,23.5,35.5,47.5] # ax.xaxis.set_major_locator(ticker.FixedLocator(positions)) #添加双y轴:使用Axes.twinx ()方法绘制 second_plot = ax.twinx() #注意:这里必须要有, 后面的图例才可以生成 line_plot, = second_plot.plot(np.arange(len(data_all_df ax.set_xticks(np.arange(-.5, 48.5, step=12)) #设置副刻度 ax.set_xticks(np.arange(0, 48, step=1),minor=True) 使用Axes.twinx ()添加双轴 second_plot = ax.twinx() #注意:这里必须要有, 后面的图例才可以生成 line_plot, = second_plot.plot(np.arange(len(data_all_df
设置双坐标轴 函数:ax.twinx() 双坐标轴一般用于复合图表,同时表示两种图表的指标量纲不一,经典的使用场景如帕累托图。 使用中,需要对原有的ax使用.twinx()方法生成ax2,再利用ax2进行绘图 fig,ax = plt.subplots() ax.plot(['北京','上海','深圳'],[1,3,5],color ='r') # 双坐标用法 ax2 = ax.twinx() ax2.bar(['北京','上海','深圳'],[20,40,60],alpha=0.3) plt.show() ?
fig.add_subplot(211) ax1.bar(np.arange(0,10,2),np.random.rand(5)) ax1.set_yticks(np.arange(0,1,0.25)) ax2 = ax1.twinx
8.双坐标轴的图 ## 重点:twinx import matplotlib.pyplot as plt import numpy as np plt.clf() # 清空画布 fig = plt.figure ].iloc[:,2:17].values[0]) ax1.set_ylabel('计算机行业指数走势') ax1.set_title('计算机行业行情走势与基金持仓占比变化') ax2 = ax1.twinx () ax1.plot(x, y1) ax1.set_ylabel('Y values for exp(-x)') ax1.set_title("Double Y axis") ax2 = ax1.twinx
8.双坐标轴的图 ## 重点:twinx import matplotlib.pyplot as plt import numpy as np plt.clf() # 清空画布 fig = plt.figure ].iloc[:,2:17].values[0]) ax1.set_ylabel('计算机行业指数走势') ax1.set_title('计算机行业行情走势与基金持仓占比变化') ax2 = ax1.twinx () ax1.plot(x, y1) ax1.set_ylabel('Y values for exp(-x)') ax1.set_title("Double Y axis") ax2 = ax1.twinx
loc参数指点图例位置;其他参数还需要仔细研究一下 ax1.set_yticks(0,10,5)坐标轴如何分割 ax1.spines["top"].set_visible(False)边框 ax1.twinx fontsize = 18) ax1.set_ylim(0,240) for tl in ax1.get_yticklabels(): tl.set_color('b') ax2 = ax1.twinx
▲画中画 d 次坐标轴 # 使用twinx是添加y轴的坐标轴 # 使用twiny是添加x轴的坐标轴 import matplotlib.pyplot as plt import numpy as np x = np.arange(0,10,0.1) y1 = 0.05 * x ** 2 y2 = -1 * y1 fig,ax1 = plt.subplots() ax2 = ax1.twinx(
可以通过调用ax2 = ax.twinx()来创建另一个y轴;ax2.set_ylabel(“Second y-axis”);但这会使绘制图例等事情变得复杂,因为现在绘图配置在同一子图中被分成两个容器, label for data", alpha=0.3, ) ax.legend(title=f"Legend {i} title", fontsize=8) 如果子图包含多个轴,例如当调用ax.twinx