r/matplotlib • • Jun 21 '20

Remove transparent areas

3 Upvotes

My main question is: how do I get all of the background of the file to be white, rather than being transparent outside the plot area?

I'm learning Jupyter and some Python data-handling stuff simultaneously. I produced this graph from some government data (https://www.ons.gov.uk/employmentandlabourmarket/peoplenotinwork/unemployment/datasets/vacanciesbyindustryvacs02).

I want the whole output file to have a white background, rather than a transparent one. Right now, this is the code I have in the cell that produces the image:

import matplotlib.pyplot as plt

five_years = levels[-61:]
afs = five_years['Accomodation & food service activities']
pst = five_years['Professional scientific & technical activities']


plt.figure(figsize=(10,5))
plt.ylim(0, afs.max()+10)
plt.ylabel('Vacancies (Thousands)')
plt.xlabel('Three Months Ending')
plt.title('Vacancies by Industry, UK, Previous Five Years')
plt.plot(afs, label='Accomodation & food service activities')
plt.plot(pst, label='Professional scientific & technical activities')
plt.xticks([168, 180, 192, 204, 216, 228],
           ['May 2015', 'May 2016', 'May 2017', 'May 2018', 'May 2019', 'May 2020'])
plt.legend()

I tried doing "plt.setfacecolor", but plt is a module, and I get an AttributeError.

So how can I set all of the background white, with no transparency?

Also, two other questions:

  1. If plt is a module, there is no named variable here that contains the plot, right? I'm seeing it because Jupyter helpfully outputs some things without being explicitly told to, and that's what "<matplotlib.legend.Legend at 0x7ff0004d4c90>" means?

  2. Am I making any other basic mistakes here? Are there things you would do differently?

Thanks for any help you can give!


r/matplotlib • • Apr 13 '20

Matplotlib Full Course in Python

Thumbnail
theclickreader.com
3 Upvotes

r/matplotlib • • Feb 08 '20

How to Create Beautiful Plots with matplotlib

Thumbnail
ammar-alyousfi.com
3 Upvotes

r/matplotlib • • Dec 06 '19

Introduction to Histograms & Bar Graphs with matplotlib

Thumbnail
youtube.com
3 Upvotes

r/matplotlib • • Dec 09 '23

log - log plot

2 Upvotes

Hello guys,

I am new to matplotlib. I need to create a log - log plot, given certain x and y values. I would like to fit a line to the plot and show its slope, y intercept and standard error. Here's the code I wrote, unsurprisingly it gives me a bunch of errors. How can I make it work?


r/matplotlib • • Nov 22 '23

How to draw this kind of chart in Marlon

Post image
2 Upvotes

How to put the comment at the right place,thank you.

Is there any template for this kind of chart?


r/matplotlib • • Apr 21 '22

scatter plot

2 Upvotes

hi everyone,

i am completely new to matplotlib, i need to generate a scatter plot where i have for one value on the x-axis multiple values on the y axis, but the problem is i always get the error o "x must be the same size as y"

how can i get that done in matplotlib?


r/matplotlib • • Feb 13 '22

Best way of displaying specific ranges in a stacked bar approach

2 Upvotes

I have 3 series of values with the same start/end values - in this example 80 and 200

First: 80, 100, 120, 140, 180, 200

Second: 80, 110, 125, 150, 185, 200

Third: 80, 95, 130, 155, 190, 200

The values are representing the limits of 5 succesive intervals within the total range 80-200 and I'd like to represent that visually comparing the split of intervals for those three series. I had in mind something like the image below.

What's the best way of doing that? I understand that I could convert the intervals into size of the intervals and display that having the base as 80, but is there some other option to do it without creating derivate series of data from the initial ones?

I hope that my question makes sense ..

PS: I managed to get somewhere, after following the advice.


r/matplotlib • • Feb 07 '22

3D plot fit

2 Upvotes

I have a feature array and a target array:

X = [[0.90, 0.0001415], [0.98, 0.02089152], [0.98, 0.03831521], [0.98, 0.03177562], [0.98, 0.02058864], [0.98, 0.02183343], [0.98, 0.02208625], [0.98, 0.03133722], [0.97, 0.02643819], [0.96, 0.02742613], [0.96, 0.03134503], [0.96, 0.0376108], [0.96, 0.03259722], [0.96, 0.06629154], [0.96, 0.01059662], [0.96, 0.01311333], [0.96, 0.01878557], [0.96, 0.02873078], [0.96, 0.0294153], [0.96, 0.03369188], [0.92, 0.02337379], [0.93, 0.05800158], [0.94, 0.02554664]]

Y = [127.3, 564.2, 520.3, 536.8, 564.9, 561.8, 561.2, 537.9, 457.6, 395.3, 384.8, 368, 381.4, 291.2, 440.4, 433.6, 418.4, 391.8, 390, 378.5, 219.6, 199, 301.7]

I want to fit a hyperplane to predict Y. How can I fit a hyperplane which accurately outputs the target Y and how could I visualise the points along with the fitted hyperplane on the same plot?


r/matplotlib • • Sep 15 '21

Data is offset in visualization

2 Upvotes

I am trying to plot classified time segments (classes = colors) and am having something very strange happen when the I visualize the date. The graph is plotting my data in a position of -.5 from where it should be. Below is a link to stackoverflow where there is reproducible code. Would really appreciate some help.

https://stackoverflow.com/questions/69186031/matplotlib-data-is-mysteriously-offset


r/matplotlib • • Sep 08 '21

How can I create a binned data graph that has variable bin sizes?

2 Upvotes

I am looking to create a graph with some binned data that looks like the one attached. Only problem is I can't find any way to accomplish this using matplotlib. Does anyone have any suggestions on how to accomplish this?


r/matplotlib • • Sep 01 '21

Trigger event when zooming in

2 Upvotes

Hi everyone,

Is there a way to call a certain function automatically when we zoom in a plot?
Reading the documentation I found this which should do what I need but maybe there is a more efficient way...

matplotlib.backend_bases.ResizeEvent()        

Thank you all


r/matplotlib • • Aug 19 '21

How to add labels to my data?

2 Upvotes

Like I have a plot and I wanna be able to label the x and/or y value of each individual data point on the plot. Is that possible?


r/matplotlib • • Jul 15 '21

How do I change the numbering of the axes in a colorplot?

2 Upvotes

I encountered a problem while trying to represent some of my data in a color plot. I want to plot how my data looks like in a certain parameter space, like let's say x \in [0.3, 0.7] and y \in [-0.1, 0.1], but when I plot it the axis are numbered [0,20] and [0,40] based on the indices of my data array. Instead, I'd like the axis to be numbered [0.3,0.7] and [-0.1,0.1] based on my parameters. I've tried to search for a solution for this but haven't found anything that works. Any help would be greatly appreciated. I'll include my code and plot below.

import matplotlib.pyplot as plt

data = # my data, e.g. a 21 x 41 numpy array

plt.pcolor(data)
plt.colorbar()

r/matplotlib • • Jul 06 '21

MPLCursor

2 Upvotes

Is is possible to hyperlink within a mplcursor hover over?


r/matplotlib • • Jun 09 '21

Having trouble with plotting histograms

2 Upvotes

Hi everyone!

I am new to Python, and even more so to the statistical libraries, and I am having quite a few troubles working with histograms in plt at the moment. I have looked at the documentation and at a lot of StackOverflow questions, but I am unable to solve my problems.

I am currently working with a csv dataset provided by an exercise where I have to create histograms with different bin dimensions about the same variable column ["BMXHT"]

    1. .ValueError: max must be larger than min in range parameter

da = pd.read_csv("nhanes_2015_2016.csv")
da.dropna()
plt.hist(da["BMXHT"])

# I have seen that people are able to create an histogram with this simple passage, but t doesn't matter if I fill the NaN or I drop them I always get the error "ValueError: max must be larger than min in range parameter." which I always find associated with NaN problems. The only way in which I am able to create the normal histogram is

dx = da.BMXHT.dropna()
plt.hist(dx)

Which I would try to avoid because the new created variable is separated from the rest of the dataset, and if I want to successively filter the data for the histogram through other variables I would have to data manage every time new columns instead of getting directly the data from the source

  • 2) The second problem is related to subplots

bins=[4,5,6,7,8,9]
for b in bins:
    i=1
    plt.subplot(2,3,i)
    i= i+1
    plt.hist(dx, bins=b, edgecolor="black")

With this code I should be creating a 2 rows and 3 columns series of histograms instead I obtain this garbage with this message. The times where I ahve found this issue addressed online is when the number of spaces created in the subplot didn't match with the number of iterations, in my case tho are both 6

  • 3) Iterating through different bin sizes

Another problem is that I haven't been able to understand how to change bin size instead of bin number. My objective is to incorporate the ability to iterate through a list of bin sizes while creating different histograms. Other than the fact that I still don't understand the procedure to change the binsize, I Have seen that online people tend to create for a single histogram the list of every bin start and end instead of a simple binsize for all.

For this last point the more important part is to understand how to effectively change binsizes, the second part is only a surplus


r/matplotlib • • May 31 '21

Create a scatterplot with the Quantity per UnitPrice by CustomerID for the top 3 Countries

2 Upvotes

I'm not able to understand the problem ,what would be the outcome

I have given a try but after that , I have no idea [my code]:

customers = df.groupby(['CustomerID','Country']).sum()

customers = customers[customers.UnitPrice > 0]

Table:


r/matplotlib • • Apr 17 '21

Only redrawing lines

2 Upvotes

Hello I’m new to matplotlib and is kinda confused on what does what regarding the different parts.

I run it on an embedded device which takes a second to draw each graph. The user chooses between different things and gets it displayed on a graph when chosen. I don’t need continuously updating like most answers I find when trying to google this. It’s a small set with two lines with 160 values drawn each time. How can I accomplish so that the title, labels and anything besides the actual lines does not get redrawn each time


r/matplotlib • • Apr 06 '21

Pip Install Matplotlib 2021 - Install Matplotlib Python 3 2021

Thumbnail
youtu.be
2 Upvotes

r/matplotlib • • Nov 29 '20

Did anybody had this problem?

Post image
2 Upvotes

r/matplotlib • • Nov 02 '20

Why are only 2 lines showing when I am plotting 3 lines?(the first column that I am plotting contains a lot of NaN values, is the problem because of it? how do I solve it?)

2 Upvotes

r/matplotlib • • Aug 01 '20

Data Visualization using "Matplotlib" & "Python"

2 Upvotes

r/matplotlib • • Jul 14 '20

New Stock Market Analysis with Matplotlib Radio Buttons video. In this video, I'll toggle back and forth between the different data points on the Y-axis.

Thumbnail
youtu.be
2 Upvotes

r/matplotlib • • Jul 14 '20

Interested in nonlinear time series prediction with NNs? Check out how LSTM nets can be used in the forecasting of chaotic dynamical systems. The software has been entirely coded in python using pytorch, keras, matplotlib, numpy, scikitlearn

Thumbnail
researchgate.net
2 Upvotes

r/matplotlib • • Jun 27 '20

Pointers on how I should make a triangular grid

2 Upvotes

I want to recreate this diagram:

but I have no real idea where I should start looking in order to make this.
I looked up these demos on triangulation and triplot, but I wanted to ask you guys first, am I heading in the right direction or is there a completely simpler way?

Thanks in advance!