0

I am creating a 2 axis line chart with Matplotlib. There are multiple data series in the chart. I have the y axis set to log scale because the 2 series have different ranges.

import pandas as pd
import matplotlib.pyplot as plt

data = {  
    'Sales':[ 1141,     1356,   1604,   2165,   2494,   2933,   3445,   4198,   4287,   4760],
    'Profit': [10,  20,  30,    40,     50,     60,     70,     80,     90, 100     ],
    'Year':['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019']
}

df = pd.DataFrame(data, columns = ['Sales', 'Profit',  'Year'])

# multiple line plot
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_yticklabels(df.Profit)
plt.plot(  'Year','Sales', data=df, marker='o', color='brown', linewidth=1)
plt.plot(  'Year','Profit', data=df, marker='o', color='green', linewidth=1, linestyle='dashed')
plt.legend()
plt.yscale("log")
plt.draw()

The output chart is shown with very less y axis labels. Is there a way to display all the y -axis values' labels in the cart? Or is there a way to show y-axis labels at a higher frequency, to make reading the values easier?

enter image description here

I have tried this related solution How to display all label values in matplotlib? but it did not show all the y labels.

I have also tried to set explicit y tick values, like this, but still the labels did not show up.

ax1 = fig.add_subplot(111)
ax1.set_yticklabels(np.arange(5000), minor=True)
Shankar
  • 2,625
  • 3
  • 25
  • 49
  • 1
    Not clear: do you want to annotate each point with its value? – wwii Oct 08 '19 at 18:57
  • 1
    Labelling all minor ticks would look like [this](https://i.stack.imgur.com/bK2Xg.png); are you sure this is what you want? – ImportanceOfBeingErnest Oct 08 '19 at 18:57
  • @wwii Yes. It would improve readability if each point had its value shown in the y axis. – Shankar Oct 08 '19 at 19:02
  • @ImportanceOfBeingErnest if I cannot show the individual y axis values on the chart, I would show the minor ticks. How can I do that? – Shankar Oct 08 '19 at 19:03
  • Possible duplicate of [Matplotlib scatter plot with different text at each data point](https://stackoverflow.com/questions/14432557/matplotlib-scatter-plot-with-different-text-at-each-data-point) – wwii Oct 08 '19 at 22:42
  • others:https://stackoverflow.com/questions/52240633/matplotlib-display-value-next-to-each-point-on-chart ... https://stackoverflow.com/questions/22272081/label-python-data-points-on-plot – wwii Oct 08 '19 at 22:42

0 Answers0