将第二列中的值分组,从第一列中选​​取值,并从其余(三)列中的每一列绘制值(针对第一列值)

时间:2019-09-15 19:51:45

标签: python pandas dataframe matplotlib pandas-groupby

如果我不遵循任何格式化规则,请立即道歉。

我的原始数据如下:

{'SOC_percent\tTemp_degC\tmeasuredResistance_discharge_mOhm_2s\tmeasuredResistance_discharge_mOhm_10s\tmeasuredResistance_discharge_mOhm_30s': {0: '5\t-30\t10.41234227\t17.87211262\t39.78919418', 1: '5\t-25\t9.005372364\t14.65298748\t31.43701839', 2: '5\t-20\t7.00477665\t11.96754116\t24.39672451', 3: '5\t-10\t4.76738317\t8.501801895\t16.57636139', 4: '5\t0\t3.287172099\t5.57923204\t9.111447624', 5: '5\t5\t2.284002209\t4.236576914\t6.727561459', 6: '5\t10\t1.869262009\t3.249023803\t5.020990525', 7: '5\t20\t1.27797832\t1.963468963\t2.881752003', 8: '5\t25\t0.936303318\t1.546113511\t2.21395074', 9: '5\t35\t0.707298817\t1.091719664\t1.630883462', 10: '5\t45\t0.543718996\t0.787786805\t1.224869972', 11: '5\t50\t0.479637917\t0.674293305\t1.068590803', 12: '10\t-30\t8.537862111\t13.76174563\t27.78918201', 13: '10\t-25\t6.685843592\t10.49706991\t20.26777709', 14: '10\t-20\t5.09106992\t7.978204366\t14.64512627', 15: '10\t-10\t3.139438794\t4.984757272\t8.55042102', 16: '10\t0\t1.991892456\t2.847802776\t4.272164584', 17: '10\t5\t1.509715963\t2.222798386\t3.332446494', 18: '10\t10\t1.21478403\t1.750446842\t2.62220956', 19: '10\t20\t0.804205279\t1.112408203\t1.663905423', 20: '10\t25\t0.632856291\t0.897095496\t1.340630939', 21: '10\t35\t0.526353672\t0.71202232\t1.078469491', 22: '10\t45\t0.442828733\t0.573468612\t0.879484805', 23: '10\t50\t0.407810006\t0.517250198\t0.797987409', 24: '15\t-30\t6.417216981\t9.928443396\t19.83665094', 25: '15\t-25\t5.204660377\t7.83495283\t15.09165094', 26: '15\t-20\t4.063358491\t6.200141509\t11.37070755', 27: '15\t-10\t2.622933962\t4.123330189\t7.229716981', 28: '15\t0\t1.767141509\t2.592150943\t3.923669811', 29: '15\t5\t1.334830189\t2.047745283\t3.042', 30: '15\t10\t1.103283019\t1.625367925\t2.386603774', 31: '15\t20\t0.764179245\t1.049566038\t1.513518868', 32: '15\t25\t0.616150943\t0.8775\t1.240150943', 33: '15\t35\t0.500009434\t0.702981132\t1.006518868', 34: '15\t45\t0.423358491\t0.57690566\t0.826358491', 35: '15\t50\t0.397910377\t0.51754717\t0.759028302', 36: '20\t-30\t4.841886792\t6.89245283\t12.58301887', 37: '20\t-25\t3.826783019\t5.370471698\t9.450754717', 38: '20\t-20\t3.027037736\t4.226226415\t7.066603774', 39: '20\t-10\t1.916518868\t2.734415094\t4.374622642', 40: '20\t0\t1.298650943\t1.716490566\t2.423150943', 41: '20\t5\t1.032518868\t1.411235849\t1.96704717', 42: '20\t10\t0.867198113\t1.152584906\t1.612735849', 43: '20\t20\t0.614556604\t0.78159434\t1.103896226', 44: '20\t25\t0.526622642\t0.691698113\t0.953169811', 45: '20\t35\t0.435009434\t0.597509434\t0.822556604', 46: '20\t45\t0.3835\t0.522943396\t0.706783019', 47: '20\t50\t0.373075472\t0.475849057\t0.66704717', 48: '25\t-30\t3.597688679\t4.51259434\t6.929858491', 49: '25\t-25\t2.749377358\t3.451745283\t5.085330189', 50: '25\t-20\t2.223735849\t2.706084906\t3.755896226', 51: '25\t-10\t1.369537736\t1.676632075\t2.206320755', 52: '25\t0\t0.940537736\t1.05545283\t1.290556604', 53: '25\t5\t0.797660377\t0.934160377\t1.155773585', 54: '25\t10\t0.686301887\t0.796311321\t1.028962264', 55: '25\t20\t0.501726415\t0.574943396\t0.792386792', 56: '25\t25\t0.461622642\t0.55495283\t0.739773585', 57: '25\t35\t0.382273585\t0.522698113\t0.687650943', 58: '25\t45\t0.349773585\t0.487377358\t0.617867925', 59: '25\t50\t0.353759434\t0.446415094\t0.59959434', 60: '30\t-30\t2.64306337\t2.770057921\t4.983524437', 61: '30\t-25\t2.086447367\t2.250982857\t4.314656224', 62: '30\t-20\t1.727334342\t1.854598294\t3.523595931', 63: '30\t-10\t1.165635292\t1.26288389\t1.583111294', 64: '30\t0\t0.831038221\t0.929507256\t1.153892595', 65: '30\t5\t0.743804417\t0.822642168\t1.020970617', 66: '30\t10\t0.639777679\t0.731319903\t0.907245747', 67: '30\t20\t0.480690569\t0.58496518\t0.725098507', 68: '30\t25\t0.43765325\t0.526189514\t0.651881429', 69: '30\t35\t0.400254942\t0.48262454\t0.611746277', 70: '30\t45\t0.368114597\t0.445089067\t0.576382366', 71: '30\t50\t0.353712659\t0.428235255\t0.560248926', 72: '35\t-30\t2.597669811\t2.713136792\t3.087745283', 73: '35\t-25\t2.044617925\t2.206014151\t2.537575472', 74: '35\t-20\t1.691103774\t1.808778302\t2.069207547', 75: '35\t-10\t1.144858491\t1.233957547\t1.57545283', 76: '35\t0\t0.805448113\t0.910122642\t1.114504717', 77: '35\t5\t0.728674528\t0.806490566\t0.983891509', 78: '35\t10\t0.633504717\t0.709603774\t0.882773585', 79: '35\t20\t0.472783019\t0.576292453\t0.710462264', 80: '35\t25\t0.433691038\t0.517485849\t0.635466981', 81: '35\t35\t0.397174528\t0.473948113\t0.602292453', 82: '35\t45\t0.369028302\t0.441570755\t0.571570755', 83: '35\t50\t0.354771226\t0.425964623\t0.557681604', 84: '40\t-30\t2.556584906\t2.660707547\t2.924632075', 85: '40\t-25\t2.006292453\t2.164622642\t2.403896226', 86: '40\t-20\t1.660443396\t1.771066038\t1.950245283', 87: '40\t-10\t1.127075472\t1.210349057\t1.530688679', 88: '40\t0\t0.784292453\t0.888660377\t1.081084906', 89: '40\t5\t0.717943396\t0.789933962\t0.955377358', 90: '40\t10\t0.62645283\t0.694886792\t0.861311321', 91: '40\t20\t0.466650943\t0.56954717\t0.696971698', 92: '40\t25\t0.431698113\t0.512273585\t0.623509434', 93: '40\t35\t0.395641509\t0.469349057\t0.594933962', 94: '40\t45\t0.368415094\t0.438198113\t0.567584906', 95: '40\t50\t0.354311321\t0.422745283\t0.555075472', 96: '45\t-30\t2.51795283\t2.612570755\t2.78604717', 97: '45\t-25\t1.972259434\t2.127523585\t2.288613208', 98: '45\t-20\t1.633462264\t1.737646226\t1.84354717', 99: '45\t-10\t1.111745283\t1.189806604\t1.498188679', 100: '45\t0\t0.767429245\t0.872103774\t1.053183962', 101: '45\t5\t0.709051887\t0.777056604\t0.93115566', 102: '45\t10\t0.621240566\t0.682622642\t0.843528302', 103: '45\t20\t0.461745283\t0.564028302\t0.685933962', 104: '45\t25\t0.430257075\t0.507674528\t0.613391509', 105: '45\t35\t0.394476415\t0.465363208\t0.588801887', 106: '45\t45\t0.36804717\t0.435438679\t0.564212264', 107: '45\t50\t0.354035377\t0.42007783\t0.552775943', 108: '50\t-30\t2.487324752\t2.571003179\t2.624742662', 109: '50\t-25\t1.941874744\t2.091626265\t2.198386641', 110: '50\t-20\t1.614132861\t1.717907652\t1.86246071', 111: '50\t-10\t1.101742804\t1.179615538\t1.34124724', 112: '50\t0\t0.755056968\t0.842862796\t1.030306432', 113: '50\t5\t0.707658681\t0.757797046\t0.921586561', 114: '50\t10\t0.61064567\t0.683998956\t0.82747666', 115: '50\t20\t0.461607921\t0.56313721\t0.674500603', 116: '50\t25\t0.431767087\t0.51355881\t0.612027845', 117: '50\t35\t0.395653787\t0.469831833\t0.585359573', 118: '50\t45\t0.364559062\t0.432224387\t0.5614416', 119: '50\t50\t0.350606411\t0.415369189\t0.550384704', 120: '55\t-30\t2.44804717\t2.529174528\t2.582462264', 121: '55\t-25\t1.917070755\t2.06620283\t2.113235849', 122: '55\t-20\t1.590537736\t1.683683962\t1.666943396', 123: '55\t-10\t1.088443396\t1.157919811\t1.469981132', 124: '55\t0\t0.746580189\t0.853707547\t1.013938679', 125: '55\t5\t0.696787736\t0.762339623\t0.895589623', 126: '55\t10\t0.616334906\t0.66545283\t0.819', 127: '55\t20\t0.455613208\t0.556669811\t0.671216981', 128: '55\t25\t0.42903066\t0.500316038\t0.598674528', 129: '55\t35\t0.39325\t0.459231132\t0.580216981', 130: '55\t45\t0.36804717\t0.431759434\t0.559306604', 131: '55\t50\t0.354035377\t0.416398585\t0.549096698', 132: '60\t-30\t2.416773585\t2.493915094\t2.517462264', 133: '60\t-25\t1.895915094\t2.041981132\t2.053141509', 134: '60\t-20\t1.57459434\t1.663141509\t1.597037736', 135: '60\t-10\t1.080471698\t1.146575472\t1.474273585', 136: '60\t0\t0.74259434\t0.851867925\t1.00259434', 137: '60\t5\t0.693415094\t0.7605\t0.884245283', 138: '60\t10\t0.616641509\t0.66054717\t0.812254717', 139: '60\t20\t0.454386792\t0.554830189\t0.667537736', 140: '60\t25\t0.429245283\t0.497556604\t0.594075472', 141: '60\t35\t0.393188679\t0.457084906\t0.577764151', 142: '60\t45\t0.368415094\t0.430839623\t0.557773585', 143: '60\t50\t0.354311321\t0.415386792\t0.547716981', 144: '65\t-30\t2.38795283\t2.462948113\t2.476990566', 145: '65\t-25\t1.879051887\t2.022051887\t2.011443396', 146: '65\t-20\t1.562330189\t1.646891509\t1.539396226', 147: '65\t-10\t1.07495283\t1.13829717\t1.490830189', 148: '65\t0\t0.742900943\t0.854933962\t0.996768868', 149: '65\t5\t0.691882075\t0.762339623\t0.877193396', 150: '65\t10\t0.618787736\t0.65809434\t0.809188679', 151: '65\t20\t0.454386792\t0.554216981\t0.666311321', 152: '65\t25\t0.430011792\t0.495410377\t0.591316038', 153: '65\t35\t0.393495283\t0.455551887\t0.576537736', 154: '65\t45\t0.369028302\t0.430533019\t0.556853774', 155: '65\t50\t0.354771226\t0.414926887\t0.546643868', 156: '70\t-30\t2.361584906\t2.436273585\t2.46104717', 157: '70\t-25\t1.866481132\t2.006415094\t1.988141509', 158: '70\t-20\t1.553745283\t1.634933962\t1.494018868', 159: '70\t-10\t1.071886792\t1.133084906\t1.519650943', 160: '70\t0\t0.7475\t0.86290566\t0.996462264', 161: '70\t5\t0.692188679\t0.767858491\t0.874433962', 162: '70\t10\t0.622773585\t0.65809434\t0.809801887', 163: '70\t20\t0.455613208\t0.554830189\t0.667537736', 164: '70\t25\t0.431330189\t0.493877358\t0.590396226', 165: '70\t35\t0.394169811\t0.454632075\t0.576537736', 166: '70\t45\t0.369886792\t0.430839623\t0.55654717', 167: '70\t50\t0.355415094\t0.415018868\t0.545877358', 168: '75\t-30\t2.337669811\t2.413891509\t2.469632075', 169: '75\t-25\t1.85820283\t1.995070755\t1.983235849', 170: '75\t-20\t1.548839623\t1.627268868\t1.46090566', 171: '75\t-10\t1.071273585\t1.130938679\t1.360735849', 172: '75\t0\t0.756391509\t0.875783019\t1.001674528', 173: '75\t5\t0.694334906\t0.777056604\t0.875966981', 174: '75\t10\t0.628599057\t0.66054717\t0.81409434', 175: '75\t20\t0.458066038\t0.556669811\t0.671216981', 176: '75\t25\t0.433200472\t0.492957547\t0.591316038', 177: '75\t35\t0.395212264\t0.454325472\t0.577764151', 178: '75\t45\t0.370990566\t0.431759434\t0.556853774', 179: '75\t50\t0.356242925\t0.415662736\t0.545417453', 180: '80\t-30\t2.316207547\t2.395801887\t2.502745283', 181: '80\t-25\t1.854216981\t1.988018868\t1.996726415', 182: '80\t-20\t1.547613208\t1.623896226\t1.440056604', 183: '80\t-10\t1.073113208\t1.131858491\t1.314084906', 184: '80\t0\t0.769575472\t0.893566038\t1.01240566', 185: '80\t5\t0.698320755\t0.789933962\t0.881792453', 186: '80\t10\t0.636264151\t0.66545283\t0.822066038', 187: '80\t20\t0.461745283\t0.559735849\t0.677349057', 188: '80\t25\t0.435622642\t0.492650943\t0.594075472', 189: '80\t35\t0.396622642\t0.454632075\t0.580216981', 190: '80\t45\t0.372339623\t0.433292453\t0.557773585', 191: '80\t50\t0.357254717\t0.416858491\t0.545264151', 192: '85\t-30\t2.297198113\t2.382004717\t2.560386792', 193: '85\t-25\t1.854523585\t1.985259434\t2.028613208', 194: '85\t-20\t1.550066038\t1.624816038\t1.431471698', 195: '85\t-10\t1.07740566\t1.13584434\t1.379698113', 196: '85\t0\t0.787051887\t0.916254717\t1.02865566', 197: '85\t5\t0.704146226\t0.806490566\t0.891910377', 198: '85\t10\t0.645768868\t0.672811321\t0.833716981', 199: '85\t20\t0.466650943\t0.564028302\t0.685933962', 200: '85\t25\t0.438596698\t0.492957547\t0.598674528', 201: '85\t35\t0.398400943\t0.455551887\t0.583896226', 202: '85\t45\t0.373933962\t0.435438679\t0.559306604', 203: '85\t50\t0.358450472\t0.418606132\t0.545417453', 204: '90\t-30\t2.29946844\t2.383584796\t2.489622642', 205: '90\t-25\t1.86032786\t1.974433564\t2.103911344', 206: '90\t-20\t1.566292427\t1.657804525\t1.4556559', 207: '90\t-10\t1.096469402\t1.168700917\t1.315493648', 208: '90\t0\t0.809138285\t0.89081257\t1.0451439', 209: '90\t5\t0.727080727\t0.796859124\t0.93661049', 210: '90\t10\t0.63385084\t0.715685937\t0.842639129', 211: '90\t20\t0.488533958\t0.583686013\t0.689454905', 212: '90\t25\t0.446604847\t0.529868037\t0.626826454', 213: '90\t35\t0.404214431\t0.478697896\t0.591510491', 214: '90\t45\t0.368150895\t0.435245215\t0.56020084', 215: '90\t50\t0.35210775\t0.415939509\t0.54586218', 216: '95\t-30\t2.266537736\t2.367287736\t2.549254717', 217: '95\t-25\t1.868014151\t1.992617925\t2.147575472', 218: '95\t-20\t1.566009434\t1.639533019\t1.45109434', 219: '95\t-10\t1.093349057\t1.153014151\t1.447716981', 220: '95\t0\t0.834882075\t0.976349057\t1.077712264', 221: '95\t5\t0.721316038\t0.850641509\t0.925023585', 222: '95\t10\t0.67029717\t0.694886792\t0.868056604', 223: '95\t20\t0.480141509\t0.576292453\t0.710462264', 224: '95\t25\t0.446200472\t0.495410377\t0.613391509', 225: '95\t35\t0.403061321\t0.459231132\t0.594933962', 226: '95\t45\t0.377858491\t0.441570755\t0.564212264', 227: '95\t50\t0.361393868\t0.423757075\t0.546643868', 228: '100\t-30\t2.254886792\t2.366367925\t2.580481132', 229: '100\t-25\t1.881198113\t2.002735849\t2.234650943', 230: '100\t-20\t1.5795\t1.653330189\t1.479301887', 231: '100\t-10\t1.105\t1.166198113\t1.450122642', 232: '100\t0\t0.865235849\t1.013754717\t1.110518868', 233: '100\t5\t0.732660377\t0.878235849\t0.948018868', 234: '100\t10\t0.685320755\t0.709603774\t0.890745283', 235: '100\t20\t0.488726415\t0.584264151\t0.72640566', 236: '100\t25\t0.450830189\t0.497556604\t0.623509434', 237: '100\t35\t0.405943396\t0.461990566\t0.602292453', 238: '100\t45\t0.380188679\t0.445556604\t0.567584906', 239: '100\t50\t0.363141509\t0.427160377\t0.547716981'}}

我希望从原始数据中得出以下图表(x轴= 2s,10s和30s; y轴=电阻数据)。原始数据在time_seconds中不包含任何数字信息。 Required final plot

我能够使用数据透视表和表格通过MS Excel 2016实现此目的。花了一段时间,但是我能够建立一个模板。然后,我遇到了一些人们使用Pandas和Seaborn上传到互联网的地块,这使我开始考虑减少构建excel模板所需的时间和精力。我没有比seaborn 0.8.1高的任何东西,而且我认为我将不能升级到高于该版本的任何东西。

我的问题是:1)是否可以在Python 3.6和Pandas中开发类似的东西,以便获得与上述相似的图? 2)如果是,我将如何完成这项工作(知道原始数据不包含时间值(以秒为单位)?

谢谢

1 个答案:

答案 0 :(得分:0)

是的,确实可以使用pandasmatplotlib来做到这一点。例如,考虑以下功能:

def myplotting(dt, SOC_p, temp):
    """plot data at selected SOC_percent and temperature.

    dt: the dataframe
    SOC_p: a list of 'SOC_percent' values. A line for each percentage will be plotted.
    temp: numeric, a single temperature value.
    """
    x = [2, 10, 30]
    selplt = dt.loc[(dt['SOC_percent'].isin(SOC_p)) & (dt['Temp_degC'] == temp)].drop('Temp_degC', axis=1).set_index('SOC_percent')
    for prc in SOC_p:
        plt.plot(x, selplt.loc[prc], label=f'SOC percent: {prc:d}')
    plt.title(f'Temp_degC: {temp:d}')
    plt.xlabel('Time (s)')
    plt.ylabel('Discharge Resistance (mOhm)')
    plt.legend()
    plt.grid(True)
    plt.show()

该功能适用​​于您的数据框。您需要将其作为数据框的参数,'SOC_percent'值和温度值的列表。
首先,使用loc选择所需温度和百分比值的相关行。然后,使用for循环(每个百分比值的一行)生成图。最后,添加标题,标签和图例。

例如,要复制您的情节,请执行以下操作:

myplotting(df, [10, 20, 30], -10)

其中df是您的数据框。将生成以下图:

enter image description here