是否可以使用 matplotlib 创建带有垂直渐变和列边框的自定义宽度条的条形图?

时间:2021-05-12 18:02:27

标签: python matplotlib seaborn data-visualization bar-chart

我正在尝试创建具有以下所有特征的特定条形图:

  1. 自定义宽度条
  2. 条形的彩色边框
  3. 垂直渐变

到目前为止,我已经能够通过水平(而不是垂直)渐变或(1 和 3)获得(1 和 2),但我无法同时获得(1 和 2 和 3)时间。 为了说明,这里是 1 & 2: enter image description here

chart.py 脚本:

import matplotlib.pyplot as plt
from colour import Color
height=[..]
width=[..]
x_pos=[..]
colors=[]
for i in list(Color("gray").range_to(Color('#00C1D1'),len(data))):
    colors.append(i.hex_l)

plt.bar(x_pos, height, width=width, align='edge', color=colors, edgecolor="white", linewidth=0.3)
plt.show()

这是 1 和 3: enter image description here

chart.py 脚本:

import matplotlib.pyplot as plt
from colour import Color
from gradient_chart import visualize_data
height=[..]
width=[..]
x_pos=[..]
visualize_data(x_pos,height,width)

gradient_chart.py

import argparse
import numpy as np

from matplotlib import pyplot as plt

def gradient_image(ax, extent, direction=0.05, cmap_range=(0, 1), **kwargs):
    phi = direction * np.pi / 2
    v = np.array([np.cos(phi), np.sin(phi)])
    X = np.array([[v @ [1, 0], v @ [1, 1]], [v @ [0, 0], v @ [0, 1]]])
    a, b = cmap_range
    X = a + (b - a) / X.max() * X
    im = ax.imshow(X, extent=extent, interpolation="bicubic", vmin=0, vmax=1, **kwargs)
    return im


def gradient_bar(ax, x, y, widths, bottom=0):
    for left, top, width in zip(x, y, widths):
        right = left + width
        gradient_image(
            ax,
            extent=(left, right, bottom, top),
            cmap="winter",
            cmap_range=(0.25, 0.85),
        )

def visualize_data(x_pos: list, height: list, width: list):
    xlim = (0, sum(width))
    ylim = (0, max(height))

    fig, ax = plt.subplots()
    ax.set(xlim=xlim, ylim=ylim, autoscale_on=False)
    gradient_bar(ax, x_pos, height, width)
    ax.set_aspect("auto")
    plt.savefig("bar.png", dpi=72)
    plt.show()

这是数据:

heigth=[0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.7071068,0.802301919,1.43246706,1.46461545,1.4928755,1.511782365,1.531091129,1.548707097,1.558339896,1.56545997,1.568193925,1.572500504,1.583429484,1.592396845,1.593307081,1.594706608,1.600956011,1.608112212,1.610486687,1.613005881,1.615324641,1.616097991,1.616728799,1.617822862,1.618928751,1.620501458,1.622433491,1.696794387,1.77077331,1.778544896,1.786565616,1.78846728,1.790230358,1.791287388,1.844274481,1.897942907,1.925275234,1.958241303,1.960365732,1.961356058,1.962014556,1.987720812,2.01560191,2.018330288,2.018658981,2.032033526,2.044623053,2.045398536,2.299888844,2.555846549,2.578067639,2.601988556,2.607561473,2.628311647,2.647418882,2.648329588,2.650077731,2.65164762,2.655918331,2.660828199,2.662871086,2.693536103,2.724338055,2.727244302,2.729618015,2.731246355,2.733277265,2.735314175,2.737347934,2.740301515,2.74445905,2.747687147,2.74968592,2.753016422,2.756339416,2.783700215,2.811034297,2.820786426,2.847210253,2.900088807,2.943898451,2.954369814,2.957200311,2.974859109,3.000084325,3.010163061,3.016808672,3.025924214,3.036304836,3.044443602,3.085190926,3.125650947,3.135215527,3.159005478,3.182423556,3.192006899,3.216510108,3.240563814,3.249799537,3.259639796,3.372965407,3.491304188,3.50358988,3.517936679,3.534232106,3.542717124,3.548344732,3.56965051,3.846702914,4.110474979,4.152541934,4.192988337,4.198258769,4.210454226,4.210823244,4.211915977,4.216868357,4.227898419,4.240197705,4.240546229,4.244998213,4.247661057,4.249872364,4.251048986,4.253720457,4.25742323,4.259447864,4.260370021,4.260370752,4.262028226,4.266064978,4.275414765,4.280905751,4.286387362,4.290579301,4.292025296,4.293490029,4.294443729,4.297698826,4.302864339,4.304464046,4.305720479,4.306940858,4.309734398,4.318669215,4.322737597,4.324582847,4.326799166,4.333613484,4.336715871,4.339796589,4.340661794,4.341530005,4.342988362,4.344529087,4.345678432,4.346729198,4.350688634,4.354681223,4.357246524,4.35977953,4.360679556,4.363012497,4.373756545,4.383063474,4.384540191,4.386025534,4.386753829,4.387648878,4.388571979,4.389394581,4.390188649,4.391419862,4.393004617,4.3940818,4.394818851,4.396973648,4.399891825,4.401919827,4.403712968,4.406412211,4.40920005,4.411143159,4.430251797,4.449357524,4.451850591,4.462206008,4.472026492,4.473475602,4.559723163,4.64641236,4.652303126,4.658381088,4.660277561,4.662855189,4.665405014,4.669597116,4.676298465,4.686328952,4.693878455,4.698078497,4.702579866,4.70651582,4.710114215,4.71379437,4.717505582,4.719269285,4.723070527,4.727626149,4.729295146,4.749597537,4.922862686,4.941152789,4.95477639,4.956167444,4.957569989,4.958972172,4.960273514,4.968301975,4.976309772,4.977876867,4.979571304,4.982891603,4.987712396,4.990619387,4.991947092,4.993271909,4.996455537,4.999638352,5.003208546,5.008682862,5.015029646,5.022636024,5.027068747,5.028460625,5.029853924,5.03128473,5.039655593,5.047869379,5.049516847,5.057573552,5.094520348,5.125060634,5.126323418,5.1275643,5.129176939,5.131409014,5.133274266,5.134519243]

width=[209.9541399,141.4213525,141.4213525,28284.27049,14002.12811,180.1708031,151.8441062,706.3996556,271.3027226,1487.243511,151.9006747,567.0289127,141.9304693,984.7734458,865.4986771,838.5579095,141.4213525,151.5895477,1781.527204,393.1513599,151.5895477,367.8227957,212.1320287,141.4213525,178.8414423,255.972648,148.4924201,141.4213525,3802.579752,282.1355982,707.1067624,183.8477582,212.1320287,1524.564606,314.3796666,2121.320287,282.8427049,459.3789792,141.4213525,3142.198604,202.232534,4631.549294,142.835566,3051.349527,237.5878722,20890.76219,2820.861007,1434.521631,141.4213525,144.6740436,227.6883775,15157.54056,1408.556671,249.4672658,7666.53637,2833.433365,13714.37808,272.5033992,3577.338805,2852.339159,2799.670837,981.7021515,2880.05065,643.1429172,1283.417055,140.5976545,406.1933858,455.1222708,1730.673849,62.79841629,119.2488267,160.6565112,1089.224181,342.0159338,132.8790866,370.9595899,92.79249272,61.877436,64.28412736,154.5286606,66.64901092,247.8924027,138.5141525,14733.66496,62.11975264,1492.197361,111.94663,268.3862352,84.22938385,127.1766895,10470.24193,263.4431195,5203.022313,297.358655,127.5272241,70.53793186,61.16162575,5080.089687,496.1297145,49.54590466,115.2844548,2460.589324,57.31618834,97.78045522,50800.28119,391.2597962,4052.958054,731.2253529,383.3581721,3766.676608,54.7703278,127.3708535,222.2576316,91.72033198,762.4217871,219.5519426,189.0252978,5943.978245,216.4122029,364.8371359,109.905488,215.7623017,190.4197597,216.96228,189.7895381,400.9266842,430.5802996,215.0390377,184.715642,481.3847057,183.214011,5288.945958,177.870473,1772.555325,3512.209887,7063.500936,1698.42815,395.8441474,170.2556293,3361.503733,1683.539345,332.2079169,996.9143975,826.193858,1249.930493,377.8227323,7771.642201,320.3620676,1592.553991,3165.436106,1518.179436,398.4891137,4502.152803,308.5882758,1538.556438,429.4953086,22235.6268,1432.12958,1025.008669,1844.350991,1414.734474,282.2692202,843.2523405,3417.903228,51992.57766,2432.808873,24434.11809,1397.809755,4763.879765,239.2853469,474.9665052,1172.862903,562.9865101,7607.562154,247.6299628,470.2224412,758.9850073,941.6947225,470.6023684,280.8718516,1425.316981,939.5354383,353.5387797,235.4161716,235.8832267,351.9451117,2930.100705,2338.954359,1167.977127,2332.966938,344.3008266,579.2137344,356.2672767,252.8336773,2331.768792,462.4826263,559.203184,243.2438439,536.1763897,2320.328604,2315.528118,282.8277157,895.6794533,2311.177297,252.560595,1728.842798,238.720866,313.8599745,240.6409719,690.7686022,293.2469721,440.8057407,230.2880981,2298.486709,251.4627234,1386.919461,230.8373607,343.9830835,1145.997176,5715.91028,228.1509282,714.9848932,233.6602904,231.4809628,340.1593978,249.3977552,275.9742779,231.1745761,555.1644062,456.9719759,230.9925136,239.7413945,1136.463486,727.2906078,567.9340148,577.2923936,1134.71,657.715678,583.2909764,11620.83384,581.4322598,1010.815594,5602.61,669.7187517,255.7854567,54827.89,538.0495329,3224.209514,657.6061387,553.615094,1092.639551,535.8591575,2141.512373,2138.443488,4267.732847,553.9129368,2128.529782,746.3626562,1767.41571,530.7726917,1819.633893,550.6087814,575.8158384,1851.926612,1057.613238,1057.24,421088.31,4256.36288,10118.28659,588.8419114,504.4220213,597.8735563,504.136727,518.6205948,5791.147588,502.3803008,729.2386086,602.4615006,2007.045466,1781.73465,502.9435838,540.5325718,500.6737157,2001.418791,500.0361674,2305.868303,1996.532876,2991.563572,2986.479595,497.3077007,596.6040552,498.4240175,626.0826348,5952.787735,502.631865,792.1549964,5539.810684,23497.59974,504.7745158,487.6789457,487.5609263,779.8522156,974.3912416,491.5576042,486.9005026]

x_pos=[0,209.9541399,351.3754924,492.7968449,28777.06733,42779.19544,42959.36625,43111.21035,43817.61001,44088.91273,45576.15624,45728.05692,46295.08583,46437.0163,47421.78975,48287.28842,49125.84633,49267.26769,49418.85723,51200.38444,51593.5358,51745.12534,52112.94814,52325.08017,52466.50152,52645.34296,52901.31561,53049.80803,53191.22938,56993.80914,57275.94473,57983.0515,58166.89925,58379.03128,59903.59589,60217.97556,62339.29584,62622.13855,63081.51753,63222.93888,66365.13748,66567.37002,71198.91931,71341.75488,74393.1044,74630.69228,95521.45447,98342.31547,99776.83711,99918.25846,100062.9325,100290.6209,115448.1614,116856.7181,117106.1854,124772.7217,127606.1551,141320.5332,141593.0366,145170.3754,148022.7146,150822.3854,151804.0875,154684.1382,155327.2811,156610.6982,156751.2958,157157.4892,157612.6115,159343.2853,159406.0837,159525.3326,159685.9891,160775.2133,161117.2292,161250.1083,161621.0679,161713.8604,161775.7378,161840.0219,161994.5506,162061.1996,162309.092,162447.6062,177181.2711,177243.3909,178735.5882,178847.5349,179115.9211,179200.1505,179327.3272,189797.5691,190061.0122,195264.0345,195561.3932,195688.9204,195759.4583,195820.62,200900.7097,201396.8394,201446.3853,201561.6697,204022.259,204079.5752,204177.3557,254977.6369,255368.8967,259421.8547,260153.0801,260536.4383,264303.1149,264357.8852,264485.256,264707.5137,264799.234,265561.6558,265781.2077,265970.233,271914.2113,272130.6235,272495.4606,272605.3661,272821.1284,273011.5482,273228.5105,273418.3,273819.2267,274249.807,274464.846,274649.5617,275130.9464,275314.1604,280603.1063,280780.9768,282553.5321,286065.742,293129.2429,294827.6711,295223.5152,295393.7709,298755.2746,300438.814,300771.0219,301767.9363,302594.1301,303844.0606,304221.8834,311993.5256,312313.8876,313906.4416,317071.8777,318590.0572,318988.5463,323490.6991,323799.2873,325337.8438,325767.3391,348002.9659,349435.0955,350460.1041,352304.4551,353719.1896,354001.4588,354844.7112,358262.6144,410255.1921,412688.0009,437122.119,438519.9288,443283.8085,443523.0939,443998.0604,445170.9233,445733.9098,453341.472,453589.1019,454059.3244,454818.3094,455760.0041,456230.6065,456511.4783,457936.7953,458876.3307,459229.8695,459465.2857,459701.1689,460053.114,462983.2147,465322.1691,466490.1462,468823.1131,469167.414,469746.6277,470102.895,470355.7287,472687.4975,473149.9801,473709.1833,473952.4271,474488.6035,476808.9321,479124.4602,479407.2879,480302.9674,482614.1447,482866.7053,484595.5481,484834.2689,485148.1289,485388.7699,486079.5385,486372.7855,486813.5912,487043.8793,489342.366,489593.8287,490980.7482,491211.5856,491555.5686,492701.5658,498417.4761,498645.627,499360.6119,499594.2722,499825.7532,500165.9126,500415.3103,500691.2846,500922.4592,501477.6236,501934.5956,502165.5881,502405.3295,503541.793,504269.0836,504837.0176,505414.31,506549.02,507206.7357,507790.0266,519410.8605,519992.2927,521003.1083,526605.7183,527275.4371,527531.2225,582359.1125,582897.1621,586121.3716,586778.9777,587332.5928,588425.2324,588961.0915,591102.6039,593241.0474,597508.7802,598062.6932,600191.2229,600937.5856,602705.0013,603235.774,605055.4079,605606.0167,606181.8325,608033.7591,609091.3724,610148.6124,1031236.922,1035493.285,1045611.572,1046200.414,1046704.836,1047302.709,1047806.846,1048325.467,1054116.614,1054618.995,1055348.233,1055950.695,1057957.74,1059739.475,1060242.418,1060782.951,1061283.625,1063285.043,1063785.08,1066090.948,1068087.481,1071079.044,1074065.524,1074562.832,1075159.436,1075657.86,1076283.942,1082236.73,1082739.362,1083531.517,1089071.328,1112568.927,1113073.702,1113561.381,1114048.942,1114828.794,1115803.185,1116294.743]

任何关于如何获得 (1 & 2 & 3) 的想法都会非常有帮助!

1 个答案:

答案 0 :(得分:2)

您可以在条形周围添加白色边框,例如通过plt.Rectangle((x,y), width, height)

这是 gradient_bar() 中循环的扩展版本:

def gradient_bar(ax, x, y, widths, bottom=0):
    for left, top, width in zip(x, y, widths):
        right = left + width
        gradient_image(
            ax,
            extent=(left, right, bottom, top),
            cmap="winter",
            cmap_range=(0.25, 0.85),
        )
        ax.add_patch(plt.Rectangle((left, bottom), width, top,
                                   facecolor='none', edgecolor='white', linewidth=0.3))

bars with gradients

相关问题