2

I am trying to plot a graph. It has a list contains action name (text) and another list which contains action's frequency (int).

I want to plot a connected graph. This is the code I've written:

xTicks=np.array(action) 
x=np.array(count)
y=np.array(freq)
pl.xticks(x,xTicks)
pl.xticks(rotation=90)
pl.plot(x,y)
pl.show()

In the list xTicks, I have actions and in the list y, I have their frequencies .

With the above code, I am getting this graph:

enter image description here

Why am I getting extra spaces on x axis? It should be symmetric and the size of lists are 130-135 so how can I scroll it?

DavidG
  • 24,279
  • 14
  • 89
  • 82
  • 1
    Make `x` an evenly spaced list and then set the x ticks to `xTicks` – DavidG Jun 27 '17 at 11:05
  • I have got it done. I want to make figure scrollable . I have got some answers but I am not able to understand the code [link](https://stackoverflow.com/a/12237591/8182334) . Can you suggest any document or link ? – Raghvendra Singh Jun 27 '17 at 11:15
  • See the updated answer for a basic working example of the slider – DavidG Jun 27 '17 at 11:34

1 Answers1

1

You need to set x to an evenly spaced list in order to get your x ticks to be evenly spaced. The following is an example with some made up data:

import matplotlib.pyplot as plt
import numpy as np

action = ["test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9"]
freq = [5,3,7,4,8,3,5,1,12]

y=np.array(freq)
xTicks=np.array(action)

x = np.arange(0,len(action),1) # evenly spaced list with the same length as "freq"

plt.plot(x,y)
plt.xticks(x, xTicks, rotation=90)
plt.show()

This produces the following plot:

enter image description here

Update:

A simple example of a slider is shown below. You will have to make changes to this in order to get it exactly how you want but it will be a start:

from matplotlib.widgets import Slider

freq = [5,3,7,4,8,3,5,1,12,5,3,7,4,8,3,5,1,12,5,3,7,4,8,3,5,1,12,4,9,1]

y=np.array(freq)
x = np.arange(0,len(freq),1) # evenly spaced list with the same length as "action"

fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
l, = plt.plot(x, y, lw=2, color='red')

axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor="lightblue")
sfreq = Slider(axfreq, 'Slider', 0.1, 10, valinit=3)

def update(val):
    l.set_xdata(val* x)
    fig.canvas.draw_idle()

sfreq.on_changed(update)

plt.show()

This produces the following graph which has a slider:

enter image description here

DavidG
  • 24,279
  • 14
  • 89
  • 82