知行编程网知行编程网  2022-06-21 08:00 知行编程网 隐藏边栏 |   抢沙发  3 
文章评分 0 次,平均分 0.0
<section powered-by="xiumi.us" style="color: rgb(0, 0, 0);text-align: left;white-space: normal;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;"><section><section><p data-mpa-powered-by="yiban.io" style="white-space: normal;max-width: 100%;min-height: 1em;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 1px;text-indent: 0em;color: rgb(63, 63, 63);widows: 1;box-sizing: border-box !important;overflow-wrap: break-word !important;"> “</span><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(255, 169, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">菜鸟学Python</span><span style="max-width: 100%;letter-spacing: 1px;text-indent: 0em;color: rgb(63, 63, 63);widows: 1;box-sizing: border-box !important;overflow-wrap: break-word !important;">”, <span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">设为 “</span><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(0, 122, 170);box-sizing: border-box !important;overflow-wrap: break-word !important;">星标</span><span style="max-width: 100%;letter-spacing: 0.544px;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">”</span></span></span></p><p style="white-space: normal;max-width: 100%;min-height: 1em;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;widows: 1;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;">和30w+一起学Python!</span></span></p><p style="white-space: normal;max-width: 100%;min-height: 1em;font-family: -apple-system, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;letter-spacing: 0.544px;widows: 1;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br  /></span></span></p><p style="text-align: center;"><img class="rich_pages" data-ratio="0.8566775244299675" data-s="300,640"  data-type="png" data-w="1228" style="color: rgb(0, 0, 0);font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;letter-spacing: 0.544px;text-align: center;white-space: normal;background-color: rgb(255, 255, 255);" src="https://www.zkxjob.com/wp-content/uploads/2022/06/wxsync-2022-06-22bcb8523decc121bccbc49066c12e07.png"  /></p><section powered-by="xiumi.us"><br  /></section></section></section></section><section style="letter-spacing: 0px;white-space: normal;line-height: normal;"><br  /></section><section style="letter-spacing: 0px;text-indent: 0em;white-space: normal;line-height: 1.5em;text-align: left;"><span style="font-size: 13px;letter-spacing: 0px;color: rgb(136, 136, 136);font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;">来源丨Python大本营</span></section>

大家好,我是菜鸟哥~


今天分享给大家25个Matplotlib图的汇总,在数据分析和可视化中最有用。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># !pip install brewer2mpl</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> numpy <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">as</span> np<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> pandas <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">as</span> pd<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> matplotlib <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">as</span> mpl<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> matplotlib.pyplot <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">as</span> plt<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> seaborn <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">as</span> sns<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> warnings; warnings.filterwarnings(action=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'once'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />large = <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span>; med = <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>; small = <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">12</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />params = {<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'axes.titlesize'</span>: large,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />          <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'legend.fontsize'</span>: med,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />          <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'figure.figsize'</span>: (<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>),<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />          <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'axes.labelsize'</span>: med,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />          <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'axes.titlesize'</span>: med,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />          <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'xtick.labelsize'</span>: med,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />          <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'ytick.labelsize'</span>: med,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />          <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'figure.titlesize'</span>: large}<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.rcParams.update(params)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.style.use(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'seaborn-whitegrid'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.set_style(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"white"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />%matplotlib inline<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Version</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">print</span>(mpl.__version__)  <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">#> 3.0.0</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">print</span>(sns.__version__)  <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">#> 0.9.0</span></section>


1. 散点图


Scatteplot是用于研究两个变量之间关系的经典和基本图。如果数据中有多个组,则可能需要以不同颜色可视化每个组。在Matplotlib,你可以方便地使用。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import dataset </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />midwest = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://raw.githubusercontent.com/selva86/datasets/master/midwest_filter.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Prepare Data </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Create as many colors as there are unique midwest['category']</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />categories = np.unique(midwest[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'category'</span>])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />colors = [plt.cm.tab10(i/<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">float</span>(len(categories)-1)) <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> i <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> range(len(categories))]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw Plot for Each Category</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.figure(figsize=(16, 10), dpi= 80, facecolor=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'w'</span>, edgecolor=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'k'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> i, category <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> enumerate(categories):<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    plt.scatter(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'area'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'poptotal'</span>, <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />                data=midwest.loc[midwest.category==category, :], <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />                s=20, c=colors[i], label=str(category))<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decorations</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">set</span>(xlim=(0.0, 0.1), ylim=(0, 90000),<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />              xlabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Area'</span>, ylabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Population'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.xticks(fontsize=12); plt.yticks(fontsize=12)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Scatterplot of Midwest Area vs Population"</span>, fontsize=22)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.legend(fontsize=12)    <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()    <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>

25个常用Matplotlib图的Python代码,干货收藏!


2. 带边界的气泡图


有时,您希望在边界内显示一组点以强调其重要性。在此示例中,您将从应该被环绕的数据帧中获取记录,并将其传递给下面的代码中描述的记录。encircle()


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">from</span> matplotlib <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> patches<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">from</span> scipy.spatial <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> ConvexHull<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> warnings; warnings.simplefilter(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'ignore'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.set_style(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"white"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Step 1: Prepare Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />midwest = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://raw.githubusercontent.com/selva86/datasets/master/midwest_filter.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># As many colors as there are unique midwest['category']</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />categories = np.unique(midwest[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'category'</span>])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />colors = [plt.cm.tab10(i/float(len(categories)<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">-1</span>)) <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> i <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> range(len(categories))]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Step 2: Draw Scatterplot with unique color for each category</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />fig = plt.figure(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>, facecolor=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'w'</span>, edgecolor=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'k'</span>)    <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> i, category <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> enumerate(categories):<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    plt.scatter(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'area'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'poptotal'</span>, data=midwest.loc[midwest.category==category, :], s=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'dot_size'</span>, c=colors[i], label=str(category), edgecolors=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'black'</span>, linewidths=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.5</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Step 3: Encircling</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># https://stackoverflow.com/questions/44575681/how-do-i-encircle-different-data-sets-in-scatter-plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">def</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">encircle</span><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">(x,y, ax=None, **kw)</span>:</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">if</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">not</span> ax: ax=plt.gca()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    p = np.c_[x,y]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    hull = ConvexHull(p)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    poly = plt.Polygon(p[hull.vertices,:], **kw)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    ax.add_patch(poly)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Select data to be encircled</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />midwest_encircle_data = midwest.loc[midwest.state==<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'IN'</span>, :]                         <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw polygon surrounding vertices    </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />encircle(midwest_encircle_data.area, midwest_encircle_data.poptotal, ec=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"k"</span>, fc=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"gold"</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.1</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />encircle(midwest_encircle_data.area, midwest_encircle_data.poptotal, ec=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"firebrick"</span>, fc=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"none"</span>, linewidth=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1.5</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Step 4: Decorations</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().set(xlim=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.0</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.1</span>), ylim=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">90000</span>),<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />              xlabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Area'</span>, ylabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Population'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.xticks(fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">12</span>); plt.yticks(fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">12</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Bubble Plot with Encircling"</span>, fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.legend(fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">12</span>)    <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()    <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>
<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />
25个常用Matplotlib图的Python代码,干货收藏!


3. 带线性回归最佳拟合线的散点图


如果你想了解两个变量如何相互改变,那么最合适的线就是要走的路。下图显示了数据中各组之间最佳拟合线的差异。要禁用分组并仅为整个数据集绘制一条最佳拟合线,请从下面的调用中删除该参数。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df_select = df.loc[df.cyl.isin([4,8]), :]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.set_style(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"white"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />gridobj = sns.lmplot(x=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"displ"</span>, y=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"hwy"</span>, hue=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"cyl"</span>, data=df_select, <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />                     height=7, aspect=1.6, robust=True, palette='tab10', <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />                     scatter_kws=dict(s=60, linewidths=.7, edgecolors='black'))<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decorations</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />gridobj.set(xlim=(0.5, 7.5), ylim=(0, 50))<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Scatterplot with line of best fit grouped by number of cylinders"</span>, fontsize=20)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>

25个常用Matplotlib图的Python代码,干货收藏!


每个回归线都在自己的列中


或者,您可以在其自己的列中显示每个组的最佳拟合线。你可以通过在里面设置参数来实现这一点。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df_select = df.loc[df.cyl.isin([4,8]), :]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Each line in its own column</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.set_style(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"white"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />gridobj = sns.lmplot(x=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"displ"</span>, y=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"hwy"</span>, <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />                     data=df_select, <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />                     height=7, <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />                     robust=True, <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />                     palette='Set1', <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />                     col=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"cyl"</span>,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />                     scatter_kws=dict(s=60, linewidths=.7, edgecolors='black'))<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decorations</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />gridobj.set(xlim=(0.5, 7.5), ylim=(0, 50))<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>

25个常用Matplotlib图的Python代码,干货收藏!


4. 抖动图


通常,多个数据点具有完全相同的X和Y值。结果,多个点相互绘制并隐藏。为避免这种情况,请稍微抖动点,以便您可以直观地看到它们。这很方便使用


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw Stripplot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />fig, ax = plt.subplots(figsize=(16,10), dpi= 80)    <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.stripplot(df.cty, df.hwy, jitter=0.25, size=8, ax=ax, linewidth=.5)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decorations</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.title('Use jittered plots to avoid overlapping of points', fontsize=22)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>

25个常用Matplotlib图的Python代码,干货收藏!


5. 计数图


避免点重叠问题的另一个选择是增加点的大小,这取决于该点中有多少点。因此,点的大小越大,周围的点的集中度就越大。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">df</span> = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df_counts = df.groupby([<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'hwy'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cty'</span>]).size().reset_index(name=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'counts'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw Stripplot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />fig, ax = plt.subplots(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)    <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.stripplot(df_counts.cty, df_counts.hwy, size=df_counts.counts*<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">2</span>, ax=ax)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decorations</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Counts Plot - Size of circle is bigger as more points overlap'</span>, fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


6. 边缘直方图


边缘直方图具有沿X和Y轴变量的直方图。这用于可视化X和Y之间的关系以及单独的X和Y的单变量分布。该图如果经常用于探索性数据分析(EDA)。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Create Fig and gridspec</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />fig = plt.figure(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />grid = plt.GridSpec(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">4</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">4</span>, hspace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">5</span>, wspace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">2</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Define the axes</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_main = fig.add_subplot(grid[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">:-1</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">:-1</span>])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_right = fig.add_subplot(grid[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">:-1</span>, -<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>], xticklabels=[], yticklabels=[])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_bottom = fig.add_subplot(grid[-<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0:-1</span>], xticklabels=[], yticklabels=[])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Scatterplot on main ax</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_main.scatter(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'displ'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'hwy'</span>, s=df.cty*<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">4</span>, c=df.manufacturer.astype(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'category'</span>).cat.codes, alpha=.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">9</span>, data=df, cmap=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"tab10"</span>, edgecolors=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'gray'</span>, linewidths=.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">5</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># histogram on the right</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_bottom.hist(df.displ, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">40</span>, histtype=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'stepfilled'</span>, orientation=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'vertical'</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'deeppink'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_bottom.invert_yaxis()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># histogram in the bottom</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_right.hist(df.hwy, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">40</span>, histtype=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'stepfilled'</span>, orientation=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'horizontal'</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'deeppink'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decorations</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_main.set(title=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Scatterplot with Histograms <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /> displ vs hwy'</span>, xlabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'displ'</span>, ylabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'hwy'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_main.title.set_fontsize(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">20</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> item <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> ([ax_main.xaxis.label, ax_main.yaxis.label] + ax_main.get_xticklabels() + ax_main.get_yticklabels()):<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    item.set_fontsize(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />xlabels = ax_main.get_xticks().tolist()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_main.set_xticklabels(xlabels)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>

25个常用Matplotlib图的Python代码,干货收藏!


7.边缘箱形图


边缘箱图与边缘直方图具有相似的用途。然而,箱线图有助于精确定位X和Y的中位数,第25和第75百分位数。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://raw.githubusercontent.com/selva86/datasets/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Create Fig and gridspec</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />fig = plt.figure(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />grid = plt.GridSpec(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">4</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">4</span>, hspace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">5</span>, wspace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">2</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Define the axes</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_main = fig.add_subplot(grid[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">:-1</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">:-1</span>])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_right = fig.add_subplot(grid[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">:-1</span>, -<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>], xticklabels=[], yticklabels=[])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_bottom = fig.add_subplot(grid[-<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0:-1</span>], xticklabels=[], yticklabels=[])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Scatterplot on main ax</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_main.scatter(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'displ'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'hwy'</span>, s=df.cty*<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">5</span>, c=df.manufacturer.astype(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'category'</span>).cat.codes, alpha=.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">9</span>, data=df, cmap=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Set1"</span>, edgecolors=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'black'</span>, linewidths=.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">5</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Add a graph in each part</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.boxplot(df.hwy, ax=ax_right, orient=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"v"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.boxplot(df.displ, ax=ax_bottom, orient=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"h"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decorations ------------------</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Remove x axis name for the boxplot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_bottom.set(xlabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">''</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_right.set(ylabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">''</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Main Title, Xlabel and YLabel</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_main.set(title=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Scatterplot with Histograms <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /> displ vs hwy'</span>, xlabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'displ'</span>, ylabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'hwy'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Set font size of different components</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax_main.title.set_fontsize(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">20</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> item <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> ([ax_main.xaxis.label, ax_main.yaxis.label] + ax_main.get_xticklabels() + ax_main.get_yticklabels()):<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    item.set_fontsize(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>

25个常用Matplotlib图的Python代码,干货收藏!


8. 相关图


Correlogram用于直观地查看给定数据帧(或2D数组)中所有可能的数值变量对之间的相关度量。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import Dataset</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">df</span> = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/mtcars.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.figure(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">12</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.heatmap(df.corr(), xticklabels=df.corr().columns, yticklabels=df.corr().columns, cmap=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'RdYlGn'</span>, center=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, annot=True)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decorations</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Correlogram of mtcars'</span>, fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.xticks(fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">12</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.yticks(fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">12</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>

25个常用Matplotlib图的Python代码,干货收藏!


9. 矩阵图


成对图是探索性分析中的最爱,以理解所有可能的数字变量对之间的关系。它是双变量分析的必备工具。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Load Dataset</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = sns.load_dataset('iris')<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.figure(figsize=(10,8), dpi= 80)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.pairplot(df, kind=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"scatter"</span>, hue=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"species"</span>, plot_kws=dict(s=80, edgecolor=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"white"</span>, linewidth=2.5))<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Load Dataset</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = sns.load_dataset('iris')<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.figure(figsize=(10,8), dpi= 80)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.pairplot(df, kind=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"reg"</span>, hue=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"species"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


偏差


10. 发散型条形图


如果您想根据单个指标查看项目的变化情况,并可视化此差异的顺序和数量,那么发散条是一个很好的工具。它有助于快速区分数据中组的性能,并且非常直观,并且可以立即传达这一点。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Prepare Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/mtcars.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />x = df.loc[:, [<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg'</span>]]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg_z'</span>] = (x - x.mean())/x.std()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'colors'</span>] = [<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'red'</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">if</span> x < <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">else</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'green'</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> x <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg_z'</span>]]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.sort_values(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg_z'</span>, inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.reset_index(inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.figure(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.hlines(y=df.index, xmin=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, xmax=df.mpg_z, color=df.colors, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.4</span>, linewidth=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">5</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decorations</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().set(ylabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'$Model$'</span>, xlabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'$Mileage$'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.yticks(df.index, df.cars, fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">12</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Diverging Bars of Car Mileage'</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">20</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.grid(linestyle=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'--'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.5</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


11. 发散型文本


分散的文本类似于发散条,如果你想以一种漂亮和可呈现的方式显示图表中每个项目的价值,它更喜欢。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Prepare Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/mtcars.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />x = df.loc[:, [<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg'</span>]]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg_z'</span>] = (x - x.mean())/x.std()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'colors'</span>] = [<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'red'</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">if</span> x < <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">else</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'green'</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> x <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg_z'</span>]]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.sort_values(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg_z'</span>, inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.reset_index(inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.figure(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.hlines(y=df.index, xmin=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, xmax=df.mpg_z)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> x, y, tex <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> zip(df.mpg_z, df.index, df.mpg_z):<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    t = plt.text(x, y, round(tex, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">2</span>), horizontalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'right'</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">if</span> x < <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">else</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'left'</span>, <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />                 verticalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'center'</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'color'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'red'</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">if</span> x < <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">else</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'green'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decorations    </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.yticks(df.index, df.cars, fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">12</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Diverging Text Bars of Car Mileage'</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">20</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.grid(linestyle=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'--'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.5</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.xlim(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">-2.5</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">2.5</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


12. 发散型包点图


发散点图也类似于发散条。然而,与发散条相比,条的不存在减少了组之间的对比度和差异。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Prepare Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/mtcars.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />x = df.loc[:, [<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg'</span>]]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg_z'</span>] = (x - x.mean())/x.std()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'colors'</span>] = [<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'red'</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">if</span> x < <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">else</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'darkgreen'</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> x <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg_z'</span>]]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.sort_values(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg_z'</span>, inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.reset_index(inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.figure(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.scatter(df.mpg_z, df.index, s=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">450</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.6</span>, color=df.colors)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> x, y, tex <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> zip(df.mpg_z, df.index, df.mpg_z):<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    t = plt.text(x, y, round(tex, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>), horizontalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'center'</span>, <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />                 verticalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'center'</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'color'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'white'</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decorations</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Lighten borders</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().spines[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"top"</span>].set_alpha(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.3</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().spines[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"bottom"</span>].set_alpha(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.3</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().spines[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"right"</span>].set_alpha(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.3</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().spines[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"left"</span>].set_alpha(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.3</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.yticks(df.index, df.cars)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Diverging Dotplot of Car Mileage'</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">20</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.xlabel(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'$Mileage$'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.grid(linestyle=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'--'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.5</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.xlim(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">-2.5</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">2.5</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


13. 带标记的发散型棒棒糖图


带标记的棒棒糖通过强调您想要引起注意的任何重要数据点并在图表中适当地给出推理,提供了一种可视化分歧的灵活方式。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Prepare Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/mtcars.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />x = df.loc[:, [<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg'</span>]]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg_z'</span>] = (x - x.mean())/x.std()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'colors'</span>] = <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'black'</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># color fiat differently</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.loc[df.cars == <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Fiat X1-9'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'colors'</span>] = <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'darkorange'</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.sort_values(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'mpg_z'</span>, inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.reset_index(inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> matplotlib.patches <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">as</span> patches<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.figure(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.hlines(y=df.index, xmin=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, xmax=df.mpg_z, color=df.colors, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.4</span>, linewidth=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.scatter(df.mpg_z, df.index, color=df.colors, s=[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">600</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">if</span> x == <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Fiat X1-9'</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">else</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">300</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> x <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> df.cars], alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.6</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.yticks(df.index, df.cars)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.xticks(fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">12</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Annotate</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.annotate(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Mercedes Models'</span>, xy=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.0</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">11.0</span>), xytext=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1.0</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">11</span>), xycoords=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'data'</span>, <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />            fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">15</span>, ha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'center'</span>, va=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'center'</span>,<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />            bbox=dict(boxstyle=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'square'</span>, fc=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'firebrick'</span>),<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />            arrowprops=dict(arrowstyle=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'-[, widthB=2.0, lengthB=1.5'</span>, lw=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">2.0</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'steelblue'</span>), color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'white'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Add Patches</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />p1 = patches.Rectangle((<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">-2.0</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">-1</span>), width=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.3</span>, height=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">3</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.2</span>, facecolor=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'red'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />p2 = patches.Rectangle((<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1.5</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">27</span>), width=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.8</span>, height=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">5</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.2</span>, facecolor=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'green'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().add_patch(p1)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().add_patch(p2)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decorate</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Diverging Bars of Car Mileage'</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">20</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.grid(linestyle=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'--'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.5</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


14.面积图


通过对轴和线之间的区域进行着色,区域图不仅强调峰值和低谷,而且还强调高点和低点的持续时间。高点持续时间越长,线下面积越大。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> numpy <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">as</span> np<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> pandas <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">as</span> pd<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Prepare Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/economics.csv"</span>, parse_dates=[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'date'</span>]).head(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">100</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />x = np.arange(df.shape[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />y_returns = (df.psavert.diff().fillna(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>)/df.psavert.shift(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>)).fillna(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>) * <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">100</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.figure(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.fill_between(x[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>:], y_returns[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>:], <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, where=y_returns[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>:] >= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, facecolor=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'green'</span>, interpolate=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.fill_between(x[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>:], y_returns[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>:], <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, where=y_returns[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>:] <= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, facecolor=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'red'</span>, interpolate=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Annotate</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.annotate(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Peak <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />1975'</span>, xy=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">94.0</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">21.0</span>), xytext=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">88.0</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">28</span>),<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />             bbox=dict(boxstyle=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'square'</span>, fc=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'firebrick'</span>),<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />             arrowprops=dict(facecolor=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'steelblue'</span>, shrink=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.05</span>), fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">15</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'white'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decorations</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />xtickvals = [str(m)[:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">3</span>].upper()+<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"-"</span>+str(y) <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> y,m <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> zip(df.date.dt.year, df.date.dt.month_name())]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().set_xticks(x[::<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">6</span>])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().set_xticklabels(xtickvals[::<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">6</span>], rotation=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">90</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'horizontalalignment'</span>: <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'center'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'verticalalignment'</span>: <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'center_baseline'</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.ylim(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">-35</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">35</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.xlim(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">100</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Month Economics Return %"</span>, fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.ylabel(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Monthly returns %'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.grid(alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.5</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


排序


15. 有序条形图


有序条形图有效地传达了项目的排名顺序。但是,在图表上方添加度量标准的值,用户可以从图表本身获取精确信息。


<section style="padding: 0.5em;max-width: 100%;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Prepare Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df_raw = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = df_raw[[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cty'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'manufacturer'</span>]].groupby(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'manufacturer'</span>).apply(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">lambda</span> x: x.mean())<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.sort_values(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cty'</span>, inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.reset_index(inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> matplotlib.patches <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">as</span> patches<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />fig, ax = plt.subplots(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>), facecolor=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'white'</span>, dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.vlines(x=df.index, ymin=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, ymax=df.cty, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'firebrick'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>, linewidth=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">20</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Annotate Text</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> i, cty <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> enumerate(df.cty):<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    ax.text(i, cty+<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.5</span>, round(cty, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>), horizontalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'center'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Title, Label, Ticks and Ylim</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Bar Chart for Highway Mileage'</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set(ylabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Miles Per Gallon'</span>, ylim=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">30</span>))<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.xticks(df.index, df.manufacturer.str.upper(), rotation=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">60</span>, horizontalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'right'</span>, fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">12</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Add patches to color the X axis labels</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />p1 = patches.Rectangle((<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.57</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">-0.005</span>), width=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.33</span>, height=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.13</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.1</span>, facecolor=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'green'</span>, transform=fig.transFigure)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />p2 = patches.Rectangle((<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.124</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">-0.005</span>), width=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.446</span>, height=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.13</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.1</span>, facecolor=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'red'</span>, transform=fig.transFigure)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />fig.add_artist(p1)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />fig.add_artist(p2)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


16. 棒棒糖图


棒棒糖图表以一种视觉上令人愉悦的方式提供与有序条形图类似的目的。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Prepare Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df_raw = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = df_raw[[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cty'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'manufacturer'</span>]].groupby(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'manufacturer'</span>).apply(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">lambda</span> x: x.mean())<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.sort_values(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cty'</span>, inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.reset_index(inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />fig, ax = plt.subplots(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.vlines(x=df.index, ymin=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, ymax=df.cty, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'firebrick'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>, linewidth=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">2</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.scatter(x=df.index, y=df.cty, s=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">75</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'firebrick'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Title, Label, Ticks and Ylim</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Lollipop Chart for Highway Mileage'</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_ylabel(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Miles Per Gallon'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_xticks(df.index)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_xticklabels(df.manufacturer.str.upper(), rotation=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">60</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'horizontalalignment'</span>: <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'right'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">12</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_ylim(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">30</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Annotate</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> row <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> df.itertuples():<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    ax.text(row.Index, row.cty+<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.5</span>, s=round(row.cty, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">2</span>), horizontalalignment= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'center'</span>, verticalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'bottom'</span>, fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


17. 包点图


点图表传达了项目的排名顺序。由于它沿水平轴对齐,因此您可以更容易地看到点彼此之间的距离。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Prepare Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df_raw = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = df_raw[[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cty'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'manufacturer'</span>]].groupby(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'manufacturer'</span>).apply(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">lambda</span> x: x.mean())<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.sort_values(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cty'</span>, inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.reset_index(inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />fig, ax = plt.subplots(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.hlines(y=df.index, xmin=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">11</span>, xmax=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">26</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'gray'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>, linewidth=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, linestyles=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'dashdot'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.scatter(y=df.index, x=df.cty, s=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">75</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'firebrick'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Title, Label, Ticks and Ylim</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Dot Plot for Highway Mileage'</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_xlabel(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Miles Per Gallon'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_yticks(df.index)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_yticklabels(df.manufacturer.str.title(), fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'horizontalalignment'</span>: <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'right'</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_xlim(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">27</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


18. 坡度图


斜率图最适合比较给定人/项目的“之前”和“之后”位置。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;">import matplotlib.lines <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">as</span> mlines<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://raw.githubusercontent.com/selva86/datasets/master/gdppercap.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />left_label = [str(c) + <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">', '</span>+ str(round(y)) <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> c, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">y <span style="max-width: 100%;font-size: inherit;line-height: inherit;box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">zip</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">df.continent, df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'1952'</span>]</span>)]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />right_label </span>= [str(c) + <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">', '</span>+ str(round(y)) <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> c, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">y <span style="max-width: 100%;font-size: inherit;line-height: inherit;box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">zip</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">df.continent, df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'1957'</span>]</span>)]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />klass </span>= [<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'red'</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">if</span> (y1-y2) < <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">else</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'green'</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> y1, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">y2 <span style="max-width: 100%;font-size: inherit;line-height: inherit;box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">zip</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'1952'</span>], df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'1957'</span>]</span>)]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /># draw line<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /># https:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">//stackoverflow.com/questions/36470343/how-to-draw-a-line-with-matplotlib/36479941</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />def <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">newline</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">p1, p2, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'black'</span></span>):<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    ax </span>= plt.gca()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    l = mlines.Line2D([p1[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>],p2[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>]], [p1[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>],p2[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>]], color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'red'</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">if</span> p1[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>]-p2[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>] > <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">else</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'green'</span>, marker=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'o'</span>, markersize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">6</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    ax.add_line(l)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">return</span> l<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />fig, ax = plt.subplots(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>,figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Vertical Lines</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.vlines(x=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, ymin=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">500</span>, ymax=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">13000</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'black'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>, linewidth=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, linestyles=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'dotted'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.vlines(x=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">3</span>, ymin=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">500</span>, ymax=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">13000</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'black'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>, linewidth=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, linestyles=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'dotted'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Points</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.scatter(y=df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'1952'</span>], x=np.repeat(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, df.shape[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>]), s=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'black'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.scatter(y=df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'1957'</span>], x=np.repeat(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">3</span>, df.shape[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>]), s=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'black'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Line Segmentsand Annotation</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> p1, p2, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">c <span style="max-width: 100%;font-size: inherit;line-height: inherit;box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">zip</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'1952'</span>], df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'1957'</span>], df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'continent'</span>]</span>):<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">newline</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>,p1], [<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">3</span>,p2]</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    ax.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">text</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1-0.05</span>, p1, c + <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">', '</span> + str(round(p1</span>)), horizontalalignment</span>=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'right'</span>, verticalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'center'</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    ax.text(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">3</span>+<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.05</span>, p2, c + <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">', '</span> + str(round(p2)), horizontalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'left'</span>, verticalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'center'</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># 'Before' and 'After' Annotations</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.text(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1-0.05</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">13000</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'BEFORE'</span>, horizontalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'right'</span>, verticalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'center'</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">18</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'weight'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">700</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.text(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">3</span>+<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.05</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">13000</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'AFTER'</span>, horizontalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'left'</span>, verticalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'center'</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">18</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'weight'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">700</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decoration</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Slopechart: Comparing GDP Per Capita between 1952 vs 1957"</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">set</span>(xlim=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">4</span>), ylim=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14000</span>), ylabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Mean GDP Per Capita'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_xticks([<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">3</span>])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_xticklabels([<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"1952"</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"1957"</span>])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.yticks(np.arange(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">500</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">13000</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">2000</span>), fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">12</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Lighten borders</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().spines[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"top"</span>].set_alpha(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.0</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().spines[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"bottom"</span>].set_alpha(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.0</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().spines[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"right"</span>].set_alpha(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.0</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().spines[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"left"</span>].set_alpha(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.0</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


19. 哑铃图


哑铃图传达各种项目的“前”和“后”位置以及项目的排序。如果您想要将特定项目/计划对不同对象的影响可视化,那么它非常有用。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> matplotlib.lines <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">as</span> mlines<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://raw.githubusercontent.com/selva86/datasets/master/health.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.sort_values(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'pct_2014'</span>, inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.reset_index(inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Func to draw line segment</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">def</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">newline</span><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">(p1, p2, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'black'</span>)</span>:</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    ax = plt.gca()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    l = mlines.Line2D([p1[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>],p2[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>]], [p1[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>],p2[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>]], color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'skyblue'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    ax.add_line(l)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">return</span> l<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Figure and Axes</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />fig, ax = plt.subplots(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>,figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>), facecolor=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'#f7f7f7'</span>, dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Vertical Lines</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.vlines(x=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.05</span>, ymin=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, ymax=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">26</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'black'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, linewidth=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, linestyles=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'dotted'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.vlines(x=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.10</span>, ymin=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, ymax=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">26</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'black'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, linewidth=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, linestyles=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'dotted'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.vlines(x=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.15</span>, ymin=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, ymax=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">26</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'black'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, linewidth=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, linestyles=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'dotted'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.vlines(x=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.20</span>, ymin=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, ymax=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">26</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'black'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, linewidth=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, linestyles=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'dotted'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Points</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.scatter(y=df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'index'</span>], x=df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'pct_2013'</span>], s=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">50</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'#0e668b'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.scatter(y=df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'index'</span>], x=df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'pct_2014'</span>], s=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">50</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'#a3c4dc'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Line Segments</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> i, p1, p2 <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> zip(df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'index'</span>], df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'pct_2013'</span>], df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'pct_2014'</span>]):<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    newline([p1, i], [p2, i])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decoration</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_facecolor(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'#f7f7f7'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Dumbell Chart: Pct Change - 2013 vs 2014"</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set(xlim=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.25</span>), ylim=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">-1</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">27</span>), ylabel=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Mean GDP Per Capita'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_xticks([<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.05</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.1</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.15</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.20</span>])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_xticklabels([<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'5%'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'15%'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'20%'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'25%'</span>])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_xticklabels([<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'5%'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'15%'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'20%'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'25%'</span>])    <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


分配


20. 连续变量的直方图


直方图显示给定变量的频率分布。下面的表示基于分类变量对频率条进行分组,从而更好地了解连续变量和串联变量。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Prepare data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />x_var = <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'displ'</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />groupby_var = <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'class'</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df_agg = df.loc[:, [x_var, groupby_var]].groupby(groupby_var)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />vals = [df[x_var].values.tolist() <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> i, df <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> df_agg]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.figure(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">9</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />colors = [plt.cm.Spectral(i/<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">float</span>(len(vals)<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">-1</span>)) <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> i <span style="max-width: 100%;font-size: inherit;line-height: inherit;box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">range</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">len(vals</span>))]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />n, bins, patches </span>= plt.hist(vals, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">30</span>, stacked=True, density=False, color=colors[:len(vals)])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decoration</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.legend({<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">group</span>:col <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">group</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">col <span style="max-width: 100%;font-size: inherit;line-height: inherit;box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">zip</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">np.unique(df[groupby_var]</span>).<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">tolist</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"></span>), colors[:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">len</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">vals</span>)])})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">title</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">f<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Stacked Histogram of ${x_var}$ colored by ${groupby_var}$"</span>, fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span></span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">xlabel</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">x_var</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">ylabel</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Frequency"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">ylim</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">25</span></span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">xticks</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">ticks=bins[::<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">3</span>], labels=[round(b,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span></span>) <span style="max-width: 100%;font-size: inherit;line-height: inherit;box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> b <span style="max-width: 100%;font-size: inherit;line-height: inherit;box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> bins[::3]])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">show</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"></span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></span></section>


25个常用Matplotlib图的Python代码,干货收藏!


21. 类型变量的直方图


分类变量的直方图显示该变量的频率分布。通过对条形图进行着色,您可以将分布与表示颜色的另一个分类变量相关联。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Prepare data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />x_var = <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'manufacturer'</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />groupby_var = <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'class'</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df_agg = df.loc[:, [x_var, groupby_var]].groupby(groupby_var)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />vals = [df[x_var].values.tolist() <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> i, df <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> df_agg]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.figure(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">9</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />colors = [plt.cm.Spectral(i/<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">float</span>(len(vals)<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">-1</span>)) <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> i <span style="max-width: 100%;font-size: inherit;line-height: inherit;box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">range</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">len(vals</span>))]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />n, bins, patches </span>= plt.hist(vals, df[x_var].unique().__len__(), stacked=True, density=False, color=colors[:len(vals)])<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(91, 218, 237);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decoration</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.legend({<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">group</span>:col <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">group</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">col <span style="max-width: 100%;font-size: inherit;line-height: inherit;box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">zip</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">np.unique(df[groupby_var]</span>).<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">tolist</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"></span>), colors[:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">len</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">vals</span>)])})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">title</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">f<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Stacked Histogram of ${x_var}$ colored by ${groupby_var}$"</span>, fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span></span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">xlabel</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">x_var</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">ylabel</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Frequency"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">ylim</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">40</span></span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">xticks</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">ticks=bins, labels=np.unique(df[x_var]</span>).<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">tolist</span>(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"></span>), rotation</span>=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">90</span>, horizontalalignment=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'left'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


22. 密度图


密度图是一种常用工具,可视化连续变量的分布。通过“响应”变量对它们进行分组,您可以检查X和Y之间的关系。以下情况,如果出于代表性目的来描述城市里程的分布如何随着汽缸数的变化而变化。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw Plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.figure(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.kdeplot(df.loc[df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cyl'</span>] == <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">4</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"cty"</span>], shade=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"g"</span>, label=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Cyl=4"</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.kdeplot(df.loc[df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cyl'</span>] == <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">5</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"cty"</span>], shade=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"deeppink"</span>, label=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Cyl=5"</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.kdeplot(df.loc[df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cyl'</span>] == <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">6</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"cty"</span>], shade=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"dodgerblue"</span>, label=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Cyl=6"</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.kdeplot(df.loc[df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cyl'</span>] == <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">8</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"cty"</span>], shade=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"orange"</span>, label=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Cyl=8"</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decoration</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Density Plot of City Mileage by n_Cylinders'</span>, fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.legend()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


23. 直方密度线图


带有直方图的密度曲线将两个图表传达的集体信息汇集在一起,这样您就可以将它们放在一个图形而不是两个图形中。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw Plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.figure(figsize=(13,10), dpi= 80)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.distplot(df.loc[df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'class'</span>] == <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'compact'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"cty"</span>], color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"dodgerblue"</span>, label=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Compact"</span>, hist_kws={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'alpha'</span>:.7}, kde_kws={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'linewidth'</span>:3})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.distplot(df.loc[df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'class'</span>] == <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'suv'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"cty"</span>], color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"orange"</span>, label=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"SUV"</span>, hist_kws={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'alpha'</span>:.7}, kde_kws={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'linewidth'</span>:3})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />sns.distplot(df.loc[df[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'class'</span>] == <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'minivan'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"cty"</span>], color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"g"</span>, label=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"minivan"</span>, hist_kws={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'alpha'</span>:.7}, kde_kws={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'linewidth'</span>:3})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.ylim(0, 0.35)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decoration</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Density Plot of City Mileage by Vehicle Type'</span>, fontsize=22)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.legend()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


24. Joy Plot


Joy Plot允许不同组的密度曲线重叠,这是一种可视化相对于彼此的大量组的分布的好方法。它看起来很悦目,并清楚地传达了正确的信息。它可以使用joypy基于的包来轻松构建matplotlib。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># !pip install joypy</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Import Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />mpg = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw Plot</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.figure(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />fig, axes = joypy.joyplot(mpg, column=[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'hwy'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cty'</span>], <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">by</span>=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"class"</span>, ylim=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'own'</span>, figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">14</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>))<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decoration</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Joy Plot of City and Highway Mileage by Class'</span>, fontsize=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>


25个常用Matplotlib图的Python代码,干货收藏!


25. 分布式点图


分布点图显示按组分割的点的单变量分布。点数越暗,该区域的数据点集中度越高。通过对中位数进行不同着色,组的真实定位立即变得明显。


<section style="padding: 0.5em;max-width: 100%;min-height: 1em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background-color: rgb(40, 43, 46);margin-left: 0px;margin-right: 0px;box-sizing: border-box !important;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">import</span> matplotlib.patches <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">as</span> mpatches<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Prepare Data</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df_raw = pd.read_csv(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />cyl_colors = {<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">4</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'tab:red'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">5</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'tab:green'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">6</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'tab:blue'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">8</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'tab:orange'</span>}<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df_raw[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cyl_color'</span>] = df_raw.cyl.map(cyl_colors)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Mean and Median city mileage by make</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df = df_raw[[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cty'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'manufacturer'</span>]].groupby(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'manufacturer'</span>).apply(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">lambda</span> x: x.mean())<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.sort_values(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cty'</span>, ascending=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">False</span>, inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df.reset_index(inplace=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />df_median = df_raw[[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cty'</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'manufacturer'</span>]].groupby(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'manufacturer'</span>).apply(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">lambda</span> x: x.median())<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw horizontal lines</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />fig, ax = plt.subplots(figsize=(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">16</span>,<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>), dpi= <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">80</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.hlines(y=df.index, xmin=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, xmax=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">40</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'gray'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.5</span>, linewidth=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.5</span>, linestyles=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'dashdot'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Draw the Dots</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">for</span> i, make <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">in</span> enumerate(df.manufacturer):<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    df_make = df_raw.loc[df_raw.manufacturer==make, :]<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    ax.scatter(y=np.repeat(i, df_make.shape[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0</span>]), x=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cty'</span>, data=df_make, s=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">75</span>, edgecolors=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'gray'</span>, c=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'w'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.5</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />    ax.scatter(y=i, x=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'cty'</span>, data=df_median.loc[df_median.index==make, :], s=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">75</span>, c=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'firebrick'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Annotate    </span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.text(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">33</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">13</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"$red ; dots ; are ; the : median$"</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">12</span>}, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'firebrick'</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /><span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;"># Decorations</span><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />red_patch = plt.plot([],[], marker=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"o"</span>, ms=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">10</span>, ls=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">""</span>, mec=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">None</span>, color=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'firebrick'</span>, label=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"Median"</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.legend(handles=red_patch)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_title(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Distribution of City Mileage by Make'</span>, fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'size'</span>:<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">22</span>})<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_xlabel(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'Miles Per Gallon (City)'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_yticks(df.index)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_yticklabels(df.manufacturer.str.title(), fontdict={<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'horizontalalignment'</span>: <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'right'</span>}, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />ax.set_xlim(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, <span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">40</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.xticks(alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">0.7</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().spines[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"top"</span>].set_visible(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">False</span>)    <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().spines[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"bottom"</span>].set_visible(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">False</span>)    <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().spines[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"right"</span>].set_visible(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">False</span>)    <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.gca().spines[<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">"left"</span>].set_visible(<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">False</span>)   <br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.grid(axis=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">'both'</span>, alpha=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.4</span>, linewidth=<span style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);box-sizing: border-box !important;overflow-wrap: inherit !important;word-break: inherit !important;">.1</span>)<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  />plt.show()<br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></section>



万水千山总是情,点个 👍 行不行


<p style="max-width: 100%;min-height: 1em;caret-color: rgb(51, 51, 51);font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;color: rgb(2, 30, 170);box-sizing: border-box !important;overflow-wrap: break-word !important;"></span></strong></p><pre data-darkmode-bgcolor="rgb(36, 36, 36)" data-style="background-color: rgb(255, 255, 255); color: rgba(230, 230, 230, 0.9); letter-spacing: 0.544px; text-size-adjust: auto; font-size: 16px; text-align: center; word-spacing: 1.6px;" data-darkmode-color="rgba(230, 230, 230, 0.9)" data-darkmode-original-color="rgba(230, 230, 230, 0.9)" data-darkmode-original-bgcolor="rgb(255, 255, 255)" data-darkmode-bgcolor-15862411819306="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15862411819306="rgb(255, 255, 255)" data-darkmode-color-15862411819306="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15862411819306="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15862671987026="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15862671987026="rgb(255, 255, 255)" data-darkmode-color-15862671987026="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15862671987026="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15864118999603="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15864118999603="rgb(255, 255, 255)" data-darkmode-color-15864118999603="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15864118999603="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15864940858736="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15864940858736="rgb(255, 255, 255)" data-darkmode-color-15864940858736="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15864940858736="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15869584691402="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15869584691402="rgb(255, 255, 255)" data-darkmode-color-15869584691402="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15869584691402="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15869584691739="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15869584691739="rgb(255, 255, 255)" data-darkmode-color-15869584691739="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15869584691739="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15873005456075="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15873005456075="rgb(255, 255, 255)" data-darkmode-color-15873005456075="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15873005456075="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15873005456615="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15873005456615="rgb(255, 255, 255)" data-darkmode-color-15873005456615="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15873005456615="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-15886839320558="rgb(36, 36, 36)" data-darkmode-original-bgcolor-15886839320558="rgb(255, 255, 255)" data-darkmode-color-15886839320558="rgba(230, 230, 230, 0.9)" data-darkmode-original-color-15886839320558="rgba(230, 230, 230, 0.9)" data-darkmode-color-159923607914210="rgba(163, 163, 163, 0.9)" data-darkmode-original-color-159923607914210="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-159923607914210="rgb(25, 25, 25)" data-darkmode-original-bgcolor-159923607914210="rgb(255, 255, 255)" class="js_darkmode__61" data-darkmode-bgcolor-160008070860010="rgb(25, 25, 25)" data-darkmode-original-bgcolor-160008070860010="rgb(255, 255, 255)" data-darkmode-color-160008070860010="rgba(163, 163, 163, 0.9)" data-darkmode-original-color-160008070860010="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-16072664870629="rgb(25, 25, 25)" data-darkmode-original-bgcolor-16072664870629="rgb(255, 255, 255)" data-darkmode-color-16072664870629="rgba(163, 163, 163, 0.9)" data-darkmode-original-color-16072664870629="rgba(230, 230, 230, 0.9)" data-darkmode-bgcolor-16073544711184="rgb(25, 25, 25)" data-darkmode-original-bgcolor-16073544711184="rgb(255, 255, 255)" data-darkmode-color-16073544711184="rgba(163, 163, 163, 0.9)" data-darkmode-original-color-16073544711184="rgba(230, 230, 230, 0.9)"><p style="max-width: 100%;min-height: 1em;caret-color: rgb(51, 51, 51);font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;color: rgb(2, 30, 170);box-sizing: border-box !important;overflow-wrap: break-word !important;">推荐阅读:</span></strong></p><p style="max-width: 100%;min-height: 1em;caret-color: rgb(51, 51, 51);font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;color: rgb(2, 30, 170);box-sizing: border-box !important;overflow-wrap: break-word !important;">入门: </span><span style="max-width: 100%;color: rgb(2, 30, 170);text-decoration: underline;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">最全的零基础学Python的问题</span><span style="max-width: 100%;font-size: 15px;color: rgb(2, 30, 170);box-sizing: border-box !important;overflow-wrap: break-word !important;">  | </span><span style="max-width: 100%;color: rgb(2, 30, 170);text-decoration: underline;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">零基础学了8个月的Python </span> <span style="max-width: 100%;color: rgb(2, 30, 170);text-decoration: underline;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;">|</span> <span style="max-width: 100%;text-decoration: underline;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">实战项目</span> <span style="max-width: 100%;text-decoration: underline;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">|学Python就是这条捷径</span></p><p style="max-width: 100%;min-height: 1em;caret-color: rgb(51, 51, 51);font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></p><p style="max-width: 100%;min-height: 1em;caret-color: rgb(51, 51, 51);font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;color: rgb(2, 30, 170);box-sizing: border-box !important;overflow-wrap: break-word !important;">干货:</span><span style="max-width: 100%;font-size: 15px;color: rgb(2, 30, 170);text-decoration: underline;box-sizing: border-box !important;overflow-wrap: break-word !important;">爬取豆瓣短评,电影《后来的我们》</span> | <span style="max-width: 100%;font-size: 14px;text-decoration: underline;box-sizing: border-box !important;overflow-wrap: break-word !important;">38年NBA最佳球员分析 </span><span style="max-width: 100%;font-size: 15px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;text-decoration: underline;">|</span>   </span><span style="max-width: 100%;font-size: 15px;text-decoration: underline;box-sizing: border-box !important;overflow-wrap: break-word !important;">从万众期待到口碑扑街!唐探3令人失望</span>  | 笑看新倚天屠龙记 | 灯谜答题王 |<span style="font-size: 14px;text-decoration: underline;">用Python做个海量小姐姐素描图 |</span></p><p style="max-width: 100%;min-height: 1em;caret-color: rgb(51, 51, 51);font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></p><p style="max-width: 100%;min-height: 1em;caret-color: rgb(51, 51, 51);font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 15px;color: rgb(2, 30, 170);box-sizing: border-box !important;overflow-wrap: break-word !important;">趣味:</span><span style="max-width: 100%;color: rgb(2, 30, 170);text-decoration: underline;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">弹球游戏</span>  | <span style="max-width: 100%;text-decoration: underline;font-size: 14px;box-sizing: border-box !important;overflow-wrap: break-word !important;">九宫格</span>  | 漂亮的花 | 两百行Python《天天酷跑》游戏!</p><p style="max-width: 100%;min-height: 1em;caret-color: rgb(51, 51, 51);font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"  /></p><p style="max-width: 100%;min-height: 1em;caret-color: rgb(51, 51, 51);font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-size-adjust: auto;background-color: rgb(255, 255, 255);box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(2, 30, 170);box-sizing: border-box !important;overflow-wrap: break-word !important;">AI:</span> 会做诗的机器人 | 给图片上色 | 预测收入 | 碟中谍这么火,我用机器学习做个迷你推荐系统电影</p>

年度爆款文案

  • 1).卧槽!Pdf转Word用Python轻松搞定

  • 2).学Python真香!我用100行代码做了个网站,帮人PS旅行图片,赚个鸡腿吃

  • 3).首播过亿,火爆全网,我分析了《乘风破浪的姐姐》,发现了这些秘密 

  • 4).80行代码!用Python做一个哆来A梦分身 

  • 5).你必须掌握的20个python代码,短小精悍,用处无穷 

  • 6).30个Python奇淫技巧集 

  • 7).我总结的80页《菜鸟学Python精选干货.pdf》,都是干货 

  • 8).再见Python!我要学Go了!2500字深度分析!

  • 9).发现一个舔狗福利!这个Python爬虫神器太爽了,自动下载妹子图片

本篇文章来源于: 菜鸟学Python

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享