Python Forum
Unable to plot the graph as needed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to plot the graph as needed
#1
Hi

I am currently facing an problem which might be usual to you as i have just started my party, i thought to take help from Masters.

I am trying to plot a graph for the label which i created


fig, ax = plt.subplots(figsize=(8, 8))
counts = df.label.value_counts(normalize=True) * 100
print(counts)
Output is:

Output:
0 100.0 Name: label, dtype: float64
There supposed to be
0 100.0
-1 0.0
1 0.0

Since my label has 0 only thats why it is giving 100 viz. correct

when i am executing second part of python:
sns.barplot(x=counts.index, y=counts, ax=ax)
ax.set_xticklabels([ 'Neutral', 'Negative','Positive'])
ax.set_ylabel("Percentage")
plt.show()
I gives value error
Error:
-------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-15-243f41dc9975> in <module> 13 14 ---> 15 ax.set_xticklabels([ 'Neutral', 'Negative','Positive']) 16 ax.set_ylabel("Percentage") 17 c:\Bhaskar\ML\env\lib\site-packages\matplotlib\axes\_base.py in wrapper(self, *args, **kwargs) 61 62 def wrapper(self, *args, **kwargs): ---> 63 return get_method(self)(*args, **kwargs) 64 65 wrapper.__module__ = owner.__module__ c:\Bhaskar\ML\env\lib\site-packages\matplotlib\cbook\deprecation.py in wrapper(*args, **kwargs) 449 "parameter will become keyword-only %(removal)s.", 450 name=name, obj_type=f"parameter of {func.__name__}()") --> 451 return func(*args, **kwargs) 452 453 return wrapper c:\Bhaskar\ML\env\lib\site-packages\matplotlib\axis.py in _set_ticklabels(self, labels, fontdict, minor, **kwargs) 1794 if fontdict is not None: 1795 kwargs.update(fontdict) -> 1796 return self.set_ticklabels(labels, minor=minor, **kwargs) 1797 1798 @cbook._make_keyword_only("3.2", "minor") c:\Bhaskar\ML\env\lib\site-packages\matplotlib\axis.py in set_ticklabels(self, ticklabels, minor, **kwargs) 1715 # remove all tick labels, so only error for > 0 ticklabels 1716 if len(locator.locs) != len(ticklabels) and len(ticklabels) != 0: -> 1717 raise ValueError( 1718 "The number of FixedLocator locations" 1719 f" ({len(locator.locs)}), usually from a call to" ValueError: The number of FixedLocator locations (1), usually from a call to set_ticks, does not match the number of ticklabels (3).
I want it to display all positive negative and neutral result, but its showing only one graph in display. please help how to achieve this. I dont mind changing whole code.
Reply
#2
(May-26-2021, 05:54 AM)drunkenneo Wrote: Hi

I am currently facing an problem which might be usual to you as i have just started my party, i thought to take help from Masters.

I am trying to plot a graph for the label which i created


fig, ax = plt.subplots(figsize=(8, 8))
counts = df.label.value_counts(normalize=True) * 100
print(counts)
Output is:

Output:
0 100.0 Name: label, dtype: float64
There supposed to be
0 100.0
-1 0.0
1 0.0

Since my label has 0 only thats why it is giving 100 viz. correct

when i am executing second part of python:
sns.barplot(x=counts.index, y=counts, ax=ax)
ax.set_xticklabels([ 'Neutral', 'Negative','Positive'])
ax.set_ylabel("Percentage")
plt.show()
I gives value error
Error:
-------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-15-243f41dc9975> in <module> 13 14 ---> 15 ax.set_xticklabels([ 'Neutral', 'Negative','Positive']) 16 ax.set_ylabel("Percentage") 17 c:\Bhaskar\ML\env\lib\site-packages\matplotlib\axes\_base.py in wrapper(self, *args, **kwargs) 61 62 def wrapper(self, *args, **kwargs): ---> 63 return get_method(self)(*args, **kwargs) 64 65 wrapper.__module__ = owner.__module__ c:\Bhaskar\ML\env\lib\site-packages\matplotlib\cbook\deprecation.py in wrapper(*args, **kwargs) 449 "parameter will become keyword-only %(removal)s.", 450 name=name, obj_type=f"parameter of {func.__name__}()") --> 451 return func(*args, **kwargs) 452 453 return wrapper c:\Bhaskar\ML\env\lib\site-packages\matplotlib\axis.py in _set_ticklabels(self, labels, fontdict, minor, **kwargs) 1794 if fontdict is not None: 1795 kwargs.update(fontdict) -> 1796 return self.set_ticklabels(labels, minor=minor, **kwargs) 1797 1798 @cbook._make_keyword_only("3.2", "minor") c:\Bhaskar\ML\env\lib\site-packages\matplotlib\axis.py in set_ticklabels(self, ticklabels, minor, **kwargs) 1715 # remove all tick labels, so only error for > 0 ticklabels 1716 if len(locator.locs) != len(ticklabels) and len(ticklabels) != 0: -> 1717 raise ValueError( 1718 "The number of FixedLocator locations" 1719 f" ({len(locator.locs)}), usually from a call to" ValueError: The number of FixedLocator locations (1), usually from a call to set_ticks, does not match the number of ticklabels (3).
I want it to display all positive negative and neutral result, but its showing only one graph in display. please help how to achieve this. I dont mind changing whole code.


You have not posted the full python code, the first 3 lines result in df not defined and plt not defined.
You'll need to show full code with your python imports to get an answer
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  first time use plot - I get empty graph korenron 6 1,969 Feb-04-2023, 02:14 PM
Last Post: deanhystad
  How to plot 3D graph of non numerical value? Gevni 0 2,188 Mar-05-2021, 02:50 PM
Last Post: Gevni
  Python Matplotlib: How do I plot lines with different end point in the same graph? JaneTan 0 1,553 Feb-28-2021, 11:56 AM
Last Post: JaneTan
  Plot Graph quest_ 2 2,085 Jan-18-2021, 07:58 PM
Last Post: quest_
  How to plot intraday data of several days in one plot mistermister 3 2,855 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  plot multiple employee sales data in a single graph pitanshu 0 1,883 Oct-24-2019, 01:56 PM
Last Post: pitanshu
  Plot multiple csv into one graph problem with visualize linkxxx86 1 5,670 Oct-14-2019, 05:54 PM
Last Post: linkxxx86
  How to plot vertically stacked plot with same x-axis and SriMekala 0 1,886 Jun-12-2019, 03:31 PM
Last Post: SriMekala
  Plot a graph using matplotlib.pyplot Tibas 1 3,425 Feb-23-2018, 10:47 AM
Last Post: Tibas
  How to plot two list on the same graph with different colors? Alberto 2 28,698 Jul-18-2017, 09:20 AM
Last Post: Alberto

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020