PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, June 28, 2022

[FIXED] How to plot using python(Matplotlib) two list in which one list contain year and second contain Negative and positive values

 June 28, 2022     graph, matplotlib, python     No comments   

Issue

I have two lists -

years = ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020']
profits = ['362', '622', '-409', '-92', '-148', '-130', '-128', '98', '-74', '35', '-419']

How can I plot this list, and in which chart should I use here?

I tried this code but output is not right -

    from matplotlib import pyplot as plt
# Figure Size
fig = plt.figure(figsize =(10, 7))
 
# Horizontal Bar Plot
plt.bar(years, profits)
 
# Show Plot
plt.show()

Wrong output which I'm getting:

My Wrong Output Graph Image


Solution

Currently you have a list of strings. You should convert it to a list of floats using

years = list(map(float, years))
profits = list(map(float, profits))

If you want a list of ints, just change float to int.



Answered By - enzo
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing