Creating bar chart to output column value if they have a specific feature

Hi,

I would like to create a bar chart in which car brand appear as values for the number of total rapid charge feature are included and any brand/model which does not have rapid charge are excluded.

My code is;

fig = px.bar(ElectricCarData, x=“Brand”, color=“RapidCharge”, height=710).update_xaxes(
categoryorder=“max ascending”)

fig.update_yaxes(range=[6,13])

My chart is:

Many thanks

Can you show us your expected chart, I don’t really understand what do you want.

Hi @hoatran,

Thank you for your answer, I have included a screenshot of my expected chart below. I would like to produce this view but automatically without having to uncheck the option for rapid charging possible/rapid charging not possible.

Many thanks

I think you can use facet options or use subplot. Based on your data in your old post, I did something as below:

import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go

df = pd.read_csv('Car brand.csv')
df2 = pd.pivot_table(df,values='Model', index=['Brand','RapidCharge'], aggfunc='count').reset_index()
fig = px.bar(df2, x="Brand", y="Model", facet_row="RapidCharge", color='RapidCharge')
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
fig.show()

or with subplot:

import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

df = pd.read_csv('Car brand.csv')
df2 = pd.pivot_table(df,values='Model', index=['Brand','RapidCharge'], aggfunc='count').reset_index()
df2_1 = df2[df2['RapidCharge'] == 'Rapid charging possible']
df2_2 = df2[df2['RapidCharge'] == 'Rapid charging not possible']

fig = make_subplots(
    rows=1, cols=2,
    subplot_titles=("Plot 1", "Plot 2"))

fig.add_trace(go.Bar(x=df2_1['Brand'], y=df2_1['Model']),
              row=1, col=1)

fig.add_trace(go.Bar(x=df2_2['Brand'], y=df2_1['Model']),
              row=1, col=2)

fig.update_layout(height=500, width=700,
                  title_text="Multiple Subplots with Titles")

fig.show()

1 Like

Hi @hoatran,

Thank you so much for your help, I had tried a few things but could not get this to work so it’s very much appreciated have a nice and safe rest of your week.

I forgot to add df2 = pd.pivot_table(df,values='Model', index=['Brand','RapidCharge'], aggfunc='count').reset_index() in my post. Please check it again.

1 Like

Will do

many thanks

Hi @hoatran,

I apologise, I have just tried the solution and it is almost identical but for some reason my values are shows as decimals instead of the numbers as shown in yours I was wondering if you could advise me regarding this many thanks.

The code:

ax = pd.pivot_table(ElectricCarData,values=‘Model’, index=[‘Brand’,‘RapidCharge’], aggfunc=‘count’).reset_index()
ax1 = ElectricCarData[ElectricCarData[‘RapidCharge’] == ‘Rapid charging possible’]
ax2 = ElectricCarData[ElectricCarData[‘RapidCharge’] == ‘Rapid charging not possible’]

fig.update_xaxes(
categoryorder=“total descending”
)

fig = make_subplots(
rows=1, cols=2,
subplot_titles=(“RapidCharge”, “Without RapidCharge”))

fig.add_trace(go.Bar(x=ax1[‘Brand’], y=ax1[‘Model’]),
row=1, col=1)

fig.add_trace(go.Bar(x=ax2[‘Brand’], y=ax2[‘Model’]),
row=1, col=2)

fig.update_layout(height=500, width=710,
title_text=“Multiple Subplots with Titles”)

fig.show()

The plot:

Can you provide your dataframe?

Sure, just to clarify do you mean the name of it or its data. The dataframe is called ElectricCarData.

I mean real data that you are using to make you chart, not the dataframe name.

Sure, it’s a CSV I will paste all of it’s data here.

Brand Model Accel TopSpeed Range Efficiency Efficiency Score FastCharge RapidCharge PowerTrain PlugType BodyStyle Segment Seats PriceEuro
Lightyear One 10 150 575 104 Excellent 940 Rapid charging possible All Wheel Drive Type 2 CCS Sedan D 5 55480
Tesla Model 3 Standard Range Plus 5.6 225 310 153 Good 250 Rapid charging possible Rear Wheel Drive Type 2 CCS Hatchback C 5 30000
Hyundai IONIQ Electric 9.7 165 250 153 Good 620 Rapid charging possible All Wheel Drive Type 2 CCS Liftback D 5 56440
Hyundai Kona Electric 39 kWh 9.9 155 255 154 Good 560 Rapid charging possible Rear Wheel Drive Type 2 CCS SUV D 5 68040
Mini Cooper SE 7.3 150 185 156 Good 190 Rapid charging possible Rear Wheel Drive Type 2 CCS Hatchback B 4 32997
Sono Sion 9 140 225 156 Good 620 Rapid charging possible All Wheel Drive Type 2 CCS Sedan F 5 105000
Hyundai Kona Electric 64 kWh 7.9 167 400 160 Good 220 Rapid charging possible Front Wheel Drive Type 2 CCS Hatchback C 5 31900
Tesla Model 3 Long Range Dual Motor 4.6 233 450 161 Good 420 Rapid charging possible Front Wheel Drive Type 2 CCS Hatchback B 5 29682
BMW i3 120 Ah 7.3 150 235 161 Good 650 Rapid charging possible Rear Wheel Drive Type 2 CCS Sedan D 5 46380
Renault Zoe ZE40 R110 11.4 135 255 161 Good 540 Rapid charging possible All Wheel Drive Type 2 CCS SUV D 5 55000
Peugeot e-208 8.1 150 275 164 Average 440 Rapid charging possible All Wheel Drive Type 2 CCS SUV D 5 69484
Nissan Leaf 7.9 144 220 164 Average 230 Rapid charging possible Front Wheel Drive Type 2 CHAdeMO Hatchback C 5 29234
Opel Corsa-e 8.1 150 275 164 Average 380 Rapid charging possible Front Wheel Drive Type 2 CCS SUV B 5 40795
Renault Twingo ZE 12.6 135 130 164 Average 650 Rapid charging possible Rear Wheel Drive Type 2 CCS Sedan D 5 65000
Renault Zoe ZE50 R110 11.4 135 315 165 Average 210 Rapid charging possible Front Wheel Drive Type 2 CCS Liftback C 5 34459
BMW i3s 120 Ah 6.9 160 230 165 Average 590 Rapid charging possible Rear Wheel Drive Type 2 CCS Hatchback C 4 40936
Volkswagen e-Up! 11.9 130 195 166 Average 780 Rapid charging possible All Wheel Drive Type 2 CCS Sedan F 4 180781
Volkswagen ID.3 Pro 9 160 350 166 Average 170 Rapid charging possible Front Wheel Drive Type 2 CCS Hatchback A 4 21421
Skoda CITIGOe iV 12.3 130 195 166 Average 260 Rapid charging possible Front Wheel Drive Type 2 CCS SUV B 5 30000
SEAT Mii Electric 12.3 130 195 166 Average 260 Rapid charging possible Front Wheel Drive Type 2 CCS Hatchback B 4 31681
Volkswagen ID.3 Pure 10 160 270 167 Average 420 Rapid charging possible Front Wheel Drive Type 2 CCS Hatchback B 5 29146
Tesla Model 3 Long Range Performance 3.4 261 435 167 Average 930 Rapid charging possible All Wheel Drive Type 2 CCS SUV D 7 58620
Kia e-Niro 39 kWh 9.8 155 235 167 Average 230 Rapid charging possible Rear Wheel Drive Type 2 CCS SUV C 5 35000
Smart EQ fortwo coupe 11.6 130 100 167 Average 850 Rapid charging possible All Wheel Drive Type 2 CCS Sedan F 4 125000
Honda e 9.5 145 170 168 Average 910 Rapid charging possible All Wheel Drive Type 2 CCS Sedan D 5 61480
Volkswagen e-Golf 9.6 150 190 168 Average 560 Rapid charging possible Rear Wheel Drive Type 2 CCS SUV C 5 45000
Renault Zoe ZE50 R135 9.5 140 310 168 Average 490 Rapid charging possible Rear Wheel Drive Type 2 CCS Hatchback C 5 33000
Honda e Advance 8.3 145 170 168 Average 470 Rapid charging possible All Wheel Drive Type 2 CCS SUV C 5 60437
Fiat 500e Hatchback 9 150 250 168 Average 270 Rapid charging possible Rear Wheel Drive Type 2 CCS Hatchback B 4 38017
Fiat 500e Convertible 9 150 250 168 Average 380 Rapid charging possible Front Wheel Drive Type 2 CCS SUV B 5 34361
Kia e-Soul 39 kWh 9.9 157 230 170 Average 450 Rapid charging possible All Wheel Drive Type 2 CCS SUV E 5 67358
Tesla Model Y Long Range Dual Motor 5.1 217 425 171 Average 350 Rapid charging possible Front Wheel Drive Type 2 CCS SUV C 5 38105
Mercedes EQA 5 200 350 171 Average 230 Rapid charging possible Front Wheel Drive Type 2 CCS Hatchback B 5 31184
Volkswagen ID.3 1st 7.3 160 340 171 Average 710 Rapid charging possible All Wheel Drive Type 2 CCS Pickup N 6 75000
Volkswagen ID.3 Pro Performance 7.3 160 340 171 Average 240 Rapid charging possible Front Wheel Drive Type 2 CCS SUV C 5 32646
Nissan Leaf e+ 7.3 157 325 172 Average 390 Rapid charging possible Front Wheel Drive Type 2 CHAdeMO Hatchback C 5 37237
Kia e-Niro 64 kWh 7.8 167 370 173 Average 190 Rapid charging possible Front Wheel Drive Type 2 CHAdeMO SUV C 5 50000
Opel Ampera-e 7.3 150 335 173 Average 570 Rapid charging possible Rear Wheel Drive Type 2 CCS Hatchback C 4 45000
Volkswagen ID.3 Pro S 7.9 160 440 175 Average 230 Rapid charging possible Front Wheel Drive Type 2 CCS Hatchback B 5 33133
Kia e-Soul 64 kWh 7.9 167 365 175 Average 440 Rapid charging possible All Wheel Drive Type 2 CCS SUV C 5 45000
Kia e-Soul 64 kWh 7.9 167 365 175 Average 560 Rapid charging possible All Wheel Drive Type 2 Liftback F 5 79990
Opel Mokka-e 8.5 150 255 176 Average 210 Rapid charging possible Front Wheel Drive Type 2 CCS SUV B 5 33971
Smart EQ forfour 12.7 130 95 176 Average 610 Rapid charging possible All Wheel Drive Type 2 CCS SUV E 5 81639
Smart EQ fortwo cabrio 11.9 130 95 176 Average 170 Rapid charging possible Front Wheel Drive Type 2 CCS Hatchback A 4 24534
Tesla Model Y Long Range Performance 3.7 241 410 177 Average 170 Rapid charging possible Front Wheel Drive Type 2 CCS Hatchback A 4 20129
BMW i4 4 200 450 178 Average 340 Rapid charging possible Front Wheel Drive Type 2 CCS SUV B 5 36837
Mazda MX-30 9 150 180 178 Average 210 Rapid charging possible Front Wheel Drive Type 2 CCS MPV B 5 41906
Skoda Enyaq iV 50 10 160 290 179 Average 730 Rapid charging possible All Wheel Drive Type 2 CCS Sedan F 4 102945
Lucid Air 2.8 250 610 180 Average 540 Rapid charging possible All Wheel Drive Type 2 CCS Liftback F 5 149000
Peugeot e-2008 SUV 8.5 150 250 180 Average 350 Rapid charging possible Front Wheel Drive Type 2 CCS SUV C 5 36057
DS 3 Crossback E-Tense 8.7 150 250 180 Average 590 Rapid charging possible All Wheel Drive Type 2 CCS SUV E 5 79445
Citroen e-C4 9.7 150 250 180 Average 920 Rapid charging possible All Wheel Drive Type 2 CCS Cabrio S 4 215000
Polestar 2 4.7 210 400 181 Average 390 Rapid charging possible Front Wheel Drive Type 2 CCS SUV B 5 35000
CUPRA el-Born 6.5 160 425 181 Average 560 Rapid charging possible Rear Wheel Drive Type 2 CCS SUV C 5 40000
Skoda Enyaq iV 60 9 160 320 181 Average 490 Rapid charging possible All Wheel Drive Type 2 SUV F 7 85990
Volkswagen ID.4 7.5 160 420 183 Average 190 Rapid charging possible Rear Wheel Drive Type 2 CCS Hatchback B 4 35921
Skoda Enyaq iV 80 8.8 160 420 183 Average 380 Rapid charging possible Front Wheel Drive Type 2 CCS SUV B 5 37422
Tesla Model S Long Range 3.8 250 515 184 Poor - Rapid charging not possible Rear Wheel Drive Type 2 Hatchback A 4 24790
Aiways U5 9 150 335 188 Poor 380 Rapid charging possible Front Wheel Drive Type 2 CCS SUV C 5 40000
Tesla Model S Performance 2.5 261 505 188 Poor 550 Rapid charging possible All Wheel Drive Type 2 Liftback F 5 96990
Audi Q4 Sportback e-tron 6.3 180 410 188 Poor 230 Rapid charging possible Front Wheel Drive Type 2 CCS Hatchback B 5 29234
Nissan Ariya 63kWh 7.5 160 330 191 Poor 900 Rapid charging possible All Wheel Drive Type 2 CCS SUV D 7 65620
Audi Q4 e-tron 6.3 180 400 193 Poor 520 Rapid charging possible Front Wheel Drive Type 2 CCS Hatchback C 5 50000
MG ZS EV 8.2 140 220 193 Poor 340 Rapid charging possible All Wheel Drive Type 2 CCS SUV E 5 75351
Lexus UX 300e 7.5 160 270 193 Poor 430 Rapid charging possible Rear Wheel Drive Type 2 CCS SUV D 5 54475
Skoda Enyaq iV 80X 7 160 400 193 Poor 890 Rapid charging possible All Wheel Drive Type 2 CCS Sedan F 4 109302
Skoda Enyaq iV vRS 6.2 180 400 193 Poor 190 Rapid charging possible Front Wheel Drive Type 1 CHAdeMO SPV N 7 33246
Renault Kangoo Maxi ZE 33 22.4 130 160 194 Poor 710 Rapid charging possible All Wheel Drive Type 2 CCS Pickup N 6 55000
Ford Mustang Mach-E SR RWD 6.6 180 360 194 Poor - Rapid charging not possible Front Wheel Drive Type 2 SPV N 5 38000
Nissan Ariya e-4ORCE 63kWh 5.9 200 325 194 Poor 410 Rapid charging possible All Wheel Drive Type 2 CCS SUV D 5 62900
Porsche Taycan 4S 4 250 365 195 Poor 260 Rapid charging possible Rear Wheel Drive Type 2 CCS Hatchback B 4 41526
Audi e-tron GT 3.5 240 425 197 Poor 540 Rapid charging possible All Wheel Drive Type 2 CCS SUV C 5 45000
Porsche Taycan 4S Plus 4 250 425 197 Poor 770 Rapid charging possible All Wheel Drive Type 2 CCS Station F 4 150000
Nissan Ariya 87kWh 7.6 160 440 198 Poor 460 Rapid charging possible All Wheel Drive Type 2 CCS SUV E 5 64000
Volvo XC40 P8 AWD Recharge 4.9 180 375 200 Poor 270 Rapid charging possible Front Wheel Drive Type 2 CCS Hatchback C 5 25500
Ford Mustang Mach-E ER RWD 7 180 450 200 Poor 230 Rapid charging possible Front Wheel Drive Type 2 CCS SUV C 5 34400
Nissan e-NV200 Evalia 14 123 190 200 Poor 550 Rapid charging possible All Wheel Drive Type 2 CCS SUV D 5 57500
BMW iX3 6.8 180 360 206 Poor - Rapid charging not possible Rear Wheel Drive Type 2 Hatchback A 4 22030
Tesla Roadster 2.1 410 970 206 Poor 360 Rapid charging possible All Wheel Drive Type 2 CCS SUV D 5 54000
Ford Mustang Mach-E SR AWD 6 180 340 206 Poor 810 Rapid charging possible All Wheel Drive Type 2 CCS Sedan F 4 148301
Nissan Ariya e-4ORCE 87kWh 5.7 200 420 207 Poor 470 Rapid charging possible Rear Wheel Drive Type 2 CCS Hatchback C 5 38987
Ford Mustang Mach-E ER AWD 6 180 430 209 Poor 480 Rapid charging possible All Wheel Drive Type 2 SUV F 7 102990
Tesla Model X Long Range 4.6 250 450 211 Poor - Rapid charging not possible Rear Wheel Drive Type 2 Hatchback A 2 21387
Porsche Taycan Turbo 3.2 260 390 215 Poor 380 Rapid charging possible Rear Wheel Drive Type 2 CCS SUV D 5 46900
Mercedes EQC 400 4MATIC 5.1 180 370 216 Poor 290 Rapid charging possible Front Wheel Drive Type 2 CCS SPV N 7 70631
Tesla Model X Performance 2.8 250 440 216 Poor 330 Rapid charging possible Front Wheel Drive Type 2 CCS Hatchback B 4 34900
Porsche Taycan Cross Turismo 3.5 250 385 217 Poor 740 Rapid charging possible Rear Wheel Drive Type 2 CCS Pickup N 6 45000
Audi e-tron Sportback 50 quattro 6.8 190 295 219 Poor 470 Rapid charging possible All Wheel Drive Type 2 CCS SUV E 5 69551
Byton M-Byte 72 kWh 2WD 7.5 190 325 222 Poor 540 Rapid charging possible All Wheel Drive Type 2 CCS SUV C 5 47500
Porsche Taycan Turbo S 2.8 260 375 223 Poor 440 Rapid charging possible Rear Wheel Drive Type 2 CCS SUV C 5 37500
Audi e-tron Sportback 55 quattro 5.7 200 380 228 Poor 510 Rapid charging possible All Wheel Drive Type 2 CCS SUV E 5 93800
Audi e-tron 50 quattro 6.8 190 280 231 Poor - Rapid charging not possible Rear Wheel Drive Type 2 Cabrio A 2 24565
Jaguar I-Pace 4.8 200 365 232 Poor 320 Rapid charging possible Front Wheel Drive Type 2 CCS SUV B 5 36837
Nissan Ariya e-4ORCE 87kWh Performance 5.1 200 375 232 Poor 500 Rapid charging possible All Wheel Drive Type 2 CCS Hatchback C 5 57500
Audi e-tron 55 quattro 5.7 200 365 237 Poor 330 Rapid charging possible Front Wheel Drive Type 2 CCS Cabrio B 4 37900
Byton M-Byte 95 kWh 2WD 7.5 190 400 238 Poor 470 Rapid charging possible Rear Wheel Drive Type 2 CCS Hatchback C 5 35575
Byton M-Byte 95 kWh 4WD 5.5 190 390 244 Poor 220 Rapid charging possible Front Wheel Drive Type 2 CCS SUV B 5 33133
Tesla Cybertruck Single Motor 7 180 390 256 Poor 420 Rapid charging possible Rear Wheel Drive Type 2 CCS SUV E 5 53500
Audi e-tron S Sportback 55 quattro 4.5 210 335 258 Poor 440 Rapid charging possible Front Wheel Drive Type 2 CCS Hatchback C 5 45000
Tesla Cybertruck Dual Motor 5 190 460 261 Poor 540 Rapid charging possible All Wheel Drive Type 2 CCS SUV E 5 96050
Tesla Cybertruck Tri Motor 3 210 750 267 Poor 440 Rapid charging possible All Wheel Drive Type 2 CCS Hatchback C 5 50000
Audi e-tron S 55 quattro 4.5 210 320 270 Poor 450 Rapid charging possible All Wheel Drive Type 2 CCS Hatchback C 5 65000
Mercedes EQV 300 Long 10 140 330 273 Poor 480 Rapid charging possible All Wheel Drive Type 2 CCS SUV E 5 62000

I think your ax1 and ax2 should be changed as below:

ax = pd.pivot_table(ElectricCarData,values=‘Model’, index=[‘Brand’,‘RapidCharge’], aggfunc=‘count’).reset_index()
ax1 = ax[ax[‘RapidCharge’] == ‘Rapid charging possible’]
ax2 = ax[ax[‘RapidCharge’] == ‘Rapid charging not possible’]

Hi @hoatran,

I really you appreciate you taking the time to help me with my chart issue, unfortunately I tried the reply code you had sent above but I seem to be receiving the error below. I have tried to replace the single speech marks with ’ but it does not seem to fix the issue.

Please help me to change by this one:

ax = pd.pivot_table(ElectricCarData,values="Model", index=["Brand","RapidCharge"], aggfunc="count").reset_index()
ax1 = ax[ax["RapidCharge"] == "Rapid charging possible"]
ax2 = ax[ax["RapidCharge"] == "Rapid charging not possible"]

Hi @hoatran,

Thank you for much for all you time and help. I have just tried both codes again but for some reason the output is not in the count range as yours in both instances I am seeing values of max 1. As you are able to produce the charts, perhaps the issue could be related to the environment.

Code 1

ax = pd.pivot_table(ElectricCarData,values=‘Model’, index=[‘Brand’,‘RapidCharge’], aggfunc=‘count’).reset_index()

fig = px.bar(ax, x=“Brand”, y=“Model”, facet_row=“RapidCharge”, color=‘RapidCharge’)

fig.for_each_annotation(lambda a: a.update(text=a.text.split(“=”)[1]))

fig.show()

Output 1

code 2

ax = pd.pivot_table(ElectricCarData,values=“Model”, index=[“Brand”,“RapidCharge”], aggfunc=“count”).reset_index()
ax1 = ax[ax[“RapidCharge”] == “Rapid charging possible”]
ax2 = ax[ax[“RapidCharge”] == “Rapid charging not possible”]

fig = make_subplots(
rows=1, cols=2,
subplot_titles=(“Plot 1”, “Plot 2”))

fig.add_trace(go.Bar(x=ax1[‘Brand’], y=ax1[‘Model’]),
row=1, col=1)

fig.add_trace(go.Bar(x=ax2[‘Brand’], y=ax2[‘Model’]),
row=1, col=2)

fig.update_layout(height=500, width=700,
title_text=“Multiple Subplots with Titles”)

output 2

Hm I can not reproduce your problem, it’s working good for me. I’m using Jupyter Notebook to run.

Hi @hoatran,

I reset my session and it seems both codes are now working. I was wondering if you could advise me regarding how I can replace the trace0/trace1 to rapidcharge and no rapidcharge as I replace them with the word plot but my output did not change.

Many thanks

1 Like

Just add name when add_trace then it’ll work.

from plotly.subplots import make_subplots
import plotly.graph_objects as go

ax = pd.pivot_table(ElectricCarData,values="Model", index=["Brand","RapidCharge"], aggfunc="count").reset_index()
ax1 = ax[ax["RapidCharge"] == "Rapid charging possible"]
ax2 = ax[ax["RapidCharge"] == "Rapid charging not possible"]

fig.update_xaxes(
categoryorder="total descending"
)

fig = make_subplots(
rows=1, cols=2,
subplot_titles=("RapidCharge", "Without RapidCharge"))

fig.add_trace(go.Bar(x=ax1["Brand"], y=ax1["Model"], name='Rapid Charge'),row=1, col=1)

fig.add_trace(go.Bar(x=ax2["Brand"], y=ax2["Model"], name='WithoutRapid Charge'),row=1, col=2)

fig.update_layout(height=500, width=710,
title_text="Multiple Subplots with Titles")

fig.show()

2 Likes

Many thanks once again for all your help it’s honestly very much appreciated especially the amount of time you put in.

Have a nice safe rest of your week.

1 Like