python绘制发散型柱状图+误差阴影时间序列图+双坐标系时间序列图+绘制金字塔图

 更新时间:2022年08月16日 16:32:49   作者:不再依然07  
这篇文章主要介绍了python绘制发散型柱状图+误差阴影时间序列图+双坐标系时间序列图+绘制金字塔图,详细的内容需要的小伙伴可以参考一下下面文章内容

1.绘制发散型柱状图

python绘制发散型柱状图,展示单个指标的变化的顺序和数量,在柱子上添加了数值文本。

实现代码:

import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings(action='once')
df = pd.read_csv("C:\工作\学习\数据杂坛/datasets/mtcars.csv")
x = df.loc[:, ['mpg']]
df['mpg_z'] = (x - x.mean()) / x.std()
df['colors'] = ['red' if x < 0 else 'green' for x in df['mpg_z']]
df.sort_values('mpg_z', inplace=True)
df.reset_index(inplace=True)
# Draw plot
plt.figure(figsize=(10, 6), dpi=80)
plt.hlines(y=df.index,
           xmin=0,
           xmax=df.mpg_z,
           color=df.colors,
           alpha=0.8,
           linewidth=5)
for x, y, tex in zip(df.mpg_z, df.index, df.mpg_z):
    t = plt.text(x, y, round(tex, 2), horizontalalignment='right' if x < 0 else 'left',

                 verticalalignment='center', fontdict={'color':'black' if x < 0 else 'black', 'size':10})

# Decorations

plt.gca().set(ylabel='$Model', xlabel='$Mileage')
plt.yticks(df.index, df.cars, fontsize=12)
plt.xticks(fontsize=12)
plt.title('Diverging Bars of Car Mileage')
plt.grid(linestyle='--', alpha=0.5)
plt.show()

实现效果:

2.绘制带误差阴影的时间序列图

实现功能:

python绘制带误差阴影的时间序列图。

实现代码:

from scipy.stats import sem
import pandas as pd
import matplotlib.pyplot as plt
# Import Data
df_raw = pd.read_csv('F:\数据杂坛\datasets\orders_45d.csv',
                     parse_dates=['purchase_time', 'purchase_date'])

# Prepare Data: Daily Mean and SE Bands
df_mean = df_raw.groupby('purchase_date').quantity.mean()
df_se = df_raw.groupby('purchase_date').quantity.apply(sem).mul(1.96)

# Plot
plt.figure(figsize=(10, 6), dpi=80)
plt.ylabel("Daily Orders", fontsize=12)
x = [d.date().strftime('%Y-%m-%d') for d in df_mean.index]
plt.plot(x, df_mean, color="#c72e29", lw=2)
plt.fill_between(x, df_mean - df_se, df_mean + df_se, color="#f8f2e4")

# Decorations
# Lighten borders
plt.gca().spines["top"].set_alpha(0)
plt.gca().spines["bottom"].set_alpha(1)
plt.gca().spines["right"].set_alpha(0)
plt.gca().spines["left"].set_alpha(1)
plt.xticks(x[::6], [str(d) for d in x[::6]], fontsize=12)
plt.title("Daily Order Quantity of Brazilian Retail with Error Bands (95% confidence)",fontsize=14)

# Axis limits
s, e = plt.gca().get_xlim()
plt.xlim(s, e - 2)
plt.ylim(4, 10)

# Draw Horizontal Tick lines
for y in range(5, 10, 1):
    plt.hlines(y,
               xmin=s,
               xmax=e,
               colors='black',
               alpha=0.5,
               linestyles="--",
               lw=0.5)

plt.show()

实现效果:

3.绘制双坐标系时间序列图

实现功能:

python绘制双坐标系(双变量)时间序列图。

实现代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Import Data
df = pd.read_csv("F:\数据杂坛\datasets\economics.csv")

x = df['date']
y1 = df['psavert']
y2 = df['unemploy']

# Plot Line1 (Left Y Axis)
fig, ax1 = plt.subplots(1, 1, figsize=(12, 6), dpi=100)
ax1.plot(x, y1, color='tab:red')

# Plot Line2 (Right Y Axis)
ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis
ax2.plot(x, y2, color='tab:blue')

# Decorations
# ax1 (left Y axis)
ax1.set_xlabel('Year', fontsize=18)
ax1.tick_params(axis='x', rotation=70, labelsize=12)
ax1.set_ylabel('Personal Savings Rate', color='#dc2624', fontsize=16)
ax1.tick_params(axis='y', rotation=0, labelcolor='#dc2624')
ax1.grid(alpha=.4)

# ax2 (right Y axis)
ax2.set_ylabel("Unemployed (1000's)", color='#01a2d9', fontsize=16)
ax2.tick_params(axis='y', labelcolor='#01a2d9')
ax2.set_xticks(np.arange(0, len(x), 60))
ax2.set_xticklabels(x [::60], rotation=90, fontdict={'fontsize': 10})
ax2.set_title(
    "Personal Savings Rate vs Unemployed: Plotting in Secondary Y Axis",
    fontsize=18)
fig.tight_layout()
plt.show()

实现效果:

4.绘制金字塔图

实现功能:

python绘制金字塔图,一种排过序的分组水平柱状图barplot,可很好展示不同分组之间的差异,可可视化逐级过滤或者漏斗的每个阶段。

实现代码:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Read data
df = pd.read_csv("D:\数据杂坛\datasets\email_campaign_funnel.csv")

# Draw Plot
plt.figure()
group_col = 'Gender'
order_of_bars = df.Stage.unique()[::-1]
colors = [
    plt.cm.Set1(i / float(len(df[group_col].unique()) - 1))
    for i in range(len(df[group_col].unique()))
]

for c, group in zip(colors, df[group_col].unique()):
    sns.barplot(x='Users',
                y='Stage',
                data=df.loc[df[group_col] == group, :],
                order=order_of_bars,
                color=c,
                label=group)

# Decorations
plt.xlabel("$Users$")
plt.ylabel("Stage of Purchase")
plt.yticks(fontsize=12)
plt.title("Population Pyramid of the Marketing Funnel", fontsize=18)
plt.legend()
plt.savefig('C:\工作\学习\数据杂坛\素材\\0815\金字塔', dpi=300, bbox_inches = 'tight')
plt.show()

实现效果:

到此这篇关于python绘制发散型柱状图+误差阴影时间序列图+双坐标系时间序列图+绘制金字塔图的文章就介绍到这了,更多相关Python图绘制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python操作Excel插入删除行的方法

    Python操作Excel插入删除行的方法

    今天小编就为大家分享一篇Python操作Excel插入删除行的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • python使用for...else跳出双层嵌套循环的方法实例

    python使用for...else跳出双层嵌套循环的方法实例

    这篇文章主要给大家介绍了关于python使用for...else跳出双层嵌套循环的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-05-05
  • Python字符编码判断方法分析

    Python字符编码判断方法分析

    这篇文章主要介绍了Python字符编码判断方法,结合实例形式分析了Python字符编码的判断技巧,并给出了chardet的安装与使用方法,需要的朋友可以参考下
    2016-07-07
  • Python源码学习之PyObject和PyTypeObject

    Python源码学习之PyObject和PyTypeObject

    今天给大家带来的是关于Python源码的相关知识学习,文章围绕着PyObject和PyTypeObject展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • Windows10+anacond+GPU+pytorch安装详细过程

    Windows10+anacond+GPU+pytorch安装详细过程

    这篇文章主要介绍了Windows10+anacond+GPU+pytorch安装详细过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03
  • python中二维阵列的变换实例

    python中二维阵列的变换实例

    这篇文章主要介绍了python中二维阵列的变换实例,通过对比两种不同的方法分析了二维阵列变换的实现方法,需要的朋友可以参考下
    2014-10-10
  • python机器学习朴素贝叶斯算法及模型的选择和调优详解

    python机器学习朴素贝叶斯算法及模型的选择和调优详解

    这篇文章主要为大家介绍了python机器学习朴素贝叶斯及模型的选择和调优示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2021-11-11
  • python两种遍历字典(dict)的方法比较

    python两种遍历字典(dict)的方法比较

    这篇文章主要介绍了python两种遍历字典(dict)的方法比较,同时介绍了dict遍历中带括号与不带括号的性能问题,需要的朋友可以参考下
    2014-05-05
  • pyinstaller打包后偶尔出现黑窗口一闪而过的问题及解决

    pyinstaller打包后偶尔出现黑窗口一闪而过的问题及解决

    这篇文章主要介绍了pyinstaller打包后偶尔出现黑窗口一闪而过的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • keras 简单 lstm实例(基于one-hot编码)

    keras 简单 lstm实例(基于one-hot编码)

    这篇文章主要介绍了keras 简单 lstm实例(基于one-hot编码),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07

最新评论