How to draw double Y-axis curve by matplotlib in Python
This article mainly introduces the Python matplotlib how to draw double Y-axis curve, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Preface
Double X-axis
Can be understood as sharing the y-axis.
Ax1=ax.twiny () ax1=plt.twiny ()
Double Y-axis
Can be understood as sharing the x-axis.
Ax1=ax.twinx () ax1=plt.twinx ()
Automatically generate an example
X = np.arange (0, np.e, 0. 01) y1 = np.exp (- x) y2 = np.log (x) fig = plt.figure () ax1 = fig.add_subplot (111) ax1.plot (x, y1) ax1.set_ylabel ('Y values for exp (- x)') ax1.set_title ("Double Y axis") ax2 = ax1.twinx () # this is the important functionax2.plot (x, y2,'r') ax2.set_xlim ([0] Np.e]) ax2.set_ylabel ('Y values for ln (x)') ax2.set_xlabel ('Same X for both exp (- x) and ln (x)') plt.show ()
Example: draw a chart of double y-axis coordinates
#-*-coding: utf-8-*-# call package import pandas as pd import numpy as np import matplotlib.pyplot as plt# to read the file io=r'E:\ work\ Special\ White Knight data Verification\ White Knight data Summary Table. Xlsx'yinka=pd.read_excel (io,sheet_name='YINKA_sample') bqs=pd.read_excel (io,sheet_name='BQS_result') yinka_bqs=pd.merge (yinka,bqs,left_on='no',right_on='no') How='inner') # drawing fig,ax=plt.subplots (1pm 1pm figsize = (20,300)) ax.grid () # draw Grid x=total.index-1 # Why + 1 Because of the misalignment. So when using it, write y=total ['var1'] ax.plot according to the situation.) # draw a line chart ax.set_xlim ([0L16]) # set the value range of the x-axis, which can make the x-axis consistent with the starting point of the y-axis ax.set_xticks (np.arange (0Magne16)) # set the scale range of the x-axis ax.set_xticklabels (np.arange (0L16)) Rotation=30) # set the scale on the x-axis ax.set_ylim ([0Med 1800]) # same y-axis value range ax.set_yticks (range (0Med 1800300)) # set the y-axis scale range ax.set_yticklabels (range (0Met 1800300)) # set the scale on the y-axis ax.legend (loc='upper left') # set the legend of the ax subgraph (legend) # the new knowledge point for Aperior b in zip (x Y): # set the annotation zip function is the corresponding relation ax.text (ax.text) # key ax1=ax.twinx () # this is the key point that can realize double y-axis. Shared x-axis Another is to replace the double x-axis chart with ax.twiny () y1=total [['adopt','reject']] y1.plot.bar (ax=ax1,alpha=0.5) # this is the method of drawing bar charts in matplotlib. If you use the seaborn drawing method and use the sns.barplot () function, you need to adjust a lot of details # here only the y-axis scale is set, and the x-axis scale setting will occasionally fail. It is worth noting that the data should be aligned with ax1.set_ylim ([0Magne1800]) ax1.set_yticks (range (0M1800300)) ax1.set_yticklabels (range (0M1800300)) for ePowerww in zip (data_.index,data_ [0], data_ [1]): ax1.text (eLim 1Med frech hawks, centerings, repertoire, etc.): ax1.text (eLimel) flegged, hawks, centerings, bottoms, museums, colors, b') ax1.text Color='g') ax1.legend (loc='best') plt.show () # get into the habit of writing # # Save the picture plt.savefig ('path') # the chart is output locally
The results show that:
Thank you for reading this article carefully. I hope the article "how to draw double Y-axis curves with matplotlib in Python" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!