0% found this document useful (0 votes)
12 views

Matplotlib

The document discusses creating bar charts and customizing plots using Matplotlib. It demonstrates how to create bar charts comparing median salaries by age for different developer types and how to create horizontal bar charts showing the most popular programming languages by number of users.

Uploaded by

dbqatest001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Matplotlib

The document discusses creating bar charts and customizing plots using Matplotlib. It demonstrates how to create bar charts comparing median salaries by age for different developer types and how to create horizontal bar charts showing the most popular programming languages by number of users.

Uploaded by

dbqatest001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Matplotlib.

ipynb - Colab 26/05/24, 4:34 PM

Matplotlib

↳ 3 cells hidden

Importing library

[ ] ↳ 2 cells hidden

1. Creating and Customizing Our First Plots


[ ] ↳ 6 cells hidden

 2. Bar Charts and Analyzing Data from CSVs

import numpy as np
plt.style.use("fivethirtyeight")
import matplotlib.pyplot as plt

plt.style.use("fivethirtyeight")

plt.bar(x_indexes - width, dev_y, width=width, color='#444444', label='All Devs')


ages_x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]

x_indexes = np.arange(len(ages_x))
width = 0.25

dev_y = [38496, 42000, 46752, 49320, 53200,


56000, 62316, 64928, 67317, 68748, 73752]

plt.bar(x_indexes - width, dev_y, width=width, color='#444444', label='All Devs')

# Median Python Developer Salaries by Age


# We are using same ages_x
py_dev_y = [45372, 48876, 53850, 57287, 63016,
65998, 70003, 70000, 71496, 75370, 83640]

plt.bar(x_indexes, py_dev_y, width=width, color='#008fd5', label='Python')

# Median JavaScript Developer Salaries by Age


js_dev_y = [37810, 43515, 46823, 49293, 53437,
56373, 62375, 66674, 68745, 68746, 74583]

https://colab.research.google.com/drive/1-e6frQL-0mSS9pEkO968J_tU35DC6Zfo Page 1 of 7
Matplotlib.ipynb - Colab 26/05/24, 4:34 PM

plt.bar(x_indexes + width, js_dev_y, width=width, color='#e5ae38', label='JavaScrip

# Assign X-label and Y-label respectively


plt.xlabel("Ages")
plt.ylabel("Median Salary (USD)")

plt.xticks(ticks=x_indexes, labels=ages_x)

# Give title to plot


plt.title("Median Salary (USD) by Age")

plt.legend()

plt.tight_layout()

plt.show()

https://colab.research.google.com/drive/1-e6frQL-0mSS9pEkO968J_tU35DC6Zfo Page 2 of 7
Matplotlib.ipynb - Colab 26/05/24, 4:34 PM

The plt.xticks() function in Matplotlib is used to customize the tick locations and labels on
the x-axis of a plot. Here's an explanation of the parameters:

ticks: This parameter speciMes the locations on the x-axis where you want to place the ticks.
It takes a list of values indicating the positions of the ticks. In your example, x_indexes likely
contains the positions where you want to place ticks on the x-axis.

labels: This parameter is used to provide custom labels for the ticks. It takes a list of labels
corresponding to the tick positions speciMed in the ticks parameter. In your example, ages_x
likely contains the labels that you want to display at each tick position.

import csv

from collections import Counter

with open("/content/matplotlib_2.csv") as csv_file:


csv_reader = csv.DictReader(csv_file)

language_counter = Counter()

for row in csv_reader:


language_counter.update(row['LanguagesWorkedWith'].split(';'))

languages = []
popularity = []

for item in language_counter.most_common(15):


languages.append(item[0])
popularity.append(item[1])

# print(languages)
# print(popularity)

languages.reverse()
popularity.reverse()

# plt.bar(languages, popularity)

# Assign X-label and Y-label respectively


# plt.xlabel("Programming Languages")
# plt.ylabel("Number of People Who Use")

# bar is not readable so changing to barh


plt.barh(languages, popularity)

https://colab.research.google.com/drive/1-e6frQL-0mSS9pEkO968J_tU35DC6Zfo Page 3 of 7
Matplotlib.ipynb - Colab 26/05/24, 4:34 PM

plt.xlabel("Number of People Who Use")

# Give title to plot


plt.title("Most Popular Languages")

plt.legend()
plt.tight_layout()
plt.show()

WARNING:matplotlib.legend:No artists with labels found to put in legend. Note

import pandas as pd

data = pd.read_csv("/content/matplotlib_2.csv")

ids = data['Responder_id']
lang_responses = data['LanguagesWorkedWith']

for response in lang_responses:


language_counter.update(response.split(';'))

languages = []
popularity = []

https://colab.research.google.com/drive/1-e6frQL-0mSS9pEkO968J_tU35DC6Zfo Page 4 of 7
Matplotlib.ipynb - Colab 26/05/24, 4:34 PM

for item in language_counter.most_common(15):


languages.append(item[0])
popularity.append(item[1])

# print(languages)
# print(popularity)

languages.reverse()
popularity.reverse()

# plt.bar(languages, popularity)

# Assign X-label and Y-label respectively


# plt.xlabel("Programming Languages")
# plt.ylabel("Number of People Who Use")

# bar is not readable so changing to barh


plt.barh(languages, popularity)

plt.xlabel("Number of People Who Use")

# Give title to plot


plt.title("Most Popular Languages")

plt.legend()
plt.tight_layout()
plt.show()

https://colab.research.google.com/drive/1-e6frQL-0mSS9pEkO968J_tU35DC6Zfo Page 5 of 7
Matplotlib.ipynb - Colab 26/05/24, 4:34 PM

WARNING:matplotlib.legend:No artists with labels found to put in legend. Note

Start coding or generate with AI.

https://colab.research.google.com/drive/1-e6frQL-0mSS9pEkO968J_tU35DC6Zfo Page 6 of 7
Matplotlib.ipynb - Colab 26/05/24, 4:34 PM

https://colab.research.google.com/drive/1-e6frQL-0mSS9pEkO968J_tU35DC6Zfo Page 7 of 7

You might also like