使用 pandas 和 matplotlib 的分组折线图

死了

我有一个这样的数据集:

数据集图像

数据集可以在这里找到:https : //ucr.fbi.gov/crime-in-the-us/2013/crime-in-the-us-2013/tables/1tabledatadecoverviewpdf/table_1_crime_in_the_united_states_by_volume_and_rate_per_100000_inhabitants_194xls_19

我想按年绘制包含每个犯罪率线的折线图。像这样:犯罪率图
但该图在 x 轴上显示连续的年份,如 2005.5 2007.5。任何人都可以帮忙吗?或建议一个更好的方法来做到这一点。谢谢

这是代码:

%matplotlib inline
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import plotly.plotly as py
import seaborn as sns

cd =pd.read_clipboard() #after copying the dataset from given url above


        yearRate = cd[['Year','ViolentCrimeRate','MurderRate','RapeRate','RobberyRate','AggravatedAssaultRate','PropertyCrimeRate','BurglaryRate','LarcenyTheftRate','MotorVehicleTheftRate']]
    # These are the "Tableau 20" colors as RGB.    
    tableau20 = [(31, 119, 180), (174, 199, 232), (255, 127, 14), (255, 187, 120),    
                 (44, 160, 44), (152, 223, 138), (214, 39, 40), (255, 152, 150),    
                 (148, 103, 189), (197, 176, 213), (140, 86, 75), (196, 156, 148),    
                 (227, 119, 194), (247, 182, 210), (127, 127, 127), (199, 199, 199),    
                 (188, 189, 34), (219, 219, 141), (23, 190, 207), (158, 218, 229)] 
    for i in range(len(tableau20)):    
        r, g, b = tableau20[i]    
        tableau20[i] = (r / 255., g / 255., b / 255.)  


    plt.figure(figsize=(20,15))
    ax = plt.subplot(111)

    ax.spines['top'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['right'].set_visible(False)

    plt.ylim(0,5000)
    plt.xlim(1994, 2013)

    plt.yticks(fontsize=14)  
    plt.xticks(fontsize=14)  

    for y in range(0, 5000, 1000):    
        plt.plot(range(1994, 2013), [y] * len(range(1994, 2013)), "--", lw=0.5, color="black", alpha=0)

    rates=['ViolentCrimeRate','MurderRate','RapeRate','RobberyRate','AggravatedAssaultRate','PropertyCrimeRate','BurglaryRate','LarcenyTheftRate','MotorVehicleTheftRate']

    for rank, column in enumerate(rates):    
        # Plot each line separately with its own color, using the Tableau 20    
        # color set in order.    
        plt.plot(yearRate.Year.values,yearRate[column.replace("\n", " ")].values,lw=2.5, color=tableau20[rank])    
        # Add a text label to the right end of every line. Most of the code below    
        # is adding specific offsets y position because some labels overlapped.    
        y_pos = yearRate[column.replace("\n", " ")].values[-1] - 0.5    
        if column == "MotorVehicleTheftRate":    
            y_pos -= 50 
        elif column == "MurderRate":    
            y_pos -= 50 
        plt.text(2013, y_pos, column, fontsize=14, color=tableau20[rank])
死了

添加:

plt.xticks(cd['Year'])

解决了这个问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用pandas和matplotlib绘制叠加图(折线图和条形图)

Pandas 和 matplotlib - 在折线图上绘制散点图

使用matplotlib的图例和折线图标题

Matplotlib ylim和xlim无法在散点图和折线图或fill_between之间组合使用

如何使用Matplotlib创建折线图

无法使用pandas plot()函数组合条形图和折线图

使用Seaborn和Matplotlib在热图和折线图的共享子图中对齐x轴刻度

如何使用按钮向Matplotlib条形图和折线图添加排序功能

使用pandas和matplotlib绘制多条线图

如何注释分组的破折线图Python Matplotlib

如何使用折线图可视化 Pandas DataFrame?

使用json和mysql php创建折线图

如何使用SVG和XSL制作折线图

使用XML和XSL创建SVG折线图

使用Dash和Plotly的多个折线图

使用 Chartkick 和 Rails 的多系列折线图

分组条形图和折线图

matplotlib中的折线图

Matplotlib 折线图动画

Pandas / Matplotlib-从多个DataFrame列中平滑折线图

Matplotlib:绘制折线图设置线型、比例和自定义颜色

创建带有多条线和图例的 Matplotlib 折线图

在X轴上使用日期时间数据时,Matplotlib扭曲的折线图

在matplotlib中使用折线图时,将X轴上的数字替换为日期

使用Matplotlib将CSV文件数据绘制为折线图

使用 matplotlib 为不同的 y 值创建带有彩色线条的折线图

使用 pandas 和 matplotlib 绘图

在 Python pandas 中对齐折线图和条形图

pandas DataFrame如何混合不同比例的条形图和折线图