JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Plotting a Python dictionary in key order

Author:JIYIK Last Updated:2025/05/06 Views:

This tutorial shows how to pyplotplot a dictionary in Python using the module from Python's matplotlib library. We will key-valueplot the dictionary as pairs, where the x-axis is the keys of the dictionary and the y-axis is the values ​​of the dictionary.


Plotting Python dictionaries using Matplotlibthe library'spyplot

The following code example converts the dictionary into a list of key-value pairs and then sortedsorts it using the function so that our graph is in order. Once sorted, the and values zip​​are extracted from the list using the function .xy

After getting the values ​​of the x-axis and y-axis, we can pass them as parameters to plt.plotthe function for graph drawing.

Sample code:

import matplotlib.pylab as plt

my_dict = {"Khan": 4, "Ali": 2, "Luna": 6, "Mark": 11, "Pooja": 8, "Sara": 1}

myList = my_dict.items()
myList = sorted(myList)
x, y = zip(*myList)

plt.plot(x, y)
plt.show()

Output:

Python Draw Dictionary

We can also add labels to the x-axis and y-axis and add a title to the graph. The following code example shows how we can add them to the graph.

import matplotlib.pylab as plt

my_dict = {"Khan": 4, "Ali": 2, "Luna": 6, "Mark": 11, "Pooja": 8, "Sara": 1}

myList = my_dict.items()
myList = sorted(myList)
x, y = zip(*myList)

plt.plot(x, y)
plt.xlabel("Key")
plt.ylabel("Value")
plt.title("My Dictionary")
plt.show()

Output:

Python plotting dictionary with labels

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Finding a key by value in a Python dictionary

Publish Date:2025/05/06 Views:65 Category:Python

A dictionary is a collection of elements in key-value pairs. The elements stored in a dictionary are unordered. In this article, we will look at different ways to find a key by value in a Python dictionary. Normally, you use keys to access

How to Convert a Python Dictionary to a Pandas DataFrame

Publish Date:2025/05/02 Views:64 Category:Python

We will cover methods for dictionary converting Python to Pandas , as well as options for using as and as values ​​and converting nested to . datafarme keys columns values row dictionary DataFrame We will also introduce another method u

在 Python 字典中按值查找键

Publish Date:2023/12/23 Views:136 Category:Python

本教程演示了如何在 Python 字典中按值获取键。字典是一个键值对中的元素集合。字典中存储的元素是无序的。

按键值顺序绘制 Python 字典

Publish Date:2023/12/23 Views:95 Category:Python

本教程介绍了如何在 python 中绘制一个字典。本教程介绍了如何使用 Python 的 matplotlib 库中的 pyplot 模块在 Python 中绘制一个字典。

Python 获取字典中的第一个键

Publish Date:2023/12/23 Views:161 Category:Python

本教程说明了如何在 python 中获取字典的第一键。本教程介绍了我们如何在 Python 字典中获取首键。所谓首键,是指保存在字典第一个索引中的键。

初始化 Python 字典

Publish Date:2023/12/23 Views:160 Category:Python

本教程演示如何在 python 中初始化字典。Python 字典是有序且可变的。字典不允许存储重复项。在 Python 3.6 及以下版本中,字典曾经是无序的。

在 Python 字典中寻找最大值

Publish Date:2023/12/23 Views:136 Category:Python

本教程介绍了如何在 Python 中获取字典中具有最大值的键。本教程介绍了如何在 Python 中获取一个具有最大值的键。由于该方法与以前的 Python 版本相比已经发生了变化,因此它还列出了一些示例

如何从 Python 字典中删除元素

Publish Date:2023/04/22 Views:130 Category:Python

在 Python 中,字典(Dictionary)是一种用于存储键值对的数据类型。有时候我们需要从字典中删除一个或多个元素,本文将介绍如何在 Python 中删除字典中的元素。 使用 del 语句删除字典

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial