Python Forum
Help with Matplotlib and ordering the axis
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Matplotlib and ordering the axis
#1
Hello guys! Im from México and I am analyzing some data from my country.

This is the code that I am working with:

# Importing libraries import pandas as pd import matplotlib.pyplot as plt # Reading .CSV file df = pd.read_csv("/home/aerospace/Documents/Mortalidad_07.csv") # Cleaning up the data df = df.drop(columns=["No especificado", "No especificado.1", "No especificado.2"]) print(df.columns) # Get the variables that we want state = df["Estados"][1:33] man2017 = df["Hombres"][1:33] wman2017 = df["Mujeres"][0:33] man2018 = df["Hombres.1"][0:33] wman2018 = df["Mujeres.1"][0:33] man2019 = df["Hombres.2"][0:33] wman2019 = df["Mujeres.2"][0:33] print(state, man2017) plt.plot(state, man2017, 'ko') plt.xticks(rotation=90) plt.title("Defunciones por homicidio, por entidad federativa 2017") plt.xlabel("Estados de la República Mexicana") plt.ylabel("Total de homicidios en 2017") plt.grid() plt.show()
The problem is this. If you plot that, we'll see the Y axis ordered randomly, and I want to see the Y axis from lowest to highest. I mean, at the top see the highest values and at the bottom the lowest. the x-axis is arranged alphabetically
Reply
#2
Python has functions for sorting data and pandas has a few more.
Reply
#3
(Dec-12-2020, 07:41 PM)deanhystad Wrote: Python has functions for sorting data and pandas has a few more.

I tried doing that with "df.sort_values(by="Hombres", ascending=True), but didn't work. How would you do it?
Reply
#4
sorted_data = df.sort_values(by="Hombres", ascending=True)

or

df.sort_values(by="Hombres", ascending=True, inplace=True)

It depends on if you only want sorted data for the plot, or if you want the dataframe sorted. If you do not specify inplace=True the sort_values function creates and returns a new dataframe. The original dataframe is not changed.
theliberalguy97 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib x-axis text move bottom upward jacklee26 3 3,000 May-31-2023, 04:28 AM
Last Post: jacklee26
  x-axis labels with Matplotlib Mark17 8 6,261 Mar-23-2022, 06:10 PM
Last Post: Mark17
  How to make x-axis readable with matplotlib Mark17 7 9,544 Mar-01-2022, 04:30 PM
Last Post: DPaul
  matplotlib x axis range goes over the set range Pedroski55 5 8,463 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 9,175 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  drf ordering by custom field and add ranking in to_representation tomfong521 0 2,742 Mar-24-2021, 09:56 AM
Last Post: tomfong521
  Graphics Formatting - X-axis Notation and Annotations - Matplotlib silviover_junior 0 3,067 Mar-17-2021, 01:19 PM
Last Post: silviover_junior
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 5,033 Mar-11-2021, 10:52 AM
Last Post: buran
  matplotlib x-axis wrong order SchroedingersLion 4 6,898 Feb-23-2021, 05:42 PM
Last Post: nilamo
  What are these ordering code doing coltson 1 2,856 Nov-03-2020, 05:07 AM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.