Graph points and shapes with matplotlib

2 answers

You can adjust the size of the characters used in the scatter plot by selecting the s parameter. You will also probably need to adjust the size of your shape ( figsize ) or the size of your axes (with add_axes ). This is because the scatter symbols are square, in display units, and the x and y axes are not automatically adjusted, so width-one-change = height-one-box.

In other words, the example you provided is a rectangular graph with a width> width, and the height and width are chosen to make the width-one-change-height-of-one-box.

Here is an example of applying these methods:

 import matplotlib.pyplot as plt BOX = 5 START = 365 changes = (8, -3, 4, -4, 12, -3, 7, -3, 5, -9, 3) # one way to force dimensions is to set the figure size: fig = plt.figure(figsize=(5, 10)) # another way is to control the axes dimensions # for axes to have specific dimensions: # [ x0, y0, w, h] in figure units, from 0 to 1 #ax = fig.add_axes([.15, .15, .7*.5, .7]) ax = fig.add_axes([.15, .15, .7, .7]) def sign(val): return val / abs(val) pointChanges = [] for chg in changes: pointChanges += [sign(chg)] * abs(chg) symbol = {-1:'o', 1:'x'} chgStart = START for ichg, chg in enumerate(changes): x = [ichg+1] * abs(chg) y = [chgStart + i * BOX * sign(chg) for i in range(abs(chg))] chgStart += BOX * sign(chg) * (abs(chg)-2) ax.scatter(x, y, marker=symbol[sign(chg)], s=175) #<----- control size of scatter symbol ax.set_xlim(0, len(changes)+1) fig.savefig('pointandfigure.png') plt.show() 

The method developed for each scatter plot is very hacky, but the key point is that I needed to play with the scatter parameter s and the size of the shape in order to get something like the desired effect.

The resulting graph:

enter image description here

Ideally, you can create your own method, modeled after the scattering method. It will create a custom Collection instance that will include the x, o, and month labels. It also: a) automatically adjusts the aspect of the axis / figure; or b) creates asymmetric characters. This is obviously an advanced option for those who want to contribute as a developer to the Matplotlib project.

+11
source

No personal experience, but could there be set_view_interval () or set_data_interval () from here ? I used Matplotlib for the project, but did not have to play with fixing the width of the x axis.

0
source

Source: https://habr.com/ru/post/905255/


All Articles